Data structure routine-Example of a single-chain table application, single-chain routine
This article provides an example of a single-chain table application for the basic series of online courses on data structures (2): 11th lesson in a linear table.
Example: Split a single-chain table (linklist. h is the header file in the single-chain table "algorithm library". Click the link for details ...)
# Include <stdio. h> # include <malloc. h> # include "linklist. h "void split (LinkList * & L, LinkList * & L1, LinkList * & L2) {LinkList * p = L-> next, * q, * r1; // p points to 1st data nodes L1 = L; // L1 uses the original L header node r1 = L1; // r1 always points to the L1 End Node L2 = (LinkList *) malloc (sizeof (LinkList); // create the L2 header node L2-> next = NULL; // set the L2 pointer field to NULL while (p! = NULL) {r1-> next = p; // insert * p (data value: ai) into L1 using the tail plug method r1 = p; p = p-> next; // p moves to the next node (the data value is bi) q = p-> next; // the next field of p is modified by the header insertion method, therefore, use q to save * p's successor node p-> next = L2-> next; // use the header Insertion Method to insert * p into L2, L2-> next = p; p = q; // p points to the node of ai + 1 again} r1-> next = NULL; // The node at the end is left blank.} int main () {LinkList * L, * L1, * L2; int I; ElemType a [] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; InitList (L); InitList (L1 ); initList (L2); for (I = 9; I> = 0; I --) ListInsert (L, 1, a [I]); printf ("L :"); dispList (L); printf ("L-> L1, L2 \ n"); split (L, L1, L2); printf ("L1 :"); dispList (L1); printf ("L2:"); DispList (L2); DestroyList (L1); DestroyList (L2); return 0 ;}
For example, delete the node with the largest element (linklist. h is the header file in the single-chain table "algorithm library". Click the link for details ...)
# Include <stdio. h> # include <malloc. h> # include "linklist. h "void delmaxnode (LinkList * & L) {LinkList * p = L-> next, * pre = L, * maxp = p, * maxpre = pre; while (p! = NULL) // use p to scan the entire single-chain table, And pre always points to its precursor node {if (maxp-> data <p-> data) // if a larger node is found {maxp = p; // change maxp maxpre = pre; // change maxpre} pre = p; // p. After pre synchronization, move a node p = p-> next;} maxpre-> next = maxp-> next; // Delete * maxp node free (maxp ); // release * maxp node} int main () {LinkList * L; int I; ElemType a [] = }; initList (L); for (I = 9; I> = 0; I --) ListInsert (L, 1, a [I]); printf ("L :"); dispList (L); printf ("delete Max node \ n"); delmaxnode (L); printf ("L:"); DispList (L); DestroyList (L ); return 0 ;}
For example, sort nodes in ascending order (linklist. h is the header file in the single-chain table "algorithm library". Click the link for details ...)
# Include <stdio. h> # include <malloc. h> # include "linklist. h "void sort (LinkList * & L) {LinkList * p, * pre, * q; p = L-> next; // p points to the 2nd data nodes of L-> next = NULL; // construct an ordered table containing only one data node while (p! = NULL) {q = p-> next; // q stores * p node successor node pointer pre = L; // compare from the beginning of the ordered table, pre points to the precursor node inserted * p while (pre-> next! = NULL & pre-> next-> data <p-> data) pre = pre-> next; // In the ordered table, locate the parent node * pre p-> next = pre-> next where * p is inserted; // insert * p pre-> next = p after * pre; p = q; // scan the remaining nodes of the original single linked list} int main () {LinkList * L; int I; ElemType a [] =, 5, 8}; InitList (L); for (I = 9; I> = 0; I --) ListInsert (L, 1, a [I]); printf ("L: "); DispList (L); printf (" sort \ n "); sort (L); printf (" L: "); DispList (L); DestroyList (L ); return 0 ;}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.