Self-built Algorithm Library of Data Structure-double-stranded table, self-built algorithm of Data Structure
This article is intended for the basic series of online courses on data structures (2): double-stranded tables for 12th lesson in linear tables.
Follow the suggested methods in [Video] section of "0207 program algorithm change" to build your own professional infrastructure algorithm library.
The algorithm library of the double-stranded Table Algorithm Library adopts the multi-file organization form of the program, including two files:
1. header file: dlinklist. h, which contains the code, macro definition, and Declaration of functions to implement the algorithm for defining the data structure of a double-link table;
# Ifndef DLINKLIST_H_INCLUDED # define DLINKLIST_H_INCLUDEDtypedef int ElemType; typedef struct DNode // define the double-stranded table node type {ElemType data; struct DNode * prior; // point to the front node struct DNode * next; // point to the successor node} DLinkList; void CreateListF (DLinkList * & L, ElemType a [], int n); // create a double linked list void CreateListR (DLinkList * & L, elemType a [], int n); // create a double linked table void InitList (DLinkList * & L) by means of tail insertion; // initialize the double-linked table void DestroyList (DLinkList * & L ); // destroy the double-stranded table bool ListEmpty (DLinkList * L); // judge whether the linked list is empty int ListLength (DLinkList * L ); // calculate the length of the linked list void DispList (DLinkList * L); // output linked list bool GetElem (DLinkList * L, int I, ElemType & e ); // obtain the node value int LocateElem (DLinkList * L, ElemType e); // query a node bool ListInsert (DLinkList * & L, int I, ElemType e ); // insert a node bool ListDelete (DLinkList * & L, int I, ElemType & e); // delete a node # endif // DLINKLIST_H_INCLUDED
2. source file: linklist. cpp, including the definition of functions that implement various algorithms
# Include <stdio. h> # include <malloc. h> # include "dlinklist. h "void CreateListF (DLinkList * & L, ElemType a [], int n) // create a double linked list using the header Insertion Method {DLinkList * s; int I; L = (DLinkList *) malloc (sizeof (DLinkList); // create a header node L-> prior = L-> next = NULL; for (I = 0; I <n; I ++) {s = (DLinkList *) malloc (sizeof (DLinkList); // create a new node s-> data = a [I]; s-> next = L-> next; // insert * s before the original Start node, and then if (L-> next! = NULL) L-> next-> prior = s; L-> next = s; s-> prior = L ;}} void CreateListR (DLinkList * & L, elemType a [], int n) // create a double linked list {DLinkList * s, * r; int I; L = (DLinkList *) malloc (sizeof (DLinkList) by means of tail insertion )); // create a header node L-> prior = L-> next = NULL; r = L; // r always points to the terminal node, starting with the header node for (I = 0; I <n; I ++) {s = (DLinkList *) malloc (sizeof (DLinkList); // create a new node s-> data = a [I]; r-> next = s; s-> prior = r; // insert * s into * r and r = s;} r-> next = NULL; // set the next field of the terminal node to NULL} void Ini TList (DLinkList * & L) {L = (DLinkList *) malloc (sizeof (DLinkList); // create a header 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);} int ListLength (DLinkList * L) {DLinkList * p = L; int I = 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, int I, ElemType & e) {int j = 0; DLinkList * p = L; while (j <I & p! = NULL) {j ++; p = p-> next;} if (p = NULL) return false; else {e = p-> data; return true ;}} int LocateElem (DLinkList * L, ElemType e) {int n = 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 * & L, int I, ElemType e) {int j = 0; DLinkList * p = L, * s; while (j <I-1 & p! = NULL) {j ++; p = p-> next;} if (p = NULL) // No I-1 node found return false; else // locate the I-1 node * p {s = (DLinkList *) malloc (sizeof (DLinkList); // create a new node * s-> data = e; s-> next = p-> next; // insert * s to * p and then if (p-> next! = NULL) p-> next-> prior = s; s-> prior = p; p-> next = s; return true ;}} bool ListDelete (DLinkList * & L, int I, ElemType & e) {int j = 0; DLinkList * p = L, * q; while (j <I-1 & p! = NULL) {j ++; p = p-> next;} if (p = NULL) // No I-1 node found return false; else // find the I-1 node * p {q = p-> next; // q points to the node to be deleted if (q = NULL) return false; // node I e = q-> data; p-> next = q-> next; // Delete * q node if (p-> next! = NULL) p-> next-> prior = p; free (q); // release * q node return true ;}}
3. Create a source file (such as main. cpp) in the same project to complete the test during Algorithm Library Creation, and compile the main function to complete the test.
The idea of "Gradual" can be used for testing. The number of functions involved each time should be as small as possible.
For example, some functions are tested in the following design:
#include <stdio.h>#include "dlinklist.h"int main(){ DLinkList *A; ElemType a[]= {1, 3, 2, 9, 0, 4, 5 ,6, 7, 8}; InitList(A); CreateListF(A, a, 10); printf("length: %d\n", ListLength(A)); ListInsert(A, 4, 12); printf("After Insert: "); DispList(A); DestroyList(A); return 0;}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.