This paper is aimed at the basic series of data Structure network course (2): Linear table in the 12th 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: Dlinklist.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 dlinklist_h_included#define Dlinklist_h_includedtypedefintElemtype;typedefstructDnode//define double-linked table node types{Elemtype data;structDnode *prior;//point to the precursor node structDnode *next;//point to subsequent nodes} dlinklist;voidCREATELISTF (dlinklist *&l,elemtype a[],intn);//head interpolation to build double-linked listvoidCreatelistr (dlinklist *&l,elemtype a[],intn);//tail plug method to build double linked listvoidInitlist (Dlinklist *&l);//Initialize doubly linked listvoidDestroylist (Dlinklist *&l);//Destroy doubly linked listBOOLListempty (Dlinklist *l);//Determine if the linked list is emptyintListlength (Dlinklist *l);//Find the length of the listvoidDisplist (Dlinklist *l);//Output link listBOOLGetelem (Dlinklist *l,intI,elemtype &e);//Gets the value of the nodeintLocateelem (Dlinklist *l,elemtype e);//Find a nodeBOOLListinsert (Dlinklist *&l,intI,elemtype e);//insertion of a nodeBOOLListdelete (Dlinklist *&l,intI,elemtype &e);//Delete a node#endif//dlinklist_h_included
2. source file: Linklist.cpp, which contains definitions of functions that implement various algorithms
#include <stdio.h>#include <malloc.h>#include "dlinklist.h"void Createlistf (dlinklist*&L,elemtype a[],intN)//head interpolation double linked list {dlinklist*s;intI L= (dlinklist*)malloc (sizeof (dlinklist));//Create a head node l->prior=l->Next=null; for(i=0; i<n; i++) {s= (dlinklist*)malloc (sizeof (dlinklist));//Create a new nodes->data=a[i];s-Next=l->Next;//Will*sBefore the initial node, after the head node.if(l->Next!=null) l->Next->prior=s; L->Next=s;s->prior=l; }}void Createlistr (dlinklist*&L,elemtype a[],intn)//tail-plug method to build double linked list {dlinklist*s,*r;intI L= (dlinklist*)malloc (sizeof (dlinklist));//Create a head node l->prior=l->Next=null; R=l;//rAlways point to end node, point to head node at start for(i=0; i<n; i++) {s= (dlinklist*)malloc (sizeof (dlinklist));//Create a new nodes->data=a[i]; R->Next=s;s->prior=r;//Will*sInsert*rAfter r=s; } r->Next=null;//Terminal nodeNextThe domain is set to null}void initlist (dlinklist*&L) {l= (dlinklist*)malloc (sizeof (dlinklist));//Create a head node l->prior=l->Next=null;} void Destroylist (dlinklist*&L) {Dlinklist*p=l,*q=p->Next; while(Q!=null) {free (p); p=Q;Q=p->Next; } free (p);} BOOL Listempty (dlinklist*l){return(l->Next==null);}intListlength (dlinklist*l) {Dlinklist*p=l;intI=0; while(p->Next!=null) {i++; P=p->Next; }return(i);} void Displist (dlinklist*l) {Dlinklist*p=l->Next; while(P!=null) {printf("%d ", P->data); P=p->Next; }printf("\ n");} BOOL Getelem (dlinklist*l,intI,elemtype &e) {intj=0; Dlinklist*p=l; while(J<i && P!=null) {j + +; P=p->Next; }if(P==null)returnFalseElse{e=p->data;returnTrue }}intLocateelem (dlinklist*l, Elemtype e) {intn=1; Dlinklist*p=l->Next; while(P!=null && p->data!=e) {n++; P=p->Next; }if(P==null)return(0);Else return(n);} BOOL Listinsert (dlinklist*&LintI,elemtype e) {intj=0; Dlinklist*p=l,*s; while(j<i-1&& p!=null) {j + +; P=p->Next; }if(p==null)//not found1A knot.returnFalseElseFind the first I-1A knot.*p{s= (dlinklist*)malloc (sizeof (dlinklist));//Create a new node*s s->data=e;s-Next=p->Next;//Will*sInsert to*pAfterif(p->Next!=null) p->Next->prior=s;s->prior=p; P->Next=s;returnTrue }}bool Listdelete (dlinklist*&LintI,elemtype &e) {intj=0; Dlinklist*p=l,*q; while(j<i-1&& p!=null) {j + +; P=p->Next; }if(p==null)//not found1A knot.returnFalseElseFind the first I-1A knot.*p{Q=p->Next;//qPoint to the node you want to deleteif(Q==null)returnFalse//There is no first node e=.Q->data; P->Next=Q-Next;//Remove from single linked list*qKnot Pointif(p->Next!=null) p->Next->prior=p; FreeQ);//Release*qKnot PointreturnTrue }}
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.
The test work can be "progressive" thinking, each of the functions involved should be as few as possible.
For example, the following design tests some of the functions:
#include <stdio.h> #include"Dlinklist.h"int main () {Dlinklist *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--double linked list