This paper is aimed at the data structure Basic Series Network course (2): Linear table in the 13th class cycle chain list.
Build your own professional infrastructure algorithm library according to the method suggested in the "0207 Algorithm Change Program" [VIDEO] section.
The double-linked list algorithm library uses the program's multi-file organization, including two files:
1. header file: Cdlinklist.h, contains the code that defines the data structure of the double-linked list, the macro definition, the declaration of the function to implement the algorithm;
#ifndef cdlinklist_h_included#define Cdlinklist_h_included//Cyclic doubly linked list basic arithmetic functiontypedefintElemtype;typedefstructDnode//define double-linked table node types{Elemtype data;structDnode *prior;//point to the precursor node structDnode *next;//point to subsequent nodes} cdlinklist;voidCREATELISTF (cdlinklist *&l,elemtype a[],intn);//head interpolation to build a circular doubly linked listvoidCreatelistr (cdlinklist *&l,elemtype a[],intn);//tail interpolation method to establish circular double-linked listvoidInitlist (Cdlinklist *&l);//Initialize circular doubly linked listvoidDestroylist (Cdlinklist *&l);//DestroyBOOLListempty (Cdlinklist *l);//Determine if it is emptyintListlength (Cdlinklist *l);//Find the list lengthvoidDisplist (Cdlinklist *l);//Output link listBOOLGetelem (Cdlinklist *l,intI,elemtype &e);//Fetch list elementintLocateelem (Cdlinklist *l,elemtype e);//Find elementsBOOLListinsert (Cdlinklist *&l,intI,elemtype e);//Insert nodeBOOLListdelete (Cdlinklist *&l,intI,elemtype &e);//Delete node#endif//cdlinklist_h_included
2. source file: Cdlinklist.cpp, which contains definitions of functions that implement various algorithms
//Cyclic doubly linked list basic arithmetic function#include <stdio.h>#include <malloc.h>#include "cdlinklist.h"voidCREATELISTF (cdlinklist *&l,elemtype a[],intN//head interpolation to build a circular doubly linked list{Cdlinklist *s;intI L= (Cdlinklist *)malloc(sizeof(cdlinklist));//Create head nodel->next=null; for(i=0; i<n; i++) {s= (cdlinklist *)malloc(sizeof(cdlinklist));//Create new nodes->data=a[i]; s->next=l->next;//*s inserted before the original start node, after the head node if(L->next!=null) l->next->prior=s; l->next=s; s->prior=l; } s=l->next; while(S->next!=null)//Find tail node, point it by Ss=s->next; s->next=l;//Tail node next field points to head nodel->prior=s;the prior field of the head node points to the tail node .}voidCreatelistr (cdlinklist *&l,elemtype a[],intN//tail interpolation method to establish circular double-linked list{Cdlinklist *s,*r;intI L= (Cdlinklist *)malloc(sizeof(cdlinklist));//Create head nodel->next=null; R=l;//r always points to the tail node and points to the head node at the beginning for(i=0; i<n; i++) {s= (cdlinklist *)malloc(sizeof(cdlinklist));//Create new nodes->data=a[i]; r->next=s; s->prior=r;//After inserting the *s into the *rR=s; } r->next=l;//Tail node next field points to head nodel->prior=r;the prior field of the head node points to the tail node .}voidInitlist (Cdlinklist *&l)//Initialize circular doubly linked list{l= (Cdlinklist *)malloc(sizeof(cdlinklist));//Create head nodeL->prior=l->next=l;}voidDestroylist (Cdlinklist *&l)//Destroy{Cdlinklist *p=l,*q=p->next; while(q!=l) { Free(p); p=q; q=p->next; } Free(p);}BOOLListempty (Cdlinklist *l)//Determine if it is empty{return(l->next==l);}intListlength (Cdlinklist *l)//Find the list length{Cdlinklist *p=l;intI=0; while(p->next!=l) {i++; p=p->next; }return(i);}voidDisplist (Cdlinklist *l)//Output link list{Cdlinklist *p=l->next; while(p!=l) {printf("%d", P->data); p=p->next; }printf("\ n");}BOOLGetelem (Cdlinklist *l,intI,elemtype &e)//Fetch list element{intj=0; Cdlinklist *p;if(l->next!=l)//When a doubly linked list is not a null table{if(i==1) {e=l->next->data;return true; }Else //i not for 1 o'clock{p=l->next; while(j<i-1&& p!=l) {j + +; p=p->next; }if(p==l)return false;Else{e=p->data;return true; } } }Else //When the double-linked list is empty table return 0;}intLocateelem (Cdlinklist *l,elemtype e)//Find elements{intn=1; Cdlinklist *p=l->next; while(P!=null && p->data!=e) {n++; p=p->next; }if(P==null)return(0);Else return(n);}BOOLListinsert (Cdlinklist *&l,intI,elemtype e)//Insert node{intj=0; Cdlinklist *p=l,*s;if(p->next==l)//When the original double-linked list is empty table{s= (Cdlinklist *)malloc(sizeof(cdlinklist));//Create new node *ss->data=e; p->next=s; s->next=p; p->prior=s; s->prior=p;return true; }Else if(i==1)//Original doubly linked list is not empty table but I=1{s= (Cdlinklist *)malloc(sizeof(cdlinklist));//Create new node *ss->data=e; s->next=p->next; p->next=s;//After inserting *s into *ps->next->prior=s; s->prior=p;return true; }Else{p=l->next; while(j<i-2&& p!=l) {j + +; p=p->next; }if(p==l)//did not find the first i-1 node. return false;Else //Find the first i-1 node *p{s= (Cdlinklist *)malloc(sizeof(cdlinklist));//Create new node *ss->data=e; s->next=p->next;//After inserting *s into *p if(p->next!=null) p->next->prior=s; s->prior=p; p->next=s;return true; } }}BOOLListdelete (Cdlinklist *&l,intI,elemtype &e)//Delete node{intj=0; Cdlinklist *p=l,*q;if(p->next!=l)//When the original double-linked list is not a null table{if(i==1)when//i==1{q=l->next;//Delete 1th nodee=q->data; l->next=q->next; q->next->prior=l; Free(q);return true; }Else //i not for 1 o'clock{p=l->next; while(j<i-2&& p!=null) {j + +; p=p->next; }if(P==null)//did not find the first i-1 node. return false;Else //Find the first i-1 node *p{q=p->next;//q point to the node to delete if(Q==null)return 0;//Does not exist the first nodee=q->data; p->next=q->next;//Delete the *Q node from the single linked list if(p->next!=null) p->next->prior=p; Free(q);//Release *q node return true; } } }Else return false;//When the original double-linked list is empty table}
3. In the process of building the algorithm library, in order to complete the test, build a source file (such as main.cpp) in the same project, and compile the main function to complete the relevant testing work. Cases:
#include <stdio.h> #include"Cdlinklist.h"int main () {Cdlinklist *A;Elemtypea[]= {1,3,2,9,0,4,5,6,7,8};Initlist (A);CREATELISTF (A,a,Ten);printf"Length:%d\n", Listlength (A));Listinsert (A,4, A);printf"after Insert:");Displist (A);Destroylist (A); return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Self-built algorithm library of data structure--cyclic doubly linked list