C++实现栈
来源:互联网 发布:淘宝如何拉免费流量 编辑:程序博客网 时间:2024/06/05 16:10
栈只允许在末端进行插入和删除的线性表。栈具有后进先出的特性(LIFO,Last In First Out)。
#pragma once#include<assert.h>template<class T>class Stack{public:Stack():_array(NULL), _capicity(0), _topIndex(-1){}~Stack(){delete[]_array;_capicity = 0;_topIndex = -1;}void Push(const T& x)//入栈{if (_topIndex + 1 == _capicity){_capicity = 2 * _capicity + 3;T* tmp = new T[_capicity];if (tmp == NULL){cout << " New Failed!" << endl;exit(-1);}memcpy(tmp,_array,sizeof(T)*(_topIndex+1));delete[] _array;_array = tmp;}_array[++_topIndex] = x;}void Pop()//出栈{assert(_topIndex > -1);--_topIndex;}bool Empty(){return _topIndex == -1;}bool Full() const{return _topIndex == _capicity - 1;}const T& Top(){return _array[_topIndex];}private:T* _array; //数据块size_t _capicity; //容量int _topIndex; //栈顶数据的下标};
#include<iostream>#include"Stack.hpp"using namespace std;void Test(){Stack<int> s;s.Push(1);s.Push(2);s.Push(3);s.Push(4);s.Pop();while (!s.Empty()){cout << s.Top() << " ";s.Pop();}cout << endl;}int main(){Test();return 0;}
0 0
- c实现顺序栈
- C实现链式栈
- 栈C语言实现
- C语言实现栈
- 栈的C实现
- c实现顺序栈
- C语言实现栈
- C实现栈
- C语言实现栈
- 栈(c实现)
- C语言实现栈
- C语言实现栈
- 【c++】栈的实现
- C代码实现栈
- C链栈实现
- C链式栈实现
- c语言实现栈
- C语言实现栈
- no matching key exchange method found
- 使用YCSB对HBase进行压测
- 如何打开.war文件
- 数据结构之链表的增删操作的Java实现
- 剑指offer:数组中出现次数超过一半的数字
- C++实现栈
- 字节、字、bit、Byte、byte的关系区分
- iframe自适应大小
- 人生感悟3
- JavaScript Array(数组)
- Android Studio out of memory
- 安卓驱动开发要干的那些事
- GSON实例详解
- View的事件分发机制一:事件分发概述