题目:输入一个链表的头结点,从尾到头反过来打印出每个结点的值。
思路:有两种方法。一种是借助栈的后进先出特性来实现;另一种是采用递归,递归的本质也是一个栈结构。
测试用例:
1.输入链表只有一个结点、多个节点
2.输入链表头指针为NULL
代码:
栈:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<stack>
using namespace std;
struct ListNode
{
int m_nValue;
ListNode* m_pNext;
};
//创建链表结点
ListNode* CreateListNode(int value)
{
ListNode* pNode = new ListNode();
pNode->m_nValue = value;
pNode->m_pNext = NULL;
return pNode;
}
//链接链表结点
void ConnectListNodes(ListNode* pCurrent, ListNode* pNext)
{
if (pCurrent == NULL)
{
cout << "链接出错" << endl;
exit(1);
}
pCurrent->m_pNext = pNext;
}
//打印链表
void PrintListNode(ListNode* pHead)
{
cout << "打印链表:" << endl;
ListNode* pNode = pHead;
while (pNode != NULL)
{
cout << pNode->m_nValue << " ";
pNode = pNode->m_pNext;
}
cout << endl;
}
//销毁链表
void DestoryList(ListNode* pHead)
{
ListNode* pNode = pHead;
while (pNode != NULL)
{
pHead = pHead->m_pNext;
delete pNode;
pNode = pHead;
}
}
void PrintListReversingly_Iteratively(ListNode* pHead)
{
stack<ListNode*> nodes; //创建栈
ListNode* pNode = pHead;
while (pNode != NULL) //把所有链表结点打入栈
{
nodes.push(pNode);
pNode = pNode->m_pNext;
}
while (!nodes.empty()) //从栈中弹出
{
pNode = nodes.top();
cout << pNode->m_nValue << " " ;
nodes.pop();
}
cout << endl;
}
void test(ListNode* pHead)
{
PrintListNode(pHead);
PrintListReversingly_Iteratively(pHead);
}
void test1()
{
ListNode* pNode1 = CreateListNode(1);
ListNode* pNode2 = CreateListNode(2);
ListNode* pNode3 = CreateListNode(3);
ListNode* pNode4 = CreateListNode(4);
ListNode* pNode5 = CreateListNode(5);
ConnectListNodes(pNode1, pNode2);
ConnectListNodes(pNode2, pNode3);
ConnectListNodes(pNode3, pNode4);
ConnectListNodes(pNode4, pNode5);
test(pNode1);
DestoryList(pNode1);
}
void test2()
{
ListNode* pNode1 = CreateListNode(1);
test(pNode1);
DestoryList(pNode1);
}
void test3()
{
test(NULL);
}
int main()
{
test1();
test2();
test3();
return 0;
}
递归:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<stack>
using namespace std;
struct ListNode
{
int m_nValue;
ListNode* m_pNext;
};
//创建链表结点
ListNode* CreateListNode(int value)
{
ListNode* pNode = new ListNode();
pNode->m_nValue = value;
pNode->m_pNext = NULL;
return pNode;
}
//链接链表结点
void ConnectListNodes(ListNode* pCurrent, ListNode* pNext)
{
if (pCurrent == NULL)
{
cout << "链接出错" << endl;
exit(1);
}
pCurrent->m_pNext = pNext;
}
//打印链表
void PrintListNode(ListNode* pHead)
{
cout << "打印链表:" << endl;
ListNode* pNode = pHead;
while (pNode != NULL)
{
cout << pNode->m_nValue << " ";
pNode = pNode->m_pNext;
}
cout << endl;
}
//销毁链表
void DestoryList(ListNode* pHead)
{
ListNode* pNode = pHead;
while (pNode != NULL)
{
pHead = pHead->m_pNext;
delete pNode;
pNode = pHead;
}
}
void PrintList_Recursion(ListNode* pHead)
{
if (pHead != NULL)
{
if (pHead->m_pNext != NULL)
{
PrintList_Recursion(pHead->m_pNext); //遍历打印链表
}
cout << pHead->m_nValue << " ";
}
}
void test(ListNode* pHead)
{
PrintListNode(pHead);
PrintList_Recursion(pHead);
cout << endl;
}
void test1()
{
ListNode* pNode1 = CreateListNode(1);
ListNode* pNode2 = CreateListNode(2);
ListNode* pNode3 = CreateListNode(3);
ListNode* pNode4 = CreateListNode(4);
ListNode* pNode5 = CreateListNode(5);
ConnectListNodes(pNode1, pNode2);
ConnectListNodes(pNode2, pNode3);
ConnectListNodes(pNode3, pNode4);
ConnectListNodes(pNode4, pNode5);
test(pNode1);
DestoryList(pNode1);
}
void test2()
{
ListNode* pNode1 = CreateListNode(1);
test(pNode1);
DestoryList(pNode1);
}
void test3()
{
test(NULL);
}
int main()
{
test1();
test2();
test3();
return 0;
}
递归的代码虽然简洁,但是当链表非常长的时候,就会导致函数调用的层级很深,从而有可能导致函数调用栈溢出。显式用栈基于循环实现的代码的鲁棒性要好一些。