# Include <iostream> # Include <stdlib. h> # Include <time. h> Using namespace STD;Typedef int elemtype; Typedef struct Node { Elemtype data; Struct node * next; } Node; Typedef node * nodeptr; Bool initlist (nodeptr * NPP) { * NPP = (nodeptr) malloc (sizeof (node);/* generate a header node and point * NPP to this header node */ If (! (* NPP )) Return false; // allocation failed
(* NPP)-> next = NULL;/* the pointer field is empty */ Return true; } Bool listempty (nodeptr np) { If (NP-> next = NULL) Return true; Else Return false; } Bool clearlist (nodeptr * NPP) { Cout <"clear list..." <Endl; Nodeptr P = * NPP; If (! (P-> next )) Return true; While (p-> next)/* not at the end of the table */ { Nodeptr q = p-> next; P-> next = Q-> next; Free (Q ); } Return true; } Int listlength (nodeptr np) { Cout <"list's length :"; Nodeptr P = NP-> next;/* P points to the first node */ Int I = 0; While (P) { P = p-> next; ++ I; } Return I; } /* Operation result: Use PTR to return the value of the POS data element in NP */ Bool getelem (nodeptr NP, int POs, elemtype * PTR) { Cout <"get item from POS" <POS <":"; Nodeptr P = NP-> next; Int I = 1; /* When P is not empty or the counter I is not equal to POS, the loop continues */ While (P & I <POS) { P = p-> next; ++ I; } If (! P) Return false; * PTR = p-> data;/* obtain the data of the POs element */ Return true; } /* Return the order of the 1st data elements in the NP that match the ELEM. */ /* If such a data element does not exist, the returned value is 0 */ Int locateelem (nodeptr NP, elemtype ELEM) { Cout <"item" <ELEM <"'s pos :"; Nodeptr P = NP-> next; Int I = 1; While (P & P-> data! = ELEM) { P = p-> next; ++ I; } If (! P) Return 0; Return I; } /* Operation result: a new data element ELEM is inserted before the POs position in NP. The length of NP is increased by 1 */ Bool listinsert (nodeptr * NPP, int POs, elemtype ELEM) { Cout <"Insert list POS" <POS <"item" <ELEM <Endl; Nodeptr P = * NPP; Int I = 1; While (P & I <POS) { P = p-> next; ++ I; } If (! P) Return false; Nodeptr in = (nodeptr) malloc (sizeof (node )); In-> DATA = ELEM; In-> next = p-> next;/* assign the successor node of P to the successor node of in */ P-> next = In;/* assign the in value to the successor of p */ Return true; } /* Delete the POS data element of NPP and return its value with PTR. The length of NPP is reduced by 1 */ Bool listdelete (nodeptr * NPP, int POs, elemtype * PTR) { Cout <"delete list item in POS" <POS <Endl; Nodeptr P = * NPP; Int I = 1; While (P & I <POS) { P = p-> next; ++ I; } If (! P) Return false; Nodeptr q = p-> next; * PTR = Q-> data; P-> next = Q-> next;/* assign the Q successor to p */ Free (Q ); Return true; } Bool listtraverse (nodeptr np) { Cout <"list's items :"; Nodeptr P = NP-> next; While (P) { Cout <p-> data <''; P = p-> next; } Cout <Endl; Return true; } /* Randomly generate N element values and create a single-chain linear table NPP (Head Insertion Method) with table header nodes )*/ Void createlisthead (nodeptr * NPP, int num) { Cout <"create list from head..." <Endl; If (* NPP! = NULL) Free (* NPP ); * NPP = (nodeptr) malloc (sizeof (node )); (* NPP)-> next = NULL;/* create a single-chain table with the leading node first */ Srand (Time (null )); For (INT I = 0; I <num; I ++) { Nodeptr P = (nodeptr) malloc (sizeof (node )); P-> DATA = rand () % 100 + 1; // Random Number P-> next = (* NPP)-> next; (* NPP)-> next = P;/* insert to the header */ } } /* Randomly generate N element values, and create a single-chain linear table NPP (tail Insertion Method) with table header nodes )*/ Void createlisttail (nodeptr * NPP, int num) { Cout <"create list from tail..." <Endl; If (* NPP! = NULL) Free (* NPP ); * NPP = (nodeptr) malloc (sizeof (node )); (* NPP)-> next = NULL; Srand (Time (null )); Nodeptr tail = * NPP;/* tail is the node pointing to the end */ For (INT I = 0; I <num; I ++) { Nodeptr P = (nodeptr) malloc (sizeof (node )); P-> DATA = rand () % 100 + 1; Tail-> next = P;/* point the pointer to the End Node of the table to the new node */ Tail = P;/* define the current new node as the end node of the table */ } Tail-> next = NULL; } /* Reverse a single-chain table */ Nodeptr reverselist (nodeptr head) { Cout <"reverse list..." <Endl; If (null = head-> next | null = head-> next) Return head; Nodeptr P; Nodeptr Q; Nodeptr R; P = head-> next; Q = p-> next; Head-> next = NULL; While (q) { R = Q-> next ;// Q-> next = P; P = Q ;// Q = r ;// } Head-> next = P; Return head; } Int main (void) { Nodeptr NP; // header pointer Initlist (& NP ); For (INT I = 1; I <5; I ++) Listinsert (& NP, I, I ); If (! Listempty (NP )) Cout <listlength (NP) <Endl; Listtraverse (NP ); Cout <locateelem (NP, 3) <Endl; Int get; Getelem (NP, 2, & get ); Cout <get <Endl; Clearlist (& NP ); Cout <listlength (NP) <Endl; Createlisthead (& NP, 10 ); Listtraverse (NP ); Int result; Listdelete (& NP, 5, & result ); Listtraverse (NP ); Clearlist (& NP ); Createlisttail (& NP, 10 ); Listtraverse (NP ); Listtraverse (reverselist (NP )); Clearlist (& NP ); Return 0; } |