博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
从尾到头打印链表
阅读量:2720 次
发布时间:2019-05-13

本文共 1518 字,大约阅读时间需要 5 分钟。

#define  _CRT_SECURE_NO_WARNINGS#include
using namespace std;#include
struct ListNode{ int _value; ListNode* _next; ListNode(int value) :_value(value), _next(NULL) {}};class List{ typedef ListNode Node;public: List() :_head(NULL) {} void Insert(const int value) { Node *tmp = new Node(value); if (_head == NULL) { _head = tmp; } else { Node* cur = _head; while (cur->_next) { cur = cur->_next; } cur->_next = tmp; } } void PrintTailToHead() //利用栈实现 { stack
s; Node* cur = _head; while (cur) { s.push(cur); cur = cur->_next; } while (!s.empty()) { Node * tmp = s.top(); cout << tmp->_value << " "; s.pop(); } } void PrintTailToHeadRecur()//利用递归实现 { _PrintTailToHeadRecur(_head); }protected: void _PrintTailToHeadRecur(Node* head) { if (head) { if (head->_next) { _PrintTailToHeadRecur(head->_next); } } cout << head->_value << " "; }private: Node* _head;};void test(){ List L; L.Insert(1); L.Insert(2); L.Insert(3); L.Insert(4); L.Insert(5); L.PrintTailToHead(); cout << endl; L.PrintTailToHeadRecur();}int main(){ test(); system("pause"); return 0;}

转载地址:http://fqstd.baihongyu.com/

你可能感兴趣的文章
再谈typedef(重点为函数指针)
查看>>
C++不同类中的特征标相同的同名函数
查看>>
VS debug下为什么多此一举jmp函数地址?
查看>>
This function or variable may be unsafe
查看>>
如何输出类的非静态成员函数地址
查看>>
C++静态成员函数小结
查看>>
函数调用方式--__thiscall调用方式和__cdecl,__stdcall有什么区别
查看>>
输出成员函数地址小结
查看>>
VS生成垃圾文件清理
查看>>
【Qt学习笔记】Qt+VS2010的配置
查看>>
tinyxml
查看>>
Ansi、Unicode、UTF8字符串之间的转换和写入文本文件
查看>>
std::wstring_convert处理UTF8
查看>>
std::wstring_convert处理UTF8
查看>>
构造UTF8的std::string
查看>>
CInternetSession的简单使用
查看>>
代理IP批量验证程序
查看>>
pugixml简单实用
查看>>
how to convert wstring to string
查看>>
VC实现快递查询
查看>>