单链表逆序输出C++ (stack方法、双向链表方法以及vector方法)
2017-03-01 17:52
399 查看
#include "stdio.h" #include <iostream> #include "malloc.h" #include <stack> #include <vector> struct listNode{ listNode* next; int nodeData; }; struct twoDlistNode { twoDlistNode *next; twoDlistNode * pre; int nodeData; }; int listData[] = {5,52,4,6,2,445,6,1,52,465,1}; void buildList(listNode* head) { int i=1; listNode* tmp,*tmpold; int count = sizeof(listData)/4; head->nodeData = listData[0]; tmpold = head; for(;i<count;i++) { tmp = new listNode(); tmp->nodeData = listData[i]; tmpold->next = tmp; tmpold = tmp; } tmp -> next = NULL; } void twoD_method(listNode *tmp) { twoDlistNode *twoD,*twoDtmp,*twoDtmpold; if(!tmp) return ; twoD = new twoDlistNode(); twoD->pre = NULL; twoD->nodeData = tmp->nodeData; twoD->next = NULL; tmp = tmp->next; twoDtmpold = twoD; while(tmp) { twoDtmp = new twoDlistNode(); twoDtmp->nodeData = tmp->nodeData; twoDtmp-> pre = twoDtmpold; twoDtmpold->next = twoDtmp; twoDtmpold = twoDtmp; tmp = tmp->next; } printf("This is the twoD_method:\n"); while(twoDtmpold) { printf("%d\n",twoDtmpold->nodeData ); twoDtmpold = twoDtmpold->pre; } } void stack_method(listNode *tmp) { listNode *getNode; int getdata; std::stack<listNode*> mystack; while(tmp->next) { mystack.push(tmp); tmp = tmp->next; } mystack.push(tmp); printf("This is the stack_method:\n"); while(!mystack.empty()) { getNode = mystack.top(); getdata = getNode->nodeData; printf("%d\n",getdata); mystack.pop(); } } void vectorMethod(listNode *tmp) { std::vector<int> myvector; if (tmp==NULL) return; listNode* vMtmp = tmp; while(vMtmp) { myvector.push_back(vMtmp->nodeData); vMtmp = vMtmp->next; } printf("This is the vectorMethod\n"); for(std::vector<int>::iterator iter = myvector.end()-1;iter != myvector.begin()-1;iter--) printf("%d\n",*iter ); } int main(void) { listNode* list; list = new listNode(); listNode* tmp; tmp = list; buildList(list); twoD_method(tmp); stack_method(tmp); vectorMethod(tmp); }
相关文章推荐
- C++输入一个字符串,把其中的字符按照逆序输出的两种方法
- 两种方法实现单向链表的创建、遍历、删除、查找、逆序输出(循环法和递归法)
- C++输入一个字符串,把其中的字符按照逆序输出的两种方法
- C++学习总结——顺序存储,链式存储,索引存储,哈希存储的优缺点,以及vector数组,list链表,十字链表,索引结构,hash表的实现
- 链表逆序(递归&非递归)/倒序输出链表值/用一个递增序列构建平衡二叉搜索树/用递归的方法判断数组是不是升(降)序排列
- C++stl 向量,链表,栈,队列(vector, list, stack, queue)
- C++输入一个字符串,把其中的字符按照逆序输出的两种方法
- C/c++语言,求单向链表的逆序_普通方法_header+p+q工作指针
- C++ stack queue vector 中 易混淆的常用方法 浅析
- C++输入一个字符串,把其中的字符按照逆序输出的两种方法
- C++实现英文句子中的单词逆序输出的方法
- C++输入一个字符串,把其中的字符按照逆序输出的两种方法解析
- C/C++用递归的方法求链表逆序
- 基于c++的单链表,双向链表操作以及环
- 栈 (Stack) 的C++实现(链表方法)
- C++学习的方法以及四大名著
- 双向链表的插入,删除,以及链表的快速排序
- 循环队列的C++实现以及假溢出的解决方法
- 单链表的基本操作,正序/逆序建表(尾插法/头插法)、计算单链表长度、输出单链表内容、插入、删除,给出完整代码
- C++ 中vector的使用方法