//myLinkedList(TailInsert) v1.4.4 快速找到未知长度单链表的中间节点
// #define TT template
// #define mll myLinkedList
#define N 5
//#define nullptr NULL
#include
#include
using namespace std;
template <typename T>
struct Node
{
T data;
Node<T>* next;
Node<T>* prev;
};
template <typename T>
class myLinkedList
{
public:
myLinkedList();
myLinkedList(T a[],int n);
myLinkedList(const myLinkedList<T> & imyLinkedList);
~myLinkedList();
int Length();
bool isEmpty();
void PrintList();
T Get(int pos);
int Locate(T x);
void Insert(int i,T x);
T Delete(int i);
void InsertX(int i,T x);
T DeleteX(int i);
T PopFront();
void PushFront(T x);
void Reverse();
void Clear();
void PushBack(T x);
T PopBack();
Node<T>* GetMiddleNode();
int Test();
private:
Node<T>* first;
Node<T>* tail;
int length;
Node<T>* GetX(int pos);
};
template <typename T>
myLinkedList<T>::myLinkedList()
{
first=new Node<T>;
first->next=nullptr;
first->prev=nullptr;
length=0;
tail=first;
}
template <typename T>
myLinkedList<T>::myLinkedList(T a[],int n)
{
first=new Node<T>;
first->next=nullptr;
first->prev=nullptr;
length=0;
Node<T>* p=first;
for (int i=0;i<n;i++)
{
Node<T>* s=new Node<T>;
p->next=s;
s->prev=p;
s->next=nullptr;
s->data=a[i];
p=p->next;
length++;
}
tail=p;
}
template <typename T>
myLinkedList<T>::myLinkedList(const myLinkedList<T> & imyLinkedList)
{
first=new Node<T>;
first->next=nullptr;
length=imyLinkedList.length;
Node<T>* p=first;
Node<T>* ip=imyLinkedList.first->next;
for (int i=0;i<length;i++)
{
Node<T>* s=new Node<T>;
s->data=ip->data;
s->next=nullptr;
s->prev=p;
p->next=s;
p=p->next;
ip=ip->next;
}
tail=p;
}
template <typename T>
myLinkedList<T>::~myLinkedList()
{
while(first->next!=nullptr)
{
Node<T>* q=first->next;
first->next=q->next;
delete q;
}
delete first;
// delete tail;
}
template <typename T>
int myLinkedList<T>::Length()
{
return length;
}
template <typename T>
bool myLinkedList<T>::isEmpty()
{
if (length==0)
return true;
else
return false;
}
template <typename T>
void myLinkedList<T>::PrintList()
{
Node<T>* p=first->next;
while (p!=nullptr)
{
cout<<p->data<<' ';
p=p->next;
}
cout<<endl;
}
template <typename T>
T myLinkedList<T>::Get(int pos)
{
if ((pos>=1)&&(pos<=length))
{
Node<T>* p=first->next;
int cnt=1;//absPOS
//int cnt=0;//binPOS
while (p!=nullptr)
{
if (cnt==pos)
{
break;
}
else
{
p=p->next;
cnt++;
}
}
return p->data;
}
else
{
return -1;
}
}
template <typename T>
int myLinkedList<T>::Locate(T x)
{
Node<T>* p=first->next;
int pos=1;//absPOS
while (p!=nullptr)
{
if (x==p->data)
{
return pos;
}
else
{
pos++;
p=p->next;
}
}
return -1;
}
template <typename T>
void myLinkedList<T>::Insert(int i,T x)//insert after absPOS(i)
{
Node<T>* p=first->next;
int pos=1;
while (p!=nullptr)
{
if (pos==i)
{
Node<T>* s=new Node<T>;
s->data=x;
s->next=p->next;
s->prev=p;
p->next=s;
length++;
if (s->next==nullptr)
tail=s;
break;
}
else
{
pos++;
p=p->next;
}
}
}
template <typename T>
T myLinkedList<T>::Delete(int i)//delete after absPOS(i)
{
Node<T>* p=first->next;
int pos=1;
while (p!=nullptr)
{
if (pos==i)
{
Node<T>* s=p->next;
p->next=s->next;
s->next->prev=p;
if (p->next==nullptr)
tail=p;
T temp=s->data;
delete s;
length--;
return temp;
}
else
{
pos++;
p=p->next;
}
}
}
template <typename T>
Node<T>* myLinkedList<T>::GetX(int pos)//get absPOS
{
if ((pos>=1)&&(pos<=length))
{
Node<T>* p=first->next;
int cnt=1;//absPOS
//int cnt=0;//binPOS
while (p!=nullptr)
{
if (cnt==pos)
{
return p;
}
else
{
p=p->next;
cnt++;
}
}
}
else
{
return nullptr;
}
}
template <typename T>
void myLinkedList<T>::InsertX(int i,T x)//insert after absPOS(i)
{
Node<T>* p=GetX(i);
Node<T>* s=new Node<T>;
s->data=x;
s->next=p->next;
p->next=s;
s->prev=p;
length++;
if (s->next==nullptr)
tail=s;
}
template <typename T>
T myLinkedList<T>::DeleteX(int i)//delete after absPOS(i)
{
Node<T>* p=GetX(i);
Node<T>* s=p->next;
p->next=s->next;
s->next->prev=p;
if (p->next==nullptr)
tail=p;
T temp=s->data;
delete s;
length--;
return temp;
}
template <typename T>
T myLinkedList<T>::PopFront()
{
Node<T>* s=first->next;
T temp=s->data;
first->next=s->next;
if (first->next==nullptr)
tail=first;
else
s->next->prev=first;//ERROR
delete s;
length--;
return temp;
}
template <typename T>
void myLinkedList<T>::PushFront(T x)
{
Node<T>* s=new Node<T>;
s->data=x;
s->next=first->next;
if (first->next!=nullptr)
s->next->prev=s;//ERROR
if (s->next==nullptr)
tail=s;
first->next=s;
length++;
}
template <typename T>
void myLinkedList<T>::Reverse()
{
myLinkedList<T> temp(*this);
Clear();
while (!temp.isEmpty())
{
PushFront(temp.PopFront());
}
}
template <typename T>
void myLinkedList<T>::Clear()
{
while (!isEmpty())
{
PopFront();
}
tail=first;
}
template <typename T>
void myLinkedList<T>::PushBack(T x)
{
Node<T>* s=new Node<T>;
s->data=x;
s->next=tail->next;
s->prev=tail;
tail->next=s;
tail=s;
length++;
}
template <typename T>
T myLinkedList<T>::PopBack()
{
Node<T>* p=tail->prev;
Node<T>* s=tail;
T temp=s->data;
p->next=s->next;
s->prev=nullptr;
tail=p;
delete s;
length--;
return temp;
}
template <typename T>
Node<T>* myLinkedList<T>::GetMiddleNode()
{
Node<T>* p=first->next;
Node<T>* q=first->next;
// while (p!=nullptr)
// {
// p=p->next->next;
// q=q->next;
// }
while (true)
{
if ((p==nullptr)||(p->next==nullptr))
break;
else
{
p=p->next->next;
q=q->next;
}
}
return q;
}
template <typename T>
int myLinkedList<T>::Test()
{
first->data=114514;
int hahaha=first->data;
return hahaha;
}
int main()
{
// cout<<"根据数组初始化新链表myList:"<
cout<<"new myList:"<<endl;
int arr[N]={1,2,3,4,5};
myLinkedList<int> myList(arr,N);
myList.PopBack();
myList.PrintList();
Node<int>* ansptr=myList.GetMiddleNode();
cout<<ansptr->data;
//cout<<"
return 0;
}
//myLinkedList(TailInsert) v1.4.4 链表的冒泡排序 Swap()
// #define TT template
// #define mll myLinkedList
#define N 5
//#define nullptr NULL
#include
#include
using namespace std;
template <typename T>
struct Node
{
T data;
Node<T>* next;
Node<T>* prev;
};
template <typename T>
class myLinkedList
{
public:
myLinkedList();
myLinkedList(T a[],int n);
myLinkedList(const myLinkedList<T> & imyLinkedList);
~myLinkedList();
int Length();
bool isEmpty();
void PrintList();
T Get(int pos);
int Locate(T x);
void Insert(int i,T x);
T Delete(int i);
void InsertX(int i,T x);
T DeleteX(int i);
T PopFront();
void PushFront(T x);
void Reverse();
void Clear();
void PushBack(T x);
T PopBack();
void Swap(Node<T>* p,Node<T>* q);
bool Compare(Node<T>* p,Node<T>* q);
void BubbleSort();
int Test();
private:
Node<T>* first;
Node<T>* tail;
int length;
Node<T>* GetX(int pos);
};
template <typename T>
myLinkedList<T>::myLinkedList()
{
first=new Node<T>;
first->next=nullptr;
first->prev=nullptr;
length=0;
tail=first;
}
template <typename T>
myLinkedList<T>::myLinkedList(T a[],int n)
{
first=new Node<T>;
first->next=nullptr;
first->prev=nullptr;
length=0;
Node<T>* p=first;
for (int i=0;i<n;i++)
{
Node<T>* s=new Node<T>;
p->next=s;
s->prev=p;
s->next=nullptr;
s->data=a[i];
p=p->next;
length++;
}
tail=p;
}
template <typename T>
myLinkedList<T>::myLinkedList(const myLinkedList<T> & imyLinkedList)
{
first=new Node<T>;
first->next=nullptr;
length=imyLinkedList.length;
Node<T>* p=first;
Node<T>* ip=imyLinkedList.first->next;
for (int i=0;i<length;i++)
{
Node<T>* s=new Node<T>;
s->data=ip->data;
s->next=nullptr;
s->prev=p;
p->next=s;
p=p->next;
ip=ip->next;
}
tail=p;
}
template <typename T>
myLinkedList<T>::~myLinkedList()
{
while(first->next!=nullptr)
{
Node<T>* q=first->next;
first->next=q->next;
delete q;
}
delete first;
// delete tail;
}
template <typename T>
int myLinkedList<T>::Length()
{
return length;
}
template <typename T>
bool myLinkedList<T>::isEmpty()
{
if (length==0)
return true;
else
return false;
}
template <typename T>
void myLinkedList<T>::PrintList()
{
Node<T>* p=first->next;
while (p!=nullptr)
{
cout<<p->data<<' ';
p=p->next;
}
cout<<endl;
}
template <typename T>
T myLinkedList<T>::Get(int pos)
{
if ((pos>=1)&&(pos<=length))
{
Node<T>* p=first->next;
int cnt=1;//absPOS
//int cnt=0;//binPOS
while (p!=nullptr)
{
if (cnt==pos)
{
break;
}
else
{
p=p->next;
cnt++;
}
}
return p->data;
}
else
{
return -1;
}
}
template <typename T>
int myLinkedList<T>::Locate(T x)
{
Node<T>* p=first->next;
int pos=1;//absPOS
while (p!=nullptr)
{
if (x==p->data)
{
return pos;
}
else
{
pos++;
p=p->next;
}
}
return -1;
}
template <typename T>
void myLinkedList<T>::Insert(int i,T x)//insert after absPOS(i)
{
Node<T>* p=first->next;
int pos=1;
while (p!=nullptr)
{
if (pos==i)
{
Node<T>* s=new Node<T>;
s->data=x;
s->next=p->next;
s->prev=p;
p->next=s;
length++;
if (s->next==nullptr)
tail=s;
break;
}
else
{
pos++;
p=p->next;
}
}
}
template <typename T>
T myLinkedList<T>::Delete(int i)//delete after absPOS(i)
{
Node<T>* p=first->next;
int pos=1;
while (p!=nullptr)
{
if (pos==i)
{
Node<T>* s=p->next;
p->next=s->next;
s->next->prev=p;
if (p->next==nullptr)
tail=p;
T temp=s->data;
delete s;
length--;
return temp;
}
else
{
pos++;
p=p->next;
}
}
}
template <typename T>
Node<T>* myLinkedList<T>::GetX(int pos)//get absPOS
{
if ((pos>=1)&&(pos<=length))
{
Node<T>* p=first->next;
int cnt=1;//absPOS
//int cnt=0;//binPOS
while (p!=nullptr)
{
if (cnt==pos)
{
return p;
}
else
{
p=p->next;
cnt++;
}
}
}
else
{
return nullptr;
}
}
template <typename T>
void myLinkedList<T>::InsertX(int i,T x)//insert after absPOS(i)
{
Node<T>* p=GetX(i);
Node<T>* s=new Node<T>;
s->data=x;
s->next=p->next;
p->next=s;
s->prev=p;
length++;
if (s->next==nullptr)
tail=s;
}
template <typename T>
T myLinkedList<T>::DeleteX(int i)//delete after absPOS(i)
{
Node<T>* p=GetX(i);
Node<T>* s=p->next;
p->next=s->next;
s->next->prev=p;
if (p->next==nullptr)
tail=p;
T temp=s->data;
delete s;
length--;
return temp;
}
template <typename T>
T myLinkedList<T>::PopFront()
{
Node<T>* s=first->next;
T temp=s->data;
first->next=s->next;
if (first->next==nullptr)
tail=first;
else
s->next->prev=first;//ERROR
delete s;
length--;
return temp;
}
template <typename T>
void myLinkedList<T>::PushFront(T x)
{
Node<T>* s=new Node<T>;
s->data=x;
s->next=first->next;
if (first->next!=nullptr)
s->next->prev=s;//ERROR
if (s->next==nullptr)
tail=s;
first->next=s;
length++;
}
template <typename T>
void myLinkedList<T>::Reverse()
{
myLinkedList<T> temp(*this);
Clear();
while (!temp.isEmpty())
{
PushFront(temp.PopFront());
}
}
template <typename T>
void myLinkedList<T>::Clear()
{
while (!isEmpty())
{
PopFront();
}
tail=first;
}
template <typename T>
void myLinkedList<T>::PushBack(T x)
{
Node<T>* s=new Node<T>;
s->data=x;
s->next=tail->next;
s->prev=tail;
tail->next=s;
tail=s;
length++;
}
template <typename T>
T myLinkedList<T>::PopBack()
{
Node<T>* p=tail->prev;
Node<T>* s=tail;
T temp=s->data;
p->next=s->next;
s->prev=nullptr;
tail=p;
delete s;
length--;
return temp;
}
template <typename T>
void myLinkedList<T>::Swap(Node<T>* p,Node<T>* q)
{
T temp=p->data;
p->data=q->data;
q->data=temp;
}
template <typename T>
bool myLinkedList<T>::Compare(Node<T>* p,Node<T>* q)
{
if ((p->data)>(q->data))
return true;
else
return false;
}
template <typename T>
void myLinkedList<T>::BubbleSort()
{
Node<T>* BBack=first;
while (BBack->next!=nullptr)
{
BBack=BBack->next;
}
while (BBack!=first->next)
{
Node<T>* p=first->next;
while (p->next!=BBack)
{
if (Compare(p,p->next))
Swap(p,p->next);
p=p->next;
}
if (Compare(p,p->next))
Swap(p,p->next);
BBack=p;
}
}
template <typename T>
int myLinkedList<T>::Test()
{
first->data=114514;
int hahaha=first->data;
return hahaha;
}
int main()
{
// cout<<"根据数组初始化新链表myList:"<
cout<<"new myList:"<<endl;
int arr[N]={5,4,3,2,1};
myLinkedList<int> myList(arr,N);
myList.PrintList();
myList.BubbleSort();
myList.PrintList();
//cout<<"
return 0;
}
//myLinkedList(TailInsert) v1.4.5 约瑟夫环 lab3_3
// #define TT template
// #define mll myLinkedList
#define N 6
#define MAXN 110
#define nullptr NULL
#include
#include
using namespace std;
template <typename T>
struct Node
{
T data;
Node<T>* next;
Node<T>* prev;
};
template <typename T>
class myLinkedList
{
public:
myLinkedList();
myLinkedList(T a[],int n);
myLinkedList(const myLinkedList<T> & imyLinkedList);
~myLinkedList();
int Length();
bool isEmpty();
void PrintList();
T Get(int pos);
int Locate(T x);
void Insert(int i,T x);
T Delete(int i);
void InsertX(int i,T x);
T DeleteX(int i);
T PopFront();
void PushFront(T x);
void Reverse();
void Clear();
void PushBack(T x);
T PopBack();
int Test();
//private:
public:
Node<T>* first;
Node<T>* tail;
int length;
Node<T>* GetX(int pos);
};
template <typename T>
myLinkedList<T>::myLinkedList()
{
first=new Node<T>;
tail=first;
first->next=tail;
first->prev=tail;
length=0;
}
template <typename T>
myLinkedList<T>::myLinkedList(T a[],int n)
{
first=new Node<T>;
first->next=nullptr;
first->prev=nullptr;
length=0;
Node<T>* p=first;
for (int i=0;i<n;i++)
{
Node<T>* s=new Node<T>;
p->next=s;
s->prev=p;
s->next=nullptr;
s->data=a[i];
p=p->next;
length++;
}
tail=p;
tail->next=first;
first->prev=tail;
}
template <typename T>
myLinkedList<T>::myLinkedList(const myLinkedList<T> & imyLinkedList)
{
first=new Node<T>;
first->next=nullptr;
length=imyLinkedList.length;
Node<T>* p=first;
Node<T>* ip=imyLinkedList.first->next;
for (int i=0;i<length;i++)
{
Node<T>* s=new Node<T>;
s->data=ip->data;
s->next=nullptr;
s->prev=p;
p->next=s;
p=p->next;
ip=ip->next;
}
tail=p;
tail->next=first;
first->prev=tail;
}
template <typename T>
myLinkedList<T>::~myLinkedList()
{
// while(first->next!=nullptr)
while(first->next!=first)
{
Node<T>* q=first->next;
first->next=q->next;
delete q;
}
delete first;
// delete tail;
}
template <typename T>
int myLinkedList<T>::Length()
{
return length;
}
template <typename T>
bool myLinkedList<T>::isEmpty()
{
if (length==0)
return true;
else
return false;
}
template <typename T>
void myLinkedList<T>::PrintList()
{
Node<T>* p=first->next;
// while (p!=nullptr)
while (p!=first)
{
cout<<p->data<<' ';
p=p->next;
}
cout<<endl;
}
template <typename T>
T myLinkedList<T>::Get(int pos)
{
if ((pos>=1)&&(pos<=length))
{
Node<T>* p=first->next;
int cnt=1;//absPOS
//int cnt=0;//binPOS
// while (p!=nullptr)
while (p!=first)
{
if (cnt==pos)
{
break;
}
else
{
p=p->next;
cnt++;
}
}
return p->data;
}
else
{
return -1;
}
}
template <typename T>
int myLinkedList<T>::Locate(T x)
{
Node<T>* p=first->next;
int pos=1;//absPOS
// while (p!=nullptr)
while (p!=first)
{
if (x==p->data)
{
return pos;
}
else
{
pos++;
p=p->next;
}
}
return -1;
}
template <typename T>
void myLinkedList<T>::Insert(int i,T x)//insert after absPOS(i)
{
Node<T>* p=first->next;
int pos=1;
// while (p!=nullptr)
while (p!=first)
{
if (pos==i)
{
Node<T>* s=new Node<T>;
s->data=x;
s->next=p->next;
s->prev=p;
p->next=s;
s->next->prev=s;
length++;
// if (s->next==nullptr)
// tail=s;
if (s->next==first)
{
tail=s;
first->prev=tail;
}
break;
}
else
{
pos++;
p=p->next;
}
}
}
template <typename T>
T myLinkedList<T>::Delete(int i)//delete after absPOS(i)
{
Node<T>* p=first->next;
int pos=1;
// while (p!=nullptr)
while (p!=first)
{
if (pos==i)
{
Node<T>* s=p->next;
p->next=s->next;
s->next->prev=p;
// if (p->next==nullptr)
// tail=p;
if (p->next==first)
{
tail=p;
first->prev=tail;
}
T temp=s->data;
delete s;
length--;
return temp;
}
else
{
pos++;
p=p->next;
}
}
}
template <typename T>
Node<T>* myLinkedList<T>::GetX(int pos)//get absPOS
{
if ((pos>=1)&&(pos<=length))
{
Node<T>* p=first->next;
int cnt=1;//absPOS
//int cnt=0;//binPOS
// while (p!=nullptr)
while (p!=first)
{
if (cnt==pos)
{
return p;
}
else
{
p=p->next;
cnt++;
}
}
}
else
{
return nullptr;
}
}
template <typename T>
void myLinkedList<T>::InsertX(int i,T x)//insert after absPOS(i)
{
Node<T>* p=GetX(i);
Node<T>* s=new Node<T>;
s->data=x;
s->next=p->next;
p->next=s;
s->prev=p;
length++;
if (s->next==nullptr)
{
tail=s;
first->prev=tail;
}
}
template <typename T>
T myLinkedList<T>::DeleteX(int i)//delete after absPOS(i)
{
Node<T>* p=GetX(i);
Node<T>* s=p->next;
p->next=s->next;
s->next->prev=p;
// if (p->next==nullptr)
// tail=p;
if (p->next==first)
{
tail=p;
first->prev=tail;
}
T temp=s->data;
delete s;
length--;
return temp;
}
template <typename T>
T myLinkedList<T>::PopFront()
{
Node<T>* s=first->next;
T temp=s->data;
first->next=s->next;
// if (first->next==nullptr)
// tail=first;
if (first->next==first)
{
tail=first;
first->prev=tail;
}
else
s->next->prev=first;//ERROR
delete s;
length--;
return temp;
}
template <typename T>
void myLinkedList<T>::PushFront(T x)
{
Node<T>* s=new Node<T>;
s->data=x;
s->next=first->next;
if (first->next!=nullptr)
s->next->prev=s;//ERROR
// if (s->next==nullptr)
// tail=s;
if (s->next==first)
{
tail=s;
first->prev=tail;
}
first->next=s;
length++;
}
template <typename T>
void myLinkedList<T>::Reverse()
{
myLinkedList<T> temp(*this);
Clear();
while (!temp.isEmpty())
{
PushFront(temp.PopFront());
}
}
template <typename T>
void myLinkedList<T>::Clear()
{
while (!isEmpty())
{
PopFront();
}
tail=first;
tail->next=first;
first->prev=tail;
}
template <typename T>
void myLinkedList<T>::PushBack(T x)
{
Node<T>* s=new Node<T>;
s->data=x;
s->next=tail->next;
s->prev=tail;
tail->next=s;
tail=s;
length++;
first->prev=tail;
}
template <typename T>
T myLinkedList<T>::PopBack()
{
Node<T>* p=tail->prev;
Node<T>* s=tail;
T temp=s->data;
p->next=s->next;
//s->prev=nullptr;//No need to
tail=p;
delete s;
length--;
first->prev=tail;
return temp;
}
template <typename T>
int myLinkedList<T>::Test()
{
first->data=114514;
int hahaha=first->data;
return hahaha;
}
int main()
{
// int arr[N]={1,2,3,4,5,6},m=3;
int arr[MAXN],n=0,m=0;
cout<<"How many soliders?"<<endl;
cin>>n;
cout<<"What are their codes?"<<endl;
for (int i=0;i<n;i++)
{
cin>>arr[i];
}
myLinkedList<int> myl(arr,N);
cout<<"They are:"<<endl;
myl.PrintList();
cout<<"What is the command?"<<endl;
cin>>m;
cout<<"The killing list:"<<endl;
Node<int>* ptr=myl.first;
Node<int>* ptrX=ptr;
for (int i=0;i<N;i++)
{
for (int j=0;j<m;j++)
{
ptrX=ptr;
ptr=ptr->next;
if (ptr==myl.first)
ptr=ptr->next;
}
int temp=ptr->data;
ptrX->next=ptr->next;
cout<<temp<<' ';
// delete ptr;
}
return 0;
}
(全文完)