This paper is aimed at the basic series of data Structure network course (2): Linear table in the 13th class double linked 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: Clinklist.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 clinklist_h_included#define Clinklist_h_included//Cyclic single-link list basic arithmetic functionstypedefintElemtype;typedefstructLnode//Define single-linked table node types{Elemtype data;structLnode *next;} linklist;voidCREATELISTF (linklist *&l,elemtype a[],intn);//head interpolation to create a circular single-link listvoidCreatelistr (linklist *&l,elemtype a[],intn);//tail interpolation to create a circular single-link listvoidInitlist (linklist *&l);//Initialize linked listvoidDestroylist (linklist *&l);//Destroy linked listBOOLListempty (linklist *l);//Determine if the linked list is emptyintListlength (linklist *l);//Find the list lengthvoidDisplist (linklist *l);//Output link listBOOLGetelem (linklist *l,intI,elemtype &e);//Fetch list elementintLocateelem (linklist *l,elemtype e);//Find elementsBOOLListinsert (linklist *&l,intI,elemtype e);//Insert nodeBOOLListdelete (linklist *&l,intI,elemtype &e);//Delete node#endif//clinklist_h_included
2. source file: Clinklist.cpp, which contains definitions of functions that implement various algorithms
//Cyclic single-link list basic arithmetic functions#include "clinklist.h"voidCREATELISTF (linklist *&l,elemtype a[],intN//head interpolation to create a circular single-link list{linklist *s;intI L= (linklist *)malloc(sizeof(linklist));//Create head nodel->next=null; for(i=0; i<n; i++) {s= (linklist *)malloc(sizeof(linklist));//Create new nodes->data=a[i]; s->next=l->next;//*s inserted before the original start node, after the head nodel->next=s; } 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 node}voidCreatelistr (linklist *&l,elemtype a[],intN//tail interpolation to create a circular single-link list{linklist *s,*r;intI L= (linklist *)malloc(sizeof(linklist));//Create head nodel->next=null; R=l;//r always points to the terminal node, starting with the head node . for(i=0; i<n; i++) {s= (linklist *)malloc(sizeof(linklist));//Create new nodes->data=a[i]; r->next=s;//After inserting the *s into the *rR=s; } r->next=l;//Tail node next field points to head node}voidInitlist (linklist *&l)//Initialize linked list{l= (linklist *)malloc(sizeof(linklist));//Create head nodeL->next=l;}voidDestroylist (linklist *&l)//Destroy linked list{linklist *p=l,*q=p->next; while(q!=l) { Free(p); p=q; q=p->next; } Free(p);}BOOLListempty (linklist *l)//Determine if the linked list is empty{return(l->next==l);}intListlength (linklist *l)//Find the list length{linklist *p=l;intI=0; while(p->next!=l) {i++; p=p->next; }return(i);}voidDisplist (linklist *l)//Output link list{linklist *p=l->next; while(p!=l) {printf("%d", P->data); p=p->next; }printf("\ n");}BOOLGetelem (linklist *l,intI,elemtype &e)//Fetch list element{intj=0; Linklist *p;if(l->next!=l)//When a single 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 a single linked list is empty table return false;}intLocateelem (linklist *l,elemtype e)//Find elements{linklist *p=l->next;intn=1; while(P!=l && p->data!=e) {p=p->next; n++; }if(p==l)return(0);Else return(n);}BOOLListinsert (linklist *&l,intI,elemtype e)//Insert node{intj=0; Linklist *p=l,*s;if(P->next==l | | i==1)//When the original single-linked list is empty table or I==1{s= (linklist *)malloc(sizeof(linklist));//Create new node *ss->data=e; s->next=p->next;//After inserting *s into *pp->next=s;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= (linklist *)malloc(sizeof(linklist));//Create new node *ss->data=e; s->next=p->next;//After inserting *s into *pp->next=s;return true; } }}BOOLListdelete (linklist *&l,intI,elemtype &e)//Delete node{intj=0; Linklist *p=l,*q;if(p->next!=l)//When the original single-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; Free(q);return true; }Else //i not for 1 o'clock{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{q=p->next;//q point to the node to deletee=q->data; p->next=q->next;//Delete the *Q node from the single linked list Free(q);//Release *q node return true; } } }Else return 0;}
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.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Self-built algorithm library of data structure--circular single link list