Self-built Algorithm Library of Data Structure-single-chain table, self-built single-chain

Source: Internet
Author: User

Self-built Algorithm Library of Data Structure-single-chain table, self-built single-chain

This article focuses on the basic series of network courses on data structures (2): the implementation of basic operations on a single-chain table in a linear table in 10th class hours, and the establishment of an algorithm library for basic operations on a single-chain table data storage structure.

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 single-chain table algorithm library adopts the multi-file organization form of the program, including two files:
  
1. header file: linklist. h, containing the Code, macro definition, and Declaration of functions to implement the algorithm for defining the data structure of the sequence table;

# Ifndef LINKLIST_H_INCLUDED # define LINKLIST_H_INCLUDEDtypedef int ElemType; typedef struct LNode // defines the node type of the single-chain table {ElemType data; struct LNode * next; // points to the next node} LinkList; void CreateListF (LinkList * & L, ElemType a [], int n); // create a single-chain table void CreateListR (LinkList * & L, ElemType a [], int n); // create a single-chain table void InitList (LinkList * & L) by means of the end plug; // initialize the linear table void DestroyList (LinkList * & L ); // destroy the linear table bool ListEmpty (LinkList * L); // determine whether the linear table is null int ListLength (LinkList * L); // obtain the length of the linear table void DispList (LinkList * L ); // output linear table bool GetElem (LinkList * L, int I, ElemType & e); // evaluate the value of a data element in a linear table int LocateElem (LinkList * L, ElemType e ); // search for bool ListInsert (LinkList * & L, int I, ElemType e) by element value; // Insert the data element bool ListDelete (LinkList * & L, int I, elemType & e); // Delete the data element # endif // LINKLIST_H_INCLUDED

2. source file: linklist. cpp, including the definition of functions that implement various algorithms

# Include <stdio. h> # include <malloc. h> # include "linklist. h "void CreateListF (LinkList * & L, ElemType a [], int n) // create a single-chain table using the header Insertion Method {LinkList * s; int I; L = (LinkList *) malloc (sizeof (LinkList); // create the header node L-> next = NULL; for (I = 0; I <n; I ++) {s = (LinkList *) malloc (sizeof (LinkList); // create a new node s-> data = a [I]; s-> next = L-> next; // insert * s before the original Start Node. After the header node, L-> next = s;} void CreateListR (LinkList * & L, ElemType a [], int n) // create a single-chain table {LinkL Ist * s, * r; int I; L = (LinkList *) malloc (sizeof (LinkList); // create the header node L-> next = NULL; r = L; // r always points to the terminal node. At the beginning, it points to the header node for (I = 0; I <n; I ++) {s = (LinkList *) malloc (sizeof (LinkList); // create a new node s-> data = a [I]; r-> next = s; // after inserting * s into * r, r = s;} r-> next = NULL; // set the next field of the terminal node to NULL} void InitList (LinkList * & L) {L = (LinkList *) malloc (sizeof (LinkList); // create a header node L-> next = NULL;} void DestroyList (LinkList * & L) {LinkList * p = L, * q = p-> next; While (q! = NULL) {free (p); p = q; q = p-> next;} free (p); // q is NULL, p points to the end node, release it} bool ListEmpty (LinkList * L) {return (L-> next = NULL);} int ListLength (LinkList * L) {LinkList * p = L; int I = 0; while (p-> next! = NULL) {I ++; p = p-> next;} return (I);} void DispList (LinkList * L) {LinkList * p = L-> next; while (p! = NULL) {printf ("% d", p-> data); p = p-> next;} printf ("\ n ");} bool GetElem (LinkList * L, int I, ElemType & e) {int j = 0; LinkList * p = L; while (j <I & p! = NULL) {j ++; p = p-> next;} if (p = NULL) // No data node I has returned false; else // There is a data node I {e = p-> data; return true ;}} int LocateElem (LinkList * L, ElemType e) {LinkList * p = L-> next; int n = 1; while (p! = NULL & p-> data! = E) {p = p-> next; n ++;} if (p = NULL) return (0); else return (n );} bool ListInsert (LinkList * & L, int I, ElemType e) {int j = 0; LinkList * p = L, * s; while (j <I-1 & p! = NULL) // find the I-1 node {j + +; p = p-> next;} if (p = NULL) // node return false where bid is I-1 is not found; else // locate node * p {s = (LinkList *) malloc (sizeof (LinkList) where bid is I-1 )); // create a new node * s-> data = e; s-> next = p-> next; // insert * s to * p after * p-> next = s; return true ;}} bool ListDelete (LinkList * & L, int I, ElemType & e) {int j = 0; LinkList * p = L, * q; while (j <I-1 & p! = NULL) // find the I-1 node {j + +; p = p-> next;} if (p = NULL) // node return false where bid is I-1 is not found; else // locate node * p {q = p-> next where bid is I-1; // q points to the node to be deleted if (q = NULL) return false; // if no node I exists, false e = q-> data is returned; p-> next = q-> next; // Delete * q node free (q) from the single-link table; // 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, to design a test function, you can initialize a linear table, destroy a linear table, output a linear table, and insert a function corresponding to a data element. The test function can be:

#include "linklist.h"int main(){    LinkList *L;    InitList(L);    ListInsert(L, 1, 15);    ListInsert(L, 1, 10);    ListInsert(L, 1, 5);    ListInsert(L, 1, 20);    DispList(L);    DestroyList(L);    return 0;}

To test the code of the basic operating algorithm, design and implement the test process.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.