Reverse (reverse) of a single-chain table)
The reverse order of a single-chain table is not part of the algorithm. What can we do with our friends? the chance of an interview is high, so we put it together with the algorithm. About the basic knowledge of Single-linked list reverse order, please participate: http://blog.csdn.net/autumn20080101/article/details/7607148 when you read the part of the above blog, you can basic understanding of the time, the following content should be suitable for you. The following is a summary of key knowledge points in reverse order of a single-chain table. Bloggers think that the most important thing about a single-chain table is that the current node has a pointer to the next node (that is, the next node pointer ), A two-way linked list shows that the current node has a precursor node and a successor node. When a single-chain table is passed through a while loop, it starts from the first node until the node's successor node is empty, it reaches the end of the chain table. In fact, the reverse order of a single-chain table depends on the following key code: 1 while (head! = NULL) {2 // obtain the pointer 3 next = head-> Next of the next node before the header node changes; 4 // The Next node of the first node should be changed to its previous node, which is a reversal process. 5 head-> Next = prev; 6 // The previous node points forward to the first node 7 prev = head; 8 // The first node points forward to the next node 9 head = next; 10} the development environment is: qt, C code: complete code: 1 # include <QCoreApplication> 2 3 // defines the constant value 4 # define LEN 3 5 const int MAX_NUM = 5; 6 7 // define node 8 // typedef struct tagNode * linkNode; 9 typedef struct tagNode {10 int key; 11 char value [LEN]; 12 struct TagNode * Next; 13} * Node, * linkList; 14 15 // generate the linked list 16 linkList generateLinkList (); 17 // release the linked list 18 void freeLinkList (linkList list ); 19 // reverse linked list 20 Node ReverseLinkList (Node head); 21 // print linked list 22 void printLinkList (linkList list); 23 24 int main (int argc, char * argv []) 25 {26 QCoreApplication a (argc, argv); 27 // generate the linked list 28 Node head = generateLinkList (); 29 printf ("reverse the previous linked list \ r \ n "); 30 // print linked list 31 printLinkList (Head); 32 // reverse 33 head = ReverseLinkList (head); 34 printf ("reverse linked list \ r \ n "); 35 // print the reverse linked list 36 printLinkList (head); 37 // release the linked list memory 38 freeLinkList (head); 39 return a.exe c (); 40} 41/** 42 * @ function name: generateLinkList 43 * @ parameter: None 44 * @ return value: linked list object pointer (first node) 45 * @ purpose: generate a linked list object 46 * @ Author: yangfy 47 */48 linkList generateLinkList () 49 {50 Node head, prev; 51 // header Node 52 head = (Node) malloc (sizeof (Node); 53 head-> key = 1; 54 snprintf (head-> value, LEN-1, "% c", 65); 55 // prev initially points to the header node 56 prev = head; 57 // initialize the linked list element 58 for (int I = 1; I <MAX_NUM; I ++) {59 Node pNode = (Node) malloc (sizeof (Node )); 60 // form linked list data 61 pNode-> key = I + 1; 62 pNode-> Next = NULL; 63 snprintf (pNode-> value, LEN-1, "% c", 65 + I); 64 // The Next node of the previous node points to the current node (pNode) 65 prev-> Next = pNode; 66 // set the previous node to the current node and enter the next loop 67 prev = pNode; 68} 69 return head; 70} 71/** 72 * @ function name: freeLinkList 73 * @ parameter: head node pointer 74 * @ return value: None 75 * @ purpose: release of linked list pointer memory resources. 76 * @ Author: yangfy 77 */78 void freeLinkList (Node head) 79 {80 while (head! = NULL) {81 Node next = head-> Next; 82 free (head); 83 head = next; 84} 85} 86 87/** 88 * @ Function Name: reverseLinkList 89 * @ parameter: head node pointer 90 * @ return value: Invert back head node pointer 91 * @ purpose: reverse of linked list 92 * @ Author: yangfy 93 */94 Node ReverseLinkList (Node head) 95 {96 Node prev = NULL; 97 Node next = NULL; 98 while (head! = NULL) {99 // obtain the pointer of the next node 100 Next = head-> next before the header node changes; 101 // The Next node of the first node should be changed to its previous node, which is a reversal process 102 head-> Next = prev; 103 // The previous node points forward to the first node 104 prev = head; 105 // The first node points forward to the next node 106 head = next; 107} 108 return prev; 109} 110 111 112 void printLinkList (linkList list) 113 {114 Node node = list; 115 while (node! = NULL) {116 if (node-> Next! = NULL) 117 printf ("the current key is: % d, value: % s; next key is: % d, value: % s \ r \ n ", node-> key, node-> value, node-> Next-> key, node-> Next-> value); 118 else119 printf ("the current key is: % d, value: % s; Next key: NULL; value: NULL \ r \ n ", node-> key, node-> value); 120 node = node-> Next; 121} 122}