Print a single-chain table in reverse order using C language (Leading node)
In my previous blog "printing a single linked list (with no leading node) in reverse order in C Language", I used to print a list with no leading node in reverse order, the overall idea is also very simple, that is, to traverse the original linked list in sequence, and then use the header insertion method to create a new linked list. The new linked list is the reverse order of the original linked list. In this blog, I will use the linked list of the leading node to implement reverse order. The idea is the same as above.
The core code is as follows:
/*** The overall idea is to retrieve a node and use the header insertion method to create a new linked list. The new linked list is created in reverse order. */Node * ReverseList (Node * pNode) {Node * reverseList; InitialList (& reverseList); // initialize the Node * pMove; Node * pInsert; pInsert = (Node *) malloc (sizeof (Node); memset (pInsert, 0, sizeof (Node); pInsert-> next = NULL; pMove = pNode-> next; while (pMove! = NULL) {// pInsert-> element = pMove-> element; pInsert-> next = reverseList-> next; reverseList-> next = pInsert; // Insert the Node and re-allocate the memory pInsert = (Node *) malloc (sizeof (Node); memset (pInsert, 0, sizeof (Node); pInsert-> next = NULL; pMove = pMove-> next;} printf ("% s FUNCTION execution. The single-chain table of the leading node is successfully created \ n" ,__ FUNCTION _); PrintList (reverseList ); return reverseList ;}