Method 1: implement with the stack, step backward from the first node, and then push the node pointer (Address) into the stack in sequence. After all traversal ends, extract the content from the top of the stack and print the data. Since the later Node Address is above the stack, the printing order is printed from the end.
Voidprint (node * List)
{Stack <node *> node; node * P = List; while (P! = NULL) {node. Push (p); P = p-> next;} while (! Node. Empty () {P = node. Top (); cout <p-> data <","; node. Pop ();}}
Method 2: implement with recursionCodeConcise, but when the linked list is long, it will lead to a deep hierarchy of recursive calls, which may cause function call stack overflow. Stack performance is better, and recursion is used with caution.
Void print (node * List) {node * P = List; If (P! = NULL) {If (p-> next! = NULL) {print (p-> next) ;}cout <p-> data <",";}}