stack链式C++
来源:互联网 发布:乱世佳人电影结局知乎 编辑:程序博客网 时间:2024/06/14 01:55
#include <iostream>using namespace std;typedef struct _node{ int data; struct _node* next; _node(int e ,struct _node* p = nullptr){ data = e; next = p; }}node;class stack{public: stack():m_start(new node(-1,nullptr)){} ~stack(){ while(m_start->next != nullptr){ node* pdel = m_start; m_start = m_start->next; delete pdel; } m_start = nullptr; } void push(int e); void pop(); int top(); bool isEmpty(); private: node* m_start;};//头插法void stack::push(int e){ node* pnew = new node(e,nullptr); pnew->next = m_start->next; m_start->next = pnew;}void stack::pop(){ if(m_start->next != nullptr){ node* pdel = m_start->next; m_start->next = m_start->next->next; delete pdel; }else{ cout << "stack is empty.\n"; }}int stack::top(){ if(m_start->next != nullptr){ return m_start->next->data; }else{ cout << "stack is empty.\n"; exit(1); }}bool stack::isEmpty(){ return m_start->next == nullptr;}int main(){ stack s; s.push(1); s.push(2); s.push(3); while(!s.isEmpty()){ cout << s.top() << ends; s.pop(); } cout << endl; s.pop(); return 0;}
阅读全文
0 0
- Stack-c单向链式结构
- 链式stack
- 链式栈(Linked Stack)
- stack链式C++
- Stack(2)Stack的链式实现
- 通用的链式stack实现
- c++:stack
- Algorithms: 栈(Stack) 链式与数组实现
- C++数据结构之Linked Stack(链式栈)
- Stack栈 链式存储 实现
- queue-c链式队列
- 链式队列(数据结构C#)
- C实现链式队列
- C实现链式栈
- c++ 链式栈
- C/C++ 链式栈
- 链式栈(c)
- Objective C 链式调用
- [题解]hdu5634 Rikka with Phi
- 【经验随笔】Restlet Client发送XML格式参数消息
- i2c 相关知识总结
- CentOS6.4 配置iptables
- 端口被占用问题
- stack链式C++
- 【HTML5学习笔记】20:CSS文本样式 下
- javascript面向对象
- RepositionBars的用法和参数分析
- C/C++二维数组名和二级指针
- i2c中start和restart的区别
- 查询schema下面所有表
- Spring Cloud微服务交互设计总结
- 旅游类App的原型制作分享