Single list
A. Junction insertion and deletion operations
1. Insert the node pointed to by s
s->next = p->next;
p->next = s;
2. Delete the node
= p-q> Next; // temporarily stored is deleted node p-> = Q- Next> Next; // delete the list from node q Free (q); // free space of the node q
Second, the establishment of a single list
1. Head interpolation
void CreateListF (LinkNode * & L, elemType A [], int n-) { int I; LinkNode * S; L = (LinkNode *) the malloc ( the sizeof (LinkNode)); L -> Next = NULL; // Create the head node its next field is set to NULL for (I = 0 ; I <n-; I ++ ) { S = (LinkNode *) the malloc ( the sizeof (LinkNode)); S -> data = A [I]; // circulations data structure point s S-> Next = L-> Next; // before node s to the original first node is inserted, then the head node L->next = s; }
2. Tail interpolation
void CreateListR (LinkNode * & L, elemType A [], int n-) { int I; LinkNode * S, * R & lt; L = (LinkNode *) the malloc ( the sizeof (LinkNode)); // Create the first node r = L; // R & lt always point to the end node, initially pointing to the first node for (I = 0 ; I <n-; I ++) // loop to establish a data node { S = (LinkNode *) the malloc ( the sizeof (LinkNode)); S > data = A [I] -; // Create a data node s ; R-> Next = s // after the insertion node s to node r = R & lt S; } R & lt -> next = NULL; // End node next field is set to NULL }
III. TABLE substantially linear operation implemented in a single linked list
Table 1. Initialization linear
void InitList (LinkNode * & L) { L = (LinkNode *) the malloc ( the sizeof (LinkNode)); L -> next = NULL; // Create the first node, which next field is set to NULL }
2. Destruction linear table
void DestroyList (LinkNode * & L) { LinkNode * = L pre, * = L-p> Next; // pre precursor p junction point node the while (p =! NULL); scanning single chain L { Free (pre ); // release pre node pre = p; // pre, after a synchronous mobile node p p = pre-> Next; } Free (pre); // at the end of the cycle p is NULL, pre tail junction point , release it }
3. Analyzing linear table is empty table
bool ListEmpty(LinkNode *L) { return(L->next == NULL); }
4. The table shows the length of Linear
int ListLength (LinkNode * L) { int n-= 0 ; LinkNode * P = L; // P points to the first node, n is set to 0 the while (p-> Next =! NULL) { n- ++ ; P = p- > Next; } return (n-); }
The output of the linear form
void DispList(LinkNode *L) { LinkNode *p = L->next; while(p != NULL) { printf("%d ",p->data); p = p->next; } printf("\n"); }
6. request a data element values in the linear form
bool GetElem(LinkNode *L,int i,ElemType &e) { int j=0 LinkNode *p = L; if(i <= 0) return false; while(j<i && p!=NULL) { j++; p = p->next; } if(p == NULL) return false; else { e = p->data; return false; } }
7. Find the value of the element
int LocateElem(LinkNode *L,ElemType e) { int i=1; LinkNode *p = L->next; while(p!=NULL && p->data!=e) { p = p->next; i++; } if(p == NULL) return(0); else return(i); }
8. The insert data elements
bool ListInsert(LinkNode *&L,int i,ElemType e) { int j=0; LinkNode *p = L,*s; if(i <= 0) return false; while(j<i-1 && p!=NULL) { j++; p = p->next; } if(p == NULL) return false; else { s = (LinkNode *)malloc(sizeof(LinkNode)); s->data = e; s->next = p->next; p->next = s; return true; } }
9. delete data elements
bool ListDelete(LinkNode *&L,int i,ElemType &e) { int j=0; LinkNode *p = L,*q; if(i <= 0) return false; while(j<i-1 && p!=NULL) { j++; p = p->next; } if(p == NULL) return false; else { q = p->next; if(q == NULL) return false; e = q->data; p->next= q->next; free(q); return true; } }
IV. Application example of a single linked list
1. The lead a single linked list node L, the lead is split into two nodes of a single linked list L1 and L2, L1 requires the use of the head node L
void Split (LinkNode * & L, LinkNode * & Ll, LinkNode * & L2 of) { LinkNode * P = L-> Next, * Q, * R1; Ll = L; R1 = Ll; L2 of = (LinkNode *) the malloc ( the sizeof ( LinkNode)); L2 of -> Next = NULL; the while ! (p = NULL) { R1 -> Next = p; // tail-p-inserted interpolation L1, R1 = p; p = p-> Next; Q = p-> Next; P -> the L2-Next => Next; // use the first interpolation node p L2 inserted in the L2-> Next = p; p = Q; } R1 -> Next = NULL; }
2. Design an algorithm to delete a single linked list L maximum values of elements node (assuming such node unique)
void delmaxnode (LinkNode * & L) { LinkNode * = L-P> Next, pre * = L, P = maxp *, * maxpre = pre; the while (! P = NULL) { IF (maxp-> Data <p- > Data) // If find a larger node { maxp = P; // update maxp maxpre = pre; // update maxpre } pre = P; P = p-> Next; } maxpre -> Next = maxp- > the Next; // delete maxp node as Free (maxp); // release maxp node }
3. a single chain lead node L (at least one data node), so that element design an algorithm increments ordered
void sort(LinkNode *&L) { LinkNode *p,*pre,*q; p = L->next->next; L->next->next = NULL; while(p != NULL) { q = p->next; pre = L; while(pre->next!=NULL && pre->next->data < p->data) pre = pre->next; p->next = pre->next; pre->next = p; p = q; }
Doubly linked list
A. Establish doubly linked list
1. Head interpolation:
void CreateListF (DLinkNode & L *, elemType A [], int n-) { DLinkNode * S; int I; L = (DLinkNode *) the malloc ( the sizeof (DLinkNode)); // Create the first node L-> prior = L- > Next = NULL; for (I = 0 ; I <n-; I ++ ) { S = (DLinkNode *) the malloc ( the sizeof (DLinkNode)); S -> Data = A [I]; S -> Next = L-> Next; // after the insertion node s head node if(! L-> next = NULL) // If there is data node L, modified L-> next pointer precursor of L-> next-> Prior = S; L -> next = S; S -> Prior = L; } }
2. Tail interpolation
void CreateListR(DLinkNode *&L,ElemType a[],int n) { DLinkNode *s,*r; int i; L = (DLinkNode *)malloc(sizeof(DLinkNode)); r = L; for(i=0;i<n;i++) { s = (DLinkNode *)malloc(sizeof(DLinkNode)); s->data = a[i]; r->next = s; s->prior = r; r = s; } r->next = NULL; }
II. TABLE substantially linear operation is implemented in a doubly linked list
1. Insert node
bool ListInsert(DLinkNode *&L,int i,ElemType e) { int j=0; DLinkNode *p = L,*s; if(i <= 0) return false; while(j<i-1 && p!=NULL) { j++; p = p->next; } if(p == NULL) return false; else { s = (DLinkNode *)malloc(sizeof(DLinkNode)); s->data = e; s->next = p->next; if(p->next != NULL) p->next->prior = s; s->prior = p; p->next = s; return true; } }
2. Delete the node
bool ListDelete(DLinkNode *&L,int i,ElemType &e) { int j=0; DLinkNode *p=L,*q; if(i <= 0) return false; while(j<i-1 && p!=NULL) { j++; p = p->next; } if(p == NULL) return false; else { q = p->next; if(q == NULL) return false; e = q->data; p->next = q->next; if(p->next != NULL) p->next->prior = p; free(q); return true; } }
III. Application Examples
1 has a lead double-linked list node L, to design an algorithm inverse to set all the elements, i.e., the first element becomes the last element, the second element 2 becomes the penultimate element ...... The last element becomes the first element
void Reverse (DLinkNode * & L) { DLinkNode * = L-P> Next, * Q; L -> Next = NULL; // configured header only doubly-linked list node L the while (P =! NULL) { Q = P -> Next; p -> L-Next => Next; // use of interpolation p-head node is inserted into the doubly linked list IF (! L-> Next = NULL) L -> next-> Prior = p; L -> Next = P; // the new node as the first node p-> Next = L; P = Q; } }
2. a lead doubly linked list node L (at least one data node), so that element design an algorithm increments ordered
void sort(DLinkNode *&L) { DLinkNode *p,*pre,*q; p = L->next->next; L->next->next = NULL; //构造只含一个数据结点的有序表 while(p != NULL) { q = p->next; pre = L; while(pre->next!=NULL && pre->next->data<p->data) pre = pre->next; p->next = pre->next; if(pre->next != NULL) pre->next->prior = p; pre->next = p; p->prior = pre; p = q; } }
Circular list
Single list and the difference consists in its next pointer field tail node from the original point to the first empty node.