c++ 实现栈相关功能
#include <iostream>
using namespace std;
//自定义栈 先进后出
template<typename T>
class Stack
{
public:
//构造函数
Stack():_size(0),_top(-1){
cout<<"Stack()"<<endl;
_data = new T[_capacity];
}
//析构函数
~Stack(){
cout<<"~Stack()"<<endl;
delete [] _data;
}
//加入一个元素
void push(const T& obj) {
//添加元素到栈顶
cout<<"push "<<obj<<""<<endl;
//如果容量已达最大值,capacity扩容到原来两倍
//循环栈,_end和_start之间要想差一个元素
if(_size == _capacity){
expand_capacity();
}
_data[++_top]=obj;
_size++;
}
//删除栈顶
void pop() {
cout<<"pop ";
if(empty()){
throw std::out_of_range ("An error occurred,the queue is empty!");
}
cout<<_data[_top]<<endl;
_top--;
_size--;
}
//返回栈顶元素
T &top(){
if(empty()){
throw std::out_of_range ("An error occurred,the queue is empty!");
}
return _data[_top];
}
//判断栈是否为空
bool empty() const {
return _size == 0;
}
//返回栈大小
size_t size() const {
return _size;
}
//显示栈元素
void show(){
cout<<"*****1*****"<<endl;
cout<<"stack.top="<<_top<<endl;
cout<<"stack.size="<<_size<<endl;
cout<<"stack.capacity="<<_capacity<<endl;
cout<<"The stack:";
size_t i = 0;
while(i <= _top){
cout<<_data[i]<<" ";
i++;
}
cout<<endl;
cout<<"*****2*****"<<endl;
}
//扩容
void expand_capacity(){
cout<<"expand_capacity()"<<endl;
size_t newCapacity = this->_capacity*2;//扩容
T * new_data = new T[newCapacity];//申请新的空间
//复制数据
size_t i = 0;
while(i <= _top){
new_data[i] = _data[i];
i++;
}
delete [] _data;//删除原数据
_data = new_data;//替换成新数据
_capacity = newCapacity;//替换成新的容量
}
size_t getCapacity() const{
return _capacity;
}
private:
//容量 初始值设为5
size_t _capacity = 5 ;
//当前元素个数
size_t _size;
//定义容器存储数据
T *_data;
//栈顶
size_t _top;
};
int main() {
//定义类型为int的栈
Stack<int> queue;
//push() 在栈顶增加元素
queue.push(1);
queue.push(2);
queue.push(3);
queue.push(4);
queue.push(5);
queue.push(6);
//show()
queue.show();
//size() 返回栈中元素的个数
//top() 返回栈顶元素
cout<<"queue.size()="<<queue.size()<<endl;
cout<<"queue.top()="<<queue.top()<<endl;
//pop() 移除栈顶元素
queue.pop();
queue.pop();
queue.show();
queue.push(7);
queue.push(8);
queue.show();
//empty() 如果栈空则返回真
while(!queue.empty()){
queue.pop();
}
//queue.pop();//exception
queue.push(9);
queue.push(10);
queue.show();
cout<<"****************************************"<<endl;
//定义类型为string的栈
Stack<string> stack_string;
stack_string.push("01");
stack_string.push("02");
stack_string.push("03");
stack_string.push("04");
stack_string.push("05");
stack_string.push("06");
stack_string.show();
return 0;
}
Stack()
push 1
push 2
push 3
push 4
push 5
push 6
expand_capacity()
*****1*****
stack.top=5
stack.size=6
stack.capacity=10
The stack:1 2 3 4 5 6
*****2*****
queue.size()=6
queue.top()=6
pop 6
pop 5
*****1*****
stack.top=3
stack.size=4
stack.capacity=10
The stack:1 2 3 4
*****2*****
push 7
push 8
*****1*****
stack.top=5
stack.size=6
stack.capacity=10
The stack:1 2 3 4 7 8
*****2*****
pop 8
pop 7
pop 4
pop 3
pop 2
pop 1
push 9
push 10
*****1*****
stack.top=1
stack.size=2
stack.capacity=10
The stack:9 10
*****2*****
****************************************
Stack()
push 01
push 02
push 03
push 04
push 05
push 06
expand_capacity()
*****1*****
stack.top=5
stack.size=6
stack.capacity=10
The stack:01 02 03 04 05 06
*****2*****
~Stack()
~Stack()