Circular linked list: Linking the head and tail nodes of a single linked list or a double linked list is a circular linked list. Starting from any node in the circular table, you can access other nodes in the table.
Features: 1. A linked list connected end to end. 2. You can start from any node and access all nodes in the linked list. 3. Judge the characteristics of the tail node in the circular linked list: q-> next == first
empty table structure:
template<class T>
CycleLinkList<T>:: CycleLinkList( )
{
first=new Node<T>;
first->next=first;
}
Tail insertion method to construct circular linked list:
template<class T>
CycleLinkList<T>:: CycleLinkList(T a[ ], int n)
{
first=new Node<T>;
Node<T> *r,*s;
r=first;
for(int i=0; i<n; i++)
{
s=new Node<T>;
s->data=a[i];
r->next=s;
r=s;
}
r->next=first;
}
Head insertion method to construct a circular linked list:
template<class T>
CycleLinkList<T>:: CycleLinkList(T a[ ], int n,int k)
{
first=new Node<T>;
first->next=first;
Node<T> *s;
for(int i=1; i<n; i++)
{
s=new Node<T>;
s->data=a[i];
s->next=first->next;
first->next=s;
}
}
Transform acyclic single-linked lists into circular single-linked lists:
p=first;
while(p->next)
{
p=p->next;
}
p->next=first
Double-linked
list The main disadvantage of the single-linked list is that the link field only points to subsequent nodes and cannot effectively find the predecessor. The double linked list makes up for the above-mentioned shortcomings, adding a pointer to the precursor.
Node structure of double linked list:
template<class T>
struct DNode
{
T data;
DNode<T> *llink;
DNode<T> *rlink;
};
Structural characteristics of doubly-linked lists:
Since there are both forward and backward links in a doubly linked list, it is very convenient to find the direct predecessor and direct successor nodes of any node. Set the pointer p to point to a node in the doubly linked list, then the following formula holds: p-> llink-> rlink = p = p-> rlink-> llink
inserts the node after the doubly linked list P (there is a subsequent node in p)
q->rlink=p->rlink;
q->llink=p;
p->rlink=q;
q->rlink->llink=q;
Processing principle: first process the far-end pointer in each direction, and then process the near-end pointer.
Insert the node after the doubly linked list P (there is a successor node in p)
q->rlink=p->rlink;
p->rlink=q;
q->llink=p;
q->rlink->llink=q;
Processing principle: first insert on the forward linked list, and then insert on the reverse linked list.
Insert a node in an empty table (insert a node at the end of the table)
q->rlink=p->rlink;
p->rlink=q;
q->llink=p;
if(q->rlink)
q->rlink->llink=q;
Delete operation of doubly linked list
p->llink->rlink=p->rlink;
p->rlink->llink=p->llink;
delete(p);
Delete the tail node
p->llink->rlink=p->rlink;
if(p->rlink)
p->rlink->llink=p->llink;
delete(p);
Construction of doubly linked lists-construction of empty lists
template<class T>
DoubleLink <T>::DoubleLink(){
head=new Node<T>;
head->rlink=NULL;
head->llink=NULL;
}
Head insertion
template<class T>
void DoubleLink<T>::Append(T data){
Node<T> *s;
s=new Node<T>;
s->data=data;
s->rlink=head->rlink;
head->rlink=s;
s->llink=head;
if(s->rlink)
s->rlink->llink=s;
return;
}
Traverse
template<class T>
void DoubleLink<T>::Display(){
Node <T> *p;
p=head->rlink;
while(p){
cout<<p->data<<" ";
p=p->rlink;
}
cout<<endl;
return;
}
destruct
template<class T>
DoubleLink<T>::~DoubleLink(){
Node<T> *p,*q;
p=head;
while(p)
{
q=p->rlink;
delete p;
p=q;
}
}
Static linked
lists The necessity of static linked lists: some programming languages do not support pointer types.
Features: Use a sequential storage structure (array) to simulate a linked list.
The static linked list can be described with the help of a one-dimensional array:
#define Maxsize= 链表可能达到的最大长度
template<class T>
structNode{
ElemType data;
int next;
};
Advantages: There is no need to move elements when inserting and deleting, just modify the pointer, so the efficiency is higher.
Disadvantages: Static linked lists are implemented by means of arrays. Therefore, the size of the array cannot be dynamically modified, and storage space cannot be allocated as needed like static arrays.