剑指Offer面试题5:从尾到头打印链表
来源:互联网 发布:淘宝原图制作软件 编辑:程序博客网 时间:2024/06/08 11:19
打印链表时,我们不希望修改链表的结构,并且希望从尾到头打印其中的内容,这时很容易想到栈的结构。因为指针从头到尾依次遍历,把内容读到栈里,先进的内容会后出,符合从尾到头打印的顺序,而且没有改变栈的结构。实现如下,先随机生成一个有10个结点的链表,正向打印,最后调用PrintListReversingly_Iteratively()函数,反向打印。
#include <iostream>#include <stdlib.h>#include <stack>#include <time.h>#include <assert.h>using namespace std;typedef struct linkList{ int m_nValue; struct linkList *next;}listNode,*linklistp;linklistp insert_tail(linklistp head,linklistp newnode){ assert(newnode); if(head == NULL) { head = newnode; } else { linklistp temp = head; while(temp->next != NULL) { temp = temp->next; } newnode->next = NULL; temp->next = newnode; } return head;}void output(linklistp head){ linklistp temp = head; while(temp) { cout<<temp->m_nValue<<" "; temp = temp->next; } cout<<endl;}void PrintListReversingly_Iteratively(listNode* pHead){ stack<listNode*> nodes; listNode* pNode = pHead; while(pNode != NULL) { nodes.push(pNode); pNode = pNode->next; } while(!nodes.empty()) { pNode = nodes.top(); cout<<pNode->m_nValue<<" "; nodes.pop(); }}int main(){ listNode* head; head = NULL; srand(time(NULL)); for(int i =0; i< 10; i++){ linklistp newnode = (linklistp)malloc(sizeof(linklistp)); newnode->m_nValue = rand()%100; newnode->next = NULL; head = insert_tail(head,newnode); } output(head); PrintListReversingly_Iteratively(head);}
0 0
- 《剑指Offer》面试题:从尾到头打印链表
- 剑指offer面试题 从尾到头打印链表
- 剑指offer-面试题5-从尾到头打印列表
- 面试题6:从尾到头打印链表(offer)
- 剑指offer面试题5——链表之从尾到头打印链表
- 剑指offer-->面试题5 从尾到头打印链表
- [剑指offer][面试题5]从尾到头打印链表
- 【剑指offer】面试题5:从尾到头打印链表
- 剑指offer 面试题5 从尾到头打印链表(栈实现)
- 剑指offer 面试题5 从尾到头打印链表(递归实现)
- 【剑指offer】面试题5:从尾到头打印链表 java
- 剑指Offer: 面试题5 从尾到头打印链表
- 《剑指Offer》面试题5:从尾到头打印链表
- 剑指offer--面试题5:从尾到头打印链表--Java实现
- 【剑指Offer学习】【面试题5 : 从尾到头打印链表】
- 《剑指Offer》学习笔记--面试题5:从尾到头打印链表
- 【剑指offer】 面试题5: 从尾到头打印链表
- 剑指offer《面试题5:从尾到头打印链表》
- MyEclipse8.5 jad 反编译工具 插件安装
- MVC框架
- js 点击某个区域跳转 即某个<div区域
- Android微信智能心跳方案
- PHP开发者应了解的24个库
- 剑指Offer面试题5:从尾到头打印链表
- PHP中空字符串介绍0、null、empty和false之间的关系
- 【伯乐在线】这些 Git 技能够你用一年了
- Android ViewPager的嵌套使用
- android:baselineAligned="false"
- SVN服务器搭建和使用(二)
- 用VS2013在win7下编写的C++程序怎么在XP下运行?
- New delete 和 malloc free的区别
- 问题的种类以及解决