This blog post includes a single-chain resume table, deleting nodes, and outputting nodes.
Returns the node value of a single-chain table in reverse order.
Single-chain table reversal
Returns the reciprocal K node values of a single-chain table.
Joseph Ring
1. Single-chain table implementation and related basic operations
Struct linknode {int value; linknode * Next;}; void addtotail (linknode ** phead, int value) // insert a node using the plug-in method {linknode * pnew = new linknode (); // open space is used to store the node value pnew-> value = value; pnew-> next = NULL; If (* phead = NULL) {* phead = pnew ;} else {linknode * pnode = * phead; while (pnode-> next! = NULL) // find the final node pnode = pnode-> next; pnode-> next = pnew; // insert node} void printlist (linknode * phead) // output the node value {linknode * pnode = phead; while (pnode) {cout <pnode-> value <"; pnode = pnode-> next ;} cout <Endl;} void removenode (linknode ** phead, int value) // delete a node from the linked list {If (phead = NULL | * phead = NULL) return; linknode * pdeletednode = NULL; If (* phead)-> value = value) // header node {pdeletednode = * phead; * phead = (* phead )-> Next;} else {linknode * pnode = * phead; while (pnode-> next! = NULL & pnode-> next-> value! = Value) // find the previous node pnode that matches the search value = pnode-> next; If (pnode-> next! = NULL & pnode-> next-> value = value) // find {pdeletednode = pnode-> next; pnode-> next = pnode-> next ;} if (pdeletednode! = NULL) // delete node {Delete pdeletednode; pdeletednode = NULL ;}}}
2. output the node values of a single-chain table in reverse order (using stacks)
Void reverse_print (linknode * phead) {stack <linknode *> S; linknode * pnode = phead; while (pnode) {S. push (pnode); pnode = pnode-> next;} while (! S. empty () {pnode = S. top (); cout <pnode-> value <""; S. pop () ;}cout <Endl;} void recur_print (linknode * phead) // This method changes the original structure of the linked list, and the chain table length cannot be too long {If (phead-> next! = NULL) recur_print (phead-> next); cout <phead-> value <"";}
3. Single-chain table reversal
Void reverse_list (linknode ** phead) // reverse the single-chain table {linknode * cur = * phead; // point to the current node linknode * pre = NULL; // refers to the previous node linknode * temp = NULL; // temporary pointer while (cur) // when cur = NULL stop traversing {temp = pre; Pre = cur; cur = cur-> next; pre-> next = temp;} * phead = pre; // point the header pointer to the last node}
4 Joseph's ring problem (implemented using a Circular Single-chain table)
Void Joseph (linknode ** phead, int K) {int CNT = 1; linknode * pnode = * phead; linknode * TMP = NULL, * dnode = NULL; while (pnode-> next! = Pnode) // stop the last node {While (CNT! = K) {CNT ++; TMP = pnode; pnode = pnode-> next;} If (CNT = k) // The K node {dnode = pnode; TMP-> next = pnode-> next; pnode = pnode-> next; cout <dnode-> value <""; Delete dnode; // delete node dnode = NULL; CNT = 1 ;}}cout <pnode-> value <""; // output the last node value cout <Endl ;}
The implementation and basic operations of the cyclic single-chain table are as follows:
Struct linknode {int value; linknode * Next;}; // Insert the node void addtotail (linknode ** phead, int value) to the cyclic single-link table) // Insert the node {linknode * pnew = new linknode () by means of the end plug; // the open space is used to store the node value pnew-> value = value; pnew-> next = NULL; if (* phead = NULL) {* phead = pnew;} else {linknode * pnode = * phead; while (pnode-> next! = * Phead) // find the last node pnode = pnode-> next; pnode-> next = pnew; // insert a node} pnew-> next = * phead ;} // output the node value in the cyclic single-chain table void printlist (linknode * phead) // output the node value {linknode * pnode = phead-> next; cout <phead-> value <"; // print the first node value while (pnode! = Phead) {cout <pnode-> value <""; pnode = pnode-> next ;}cout <Endl ;} // Delete the node value void removenode (linknode ** phead, int value) // Delete the node from the linked list {If (phead = NULL | * phead = NULL) return; linknode * pdeletednode = NULL; If (* phead)-> value = value) // header node {pdeletednode = * phead; * phead = (* phead) -> next;} else {linknode * pnode = * phead; while (pnode-> next! = NULL & pnode-> next-> value! = Value) // find the previous node pnode that matches the search value = pnode-> next; If (pnode-> next! = NULL & pnode-> next-> value = value) // find {pdeletednode = pnode-> next; pnode-> next = pnode-> next ;} if (pdeletednode! = NULL) // delete node {Delete pdeletednode; pdeletednode = NULL ;}}}
6. output the last K nodes in a single-chain table.
Idea: Use pbehind to traverse the K-1 node, and then use pahead to traverse from phead until pbehind traverses to the end of the linked list
Void print_last_k (linknode * phead, int K) {If (phead = NULL | K <= 0) return; // determine the validity of the parameter linknode * pahead = NULL, * pbehind = phead; int I = 0; for (I = 0; I <K-1; I ++) {If (pbehind-> next! = NULL) pbehind = pbehind-> next; elsereturn;} pahead = phead; while (pbehind-> next! = NULL) {pbehind = pbehind-> next; pahead = pahead-> next;} cout <pahead-> value <Endl ;}