循环链表和双链表的结合就是循环双链表,代码经过连日奋战终于宣布大功告成【大神请走开】
#include<iostream>
using namespace std;
template<class T>
struct Student
{
T data;
Student<T> * prior,* next;
};
template<class T>
class LinkList
{
public:
LinkList();
LinkList(T a[],int n);
void Insert(T h,T x);
T Delete(T y);
void PrintList1(T u,T o);
void PrintList2(T h,T l);
private:
Student<T> * first;
};
template<class T>
LinkList<T>::LinkList()
{
first=new Student<T>;
first->next=first;
first->prior=first;
}
template<class T>
LinkList<T>::LinkList(T a[],int n)
{
first=new Student<T>;
Student<T> *k = first,*s;
int i;
for(i=0;i<n;i++)
{
s=new Student<T>;
s->data=a[i];
k->next=s;
(k->next)->prior=k;
s->next=first;
first->prior=s;
k=s;
}
}
template<class T>
void LinkList<T>::Insert(T h,T x)
{
Student<T> *p = first , *s;
while(p->data!=h)
{
p=p->next;
}
s=new Student<T>;
s->data=x;
s->prior=p;
s->next=p->next;
p->next->prior=s;
p->next=s;
}
template<class T>
T LinkList<T>::Delete(T y)
{
Student<T> *p =first;
while(p->data!=y)
{
p=p->next;
}
(p->prior)->next=p->next;
(p->next)->prior=p->prior;
return 0;
}
template<class T>
void LinkList<T>::PrintList1(T u,T o)
{
Student<T> *p = first->next;
while(p->data!=u)
{
while((p->next)->data!=o)
{
cout<<p->data<<" ";
p=p->next;
}
break;
}
cout<<endl;
}
template<class T>
void LinkList<T>::PrintList2(T n,T l)
{
Student<T> *p = first->prior;
while(p->data!=n)
{
while((p->prior)->data!=l)
{
cout<<p->data<<" ";
p=p->prior;
}
break;
}
cout<<endl;
}
void main( )
{
int score[6]={10,20,40,50,60,70};
LinkList<int>ScoreList(score, 6);
cout<<"follow the order"<<endl;
ScoreList.PrintList1(70,10);
cout<<"out of the order"<<endl;
ScoreList.PrintList2(10,70);
cout<<"-----------------------------"<<endl;
ScoreList.Insert(20,30);
cout<<"follow the order"<<endl;
ScoreList.PrintList1(70,10);
cout<<"out of the order"<<endl;
ScoreList.PrintList2(10,70);
cout<<"-----------------------------"<<endl;
ScoreList.Delete(50);
cout<<"follow the order"<<endl;
ScoreList.PrintList1(70,10);
cout<<"out of the order"<<endl;
ScoreList.PrintList2(10,70);
}
“循环双链表”代码实现时出现了的问题:【大神请走开】
1、不会灵活运用创建的“头结点”
2、错以为还有“NULL”,出现循环不断
3、对于创建链表时计算机所要做出的操作不了解,导致代码混乱
4、想要实现的功能没在上下文作出联系
“循环双链表”代码实现后的心得:【大神请走开】
1、有些事情,真的,如果你不去做,你完全不会知道这当中到底会出现什么事情
2、辛苦总会有回报的,一步一个脚印慢慢来,不要自以为是
3、对于问题的思考往往比问题本身的价值还要高
4、谢谢明哥
【物联网1132-11】