Use C to print two-way non-circular linked list (no leading node) in reverse order
In my previous blog "", I constructed a bidirectional non-cyclic linked list and implemented forward printing. In my previous blog "printing the reverse order of a single-chain table in C Language", I also printed the reverse order of a single-chain table. In this blog, We can print two-way non-circular linked lists in reverse order, which is very simple to implement. The Code has been uploaded to https://github.com/chenyufeng1991/reversedoublelinkedlist.
The core code is as follows:
// Print a non-circular two-way linked list. This is actually a forward print void printList (Node * pNode) {if (pNode = NULL) {printf ("% s function execution, the linked list is empty. Print failed \ n ",__ FUNCTION _);} else {while (pNode! = NULL) {printf ("% d", pNode-> element); pNode = pNode-> next;} printf ("\ n ");}}
// Print the void ReversePrintList (Node * pNode) {Node * pMove; pMove = pNode; if (pNode = NULL) {printf ("% s function execution, the two-way non-cyclic linked list is empty and printing in reverse order failed \ n ",__ FUNCTION _);} else {// traverses the last node from the past to the next while (pMove-> next! = NULL) {pMove = pMove-> next;} // traverse from the back to the first node and print the node value while (pMove! = NULL) {printf ("% d", pMove-> element); pMove = pMove-> prior;} printf ("\ n % s function execution, bidirectional non-cyclic linked list printed in reverse order \ n ",__ FUNCTION __);}}
By carefully studying the code, we can find that if you want to reverse a linked list, using a two-way linked list is much easier than using a single-chain table.