C++ 数据结构-栈
来源:互联网 发布:人类学 知乎 编辑:程序博客网 时间:2024/06/03 14:09
栈(stack)是一种先进后出的基本数据结构
一、实现
(一)手写
需要一个栈顶指针(指向空位置),数组作栈
(在结构体内写更好)
#include <iostream>using namespace std;#define EMPTY -9999999int stack[1001];int top = 1;int n, tmp;void push(const int x) { stack[top ++] = x;}bool empty() {if(top == 1) return true;return false;}int pop() { if(empty()) return EMPTY;return stack[-- top];}int Func_top() { return stack[top-1]; }int main() {cin >> n;for(int i=0; i<n; i++) {cin >> tmp;push(tmp);}while(!empty()) {cout << Func_top() << " ";pop();}return 0;}
(二)C++ STL
stack<int> a;
常用操作:
a. push(x) 将x入栈
a. top() 访问栈顶元素
a. pop() 弹出(不返回)栈顶元素
a. empty() 判断栈是否为空
a.size() 返回栈中元素个数
#include <iostream>#include <stack>using namespace std;stack<int> a;int n, tmp;int main() {cin >> n;for(int i=0; i<n; i++) {cin >> tmp;a.push(tmp);}while(!a.empty()) {cout << a.top() << " ";a.pop();}return 0;}
二、应用
阅读全文
0 0
- C数据结构 栈
- <C/C++数据结构> 栈
- 数据结构-栈/c++/迷宫
- 数据结构---栈(C++)
- 《数据结构(C语言版)》- 栈
- C数据结构-顺序栈
- 【数据结构】【C】顺序栈
- c语言数据结构之栈
- 数据结构C语言实现栈
- 数据结构 链式 栈 c实现
- 数据结构.栈(C语言实现)
- C 基础数据结构---栈 ADT
- 数据结构-------C栈和队列
- 数据结构--链式栈(C语言)
- C语言数据结构-顺序栈
- 数据结构_3:栈:C语言
- 数据结构(c)——栈
- 数据结构---栈C语言实现
- Activity与Context的继承关系
- Simple LSTM
- 内存泄漏memory leak和内存溢出OOM
- 运行报错Error:Execution failed for task ':app:transformClassesWithDexForDebug'
- VMware Fusion 10序列号
- C++ 数据结构-栈
- js统计字符串出现的频率
- 根据PowerDesigner的模型生成数据库表
- DDL-数据表的创建
- LSTM implementation explained
- eMMC分区详解
- 【HDU
- POJ 2062 Card Game Cheater
- 2017-10-02(一个水杯引发的思考)