58. output the Linked List (linked list) from the end to the end ).
Question: Enter the head node of a linked list and output the value of each node from the end to the end. The linked list node is defined as follows:
Struct listnode
{
Int m_nkey;
Listnode * m_pnext;
};
My idea: store existing numbers in an array and output them in turn. The disadvantage is that the array size is determined that the length of the linked list cannot exceed the size of the array.
/* 58. output the Linked List (linked list) from the end to the end ). Question: Enter the head node of a linked list and output the value of each node from the end to the end. The linked list node is defined as follows: struct listnode {int m_nkey; listnode * m_pnext;}; */# include <stdio. h> # include <stdlib. h> typedef struct listnode {int m_nkey; listnode * m_pnext;} listnode; void addnode (listnode * & phead, int data) {listnode * X; X = phead; while (X! = NULL & X-> m_pnext! = NULL) {x = x-> m_pnext;} If (x = NULL) {phead = (listnode *) malloc (sizeof (listnode )); phead-> m_nkey = data; phead-> m_pnext = NULL;} else {X-> m_pnext = (listnode *) malloc (sizeof (listnode )); x-> m_pnext-> m_nkey = data; X-> m_pnext = NULL;} void printformback (listnode * phead) {int * store = (int *) malloc (100 * sizeof (INT); // The length of the input linked list is limited to int num = 0; listnode * x = phead; while (X! = NULL) {store [num ++] = x-> m_nkey; X = x-> m_pnext;} For (INT I = num-1; I> = 0; I --) {printf ("% d", store [I]);} printf ("\ n"); free (store) ;}int main () {listnode * A = NULL; addnode (A, 1); addnode (A, 2); addnode (A, 3); addnode (A, 4); addnode (, 5); addnode (A, 6); addnode (A, 7); printformback (a); Return 0 ;}
Looking for answers on the internet is really frustrating!
Address of explanation: http://blog.csdn.net/chentaihan/article/details/6651795
Three methods -- output the linked list from the end to the end
Method 1: generate a linked list in reverse order using the stack
Method 2: first flip the linked list and then output it in sequence
Method 3: Recursive Implementation, a wonderful one, two wonderful words
Method 1: generate a linked list in reverse order using the stack
Because the stack is advanced and later, the elements in the linked list are stored in the stack. The elements in front of the linked list are at the bottom of the stack, and the elements behind the linked list are at the top of the stack.
Method 2: first flip the linked list and then print it in order (mainly to implement the flip of the single-chain table by yourself. This method breaks the structure of the linked list, And of course it will be restored after the flip)
To flip a linked list:
1: point the next node of the current node to the previous node.
2: Move the current node one bit down
3: If it is the last node, point its next node to its previous node and launch a loop.
Method 3: implement with recursion
Honestly, it's really amazing to say that someone else's thoughts have been stolen. You can see whether you really understand the principle of recursion.
As said by the buddy, recursion is a process of stack-to-stack. The elements in front of the linked list are advanced stacks. At the bottom of the stack, the elements in the back are pushed to the stack. At the top of the stack, the first is the stack, haha...
// Three implementation methods: output the linked list from the end to the end # include <stack> using namespace STD; Class outfromend {public: typedef struct node1 {int data; node1 * next; node1 (int d): Data (D), next (null) {}} node; outfromend () {head = cur = new node (-1 );} void add (INT data) {node * TMP = new node (data); cur-> next = TMP; cur = TMP ;} // output the linked list in reverse order by using the stack. // because the stack is advanced and later, the elements in the linked list are stored in the stack. The elements in front of the linked list are at the bottom of the stack, and the elements in the back are at the top of the stack, element behind the linked list first-out stack void stackmethod () {If (null = head | null = head-> next) {Return;} node * TMP = head-> next; stack <int> S; while (TMP! = NULL) {S. Push (TMP-> data); TMP = TMP-> next;} while (! S. empty () {cout <S. top () <"\ t"; S. pop () ;}}/* First flip the linked list and then print it in order (mainly to implement the flip of a single-chain table by yourself. This method breaks the structure of the linked list, of course, it will be restored after a flip.) Step 1: point the next node of the current node to the previous Node 2: Move the current node down to a third position: if it is the last node, point its next node to its previous one, and launch a loop */void reverse () {If (null = head | null = head-> next) {return;} cur = head-> next; node * Prev = NULL; node * pcur = head-> next; node * Next; while (pcur! = NULL) {If (pcur-> next = NULL) {pcur-> next = Prev; break;} next = pcur-> next; pcur-> next = Prev; prev = pcur; pcur = next;} head-> next = pcur; node * TMP = head-> next; while (TMP! = NULL) {cout <TMP-> data <"\ t"; TMP = TMP-> next;} void print3 () {recursion (Head-> next);} // implement it recursively. // honestly, it's amazing to say that someone else's thoughts are stolen, can you tell whether you really understand the principle of recursion? // as the buddy said, recursion is a process of going into and out of the stack. The elements in front of the linked list are advanced stacks at the bottom of the stack, the following elements are pushed to the stack. At the top of the stack, the elements are pushed to the stack first. Haha... Void recursion (node * head) {If (null = head) {return;} If (Head-> next! = NULL) {recursion (Head-> next) ;}// if this sentence is placed before the second if, it is the output chain table from start to end, you may have used the while or for loop output chain table. Now you have another cout