Description:
Enter the head node of a linked list and print 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;
};
Analysis description:
For a linked list, it is easier to print the chain list from the beginning to the end, but it is complicated to print the value of each node from the end to the end. Reverse output is the last output of the first traversal node, and the first output of the last traversal node.
This is a typical "back-to-first-out". The first method that can be thought of is to implement this sequence using stacks. When a node is not passed through, the node is put into a stack. After traversing the entire chain table, output the node values one by one from the top of the stack. The order of the output nodes has been reversed.
Recursion is essentially a stack structure, so it is natural to come up With recursion. To output the linked list in turn, each time we access a node, we first recursively output the node next to it, and then output the node itself, so that the output result of the linked list is reversed. But there is a problem: when the linked list is very long, it will lead to a deep hierarchy of function calls, which may lead to function call stack overflow.
Program example:
1. The code for "printing the linked list from the end to the end" implemented by stack is as follows:
#include <stdio.h>#include <stdlib.h>#include <memory.h>#ifndef ERROR#define ERROR (0)#endif#ifndef OK#define OK(!ERROR)#endif#define STACK_INIT_SIZE 100#define STACKINCREMENT 10typedef int ElemType;typedef struct Node{ElemType data;struct Node *next;}Node, *pNode;typedef int SElemType;typedef struct SqStack{SElemType*base;SElemType*top;intstacksize;}SqStack, *pStack;pStack S;pStack InitStack(pStack S){S = (pStack)malloc(STACK_INIT_SIZE * sizeof(SElemType));if(S == NULL){return ERROR;}S->base = (SElemType *)S;S->top = S->base;S->stacksize = STACK_INIT_SIZE;return S;}pStack Push(pStack S, SElemType e){if((S->top - S->base) >= S->stacksize){S->base = (SElemType *)realloc(S, (S->stacksize + STACKINCREMENT) * sizeof(SElemType));if(S->base == NULL)return ERROR;S->top = S->base + S->stacksize;S->stacksize += STACKINCREMENT;}*S->top++ = e;return S;}SElemType Pop(pStack S){if(S->top == S->base)return 0;return *(--S->top);}pNode CreateList(){ElemType val;pNode pHead = NULL;pNode pCur = NULL;do{scanf("%d", &val);if(val != -1){pNode pNew = (pNode)malloc(sizeof(Node));if(pNew == NULL)exit(EXIT_FAILURE);pNew->data = val;pNew->next = NULL;if(pHead == NULL){pHead = pNew;pCur = pHead;}else{pCur->next = pNew;pCur = pCur->next;}}}while(val != -1);return pHead;}void DestroyList(pNode pHead){if(pHead == NULL)return ;pNode p = NULL;while(pHead != NULL){p = pHead->next;free(pHead);pHead = p;}}void PrintListReverse(pNode pHead){if(pHead == NULL)return;pNode TmpNode = pHead;pStack S = InitStack(S);while(TmpNode != NULL){Push(S, TmpNode->data);TmpNode = TmpNode->next;}while(S->top != S->base){printf("%d", Pop(S));}}intmain(int argc, char **argv){pNode pHead = CreateList();PrintListReverse(pHead);DestroyList(pHead);return 0;}
2. The program code of "printing the linked list from the end to the end" implemented by recursion is as follows:
#include <stdio.h>#include <stdlib.h>typedef int ElemType;typedef struct Node{ElemType data;struct Node *next;}Node, *pNode;void PrintListReverse(pNode pHead){if(pHead == NULL)return;if(pHead->next != NULL)PrintListReverse(pHead->next);printf("%d\n", pHead->data);}pNode CreateList(){ElemType val;pNode pHead = NULL;pNode pCur = NULL;do{scanf("%d", &val);if(val != -1){pNode pNew = (pNode)malloc(sizeof(Node));if(pNew == NULL)exit(EXIT_FAILURE);pNew->data = val;pNew->next = NULL;if(pHead == NULL){pHead = pNew;pCur = pHead;}else{pCur->next = pNew;pCur = pCur->next;}}}while(val != -1);return pHead;}void DestroyList(pNode pHead){if(pHead == NULL)return ;pNode p = NULL;while(pHead != NULL){p = pHead->next;free(pHead);pHead = p;}}intmain(int argc, char **argv){pNode pHead = CreateList();PrintListReverse(pHead);DestroyList(pHead);return 0;}
Summary:
1. for reverse output, we should consider its features and select the data structure type for implementation. Be sure to understand the characteristics of various data structure types.
2. Examples of stack implementation can also be implemented using recursion. The disadvantage of recursion is that when the recursion level is deep, function call Stack Overflow may occur.
Note that the first program above is a little bug, and this article reference: http://blog.csdn.net/ns_code/article/details/25028525