C++ 手动实现栈(stack) (课后作业版)
创始人
2024-02-13 00:14:19
0

栈,一种遵循先进先出原则的数据结构,可以用顺序表实现,也可以用链表进行实现。

这里我使用数组实现方法,包含了进栈,出栈,访问栈顶等功能,以及一些辅助功能。

栈Stack类定义如下:

template 
class Stack {
public:Stack();Stack(int n);Stack(Stack& stack);~Stack();T& top();Stack& push(const T& elem);Stack& pop();void reserve(int num);int size() {return current+1; }int capciaty() {return cap; }int is_empty() {return current == -1; }bool is_full() {return current == (cap-1); }void clear() {this->~Stack();}
private:T* arr;int current;int cap;
};

其中,成员变量的解释:

arr :数组指针,指向栈底

current : 当前栈顶的索引,没有元素的时候为-1, 有一个元素的时候为0, 以此类推

cap :数组容量,注意容量和元素个数是不同的概念

然后,是成员函数的解释:

Stack();    默认构造函数
Stack(int n);    一般构造函数,容量为n
Stack(Stack& stack);    拷贝构造函数,浅拷贝
~Stack();       析构函数
T& top();      访问栈顶
Stack& push(const T& elem);    进栈
Stack& pop();    出栈
void reserve(int num);    增加容量
int size() {return current+1; }    获取当前元素个数
int capciaty() {return cap; }     获取容量
int is_empty() {return current == -1; }   是否为空栈
bool is_full() {return current == (cap-1); }    是否满栈
void clear() {this->~Stack();}    清除,调用析构函数

完成实现代码:

#include using namespace std;template 
class Stack {
public:Stack();Stack(int n);Stack(Stack& stack);~Stack();T& top();Stack& push(const T& elem);Stack& pop();void reserve(int num);int size() {return current+1; }int capciaty() {return cap; }int is_empty() {return current == -1; }bool is_full() {return current == (cap-1); }void clear() {this->~Stack();}
private:T* arr;int current;int cap;
};//默认构造函数
template 
Stack::Stack() {cap = 0;current = -1;arr = nullptr;
}//一般构造函数
template 
Stack::Stack(int n) {cap = n;current = -1;arr = new T[n]{};
}//拷贝构造函数(前浅贝)
template 
Stack::Stack(Stack& stack) {cap = stack.capciaty();current = stack.size();this->arr = stack.arr;
}//析构函数
template 
Stack::~Stack() {if ( cap == 0 ) {return;}cap = 0;current = -1;delete [] arr;arr = nullptr;
}//访问栈顶
template 
T& Stack::top() {if ( is_empty() ) {cout << "[error]: stack has no element" << endl;}return *(arr+current);
}//在栈顶添加一个元素
template 
Stack& Stack::push(const T& elem) {if ( is_full() ) {reserve(2*cap);}current++;arr[current] = elem;return *this;
}//栈顶弹出
template 
Stack& Stack::pop() {if ( is_empty() ) {cout << "[error]: don't try to pop a empty stack" << endl;return *this;}current--;return *this;
}//增加容量
template 
void Stack::reserve(int num) {if ( num < cap ) {cout << "[warning]: input of reserve() function shuold lager than capciaty" << endl;return;}T *arr_ = new T[num]{};for ( int i = 0; i <= current; i++ )arr_[i] = arr[i];delete [] arr;arr = arr_;arr_ = nullptr;cap = num;
}int main() {Stack stack(3);
//    cout << "top=" << stack.top() << ", 容量=" << stack.capciaty() << ", 元素个数=" << stack.size() << endl;stack.push(3);cout << "top=" << stack.top() << ", 容量=" << stack.capciaty() << ", 元素个数=" << stack.size() << endl;stack.push(2);cout << "top=" << stack.top() << ", 容量=" << stack.capciaty() << ", 元素个数=" << stack.size() << endl;stack.push(5);cout << "top=" << stack.top() << ", 容量=" << stack.capciaty() << ", 元素个数=" << stack.size() << endl;stack.push(5);cout << "top=" << stack.top() << ", 容量=" << stack.capciaty() << ", 元素个数=" << stack.size() << endl;stack.pop();cout << "top=" << stack.top() << ", 容量=" << stack.capciaty() << ", 元素个数=" << stack.size() << endl;stack.clear();cout << "top=" << stack.top() << ", 容量=" << stack.capciaty() << ", 元素个数=" << stack.size() << endl;
}

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
客厅放八骏马摆件可以吗(家里摆... 今天给各位分享客厅放八骏马摆件可以吗的知识,其中也会对家里摆八骏马摆件好吗进行解释,如果能碰巧解决你...
美团联名卡审核成功待激活(美团... 今天百科达人给各位分享美团联名卡审核成功待激活的知识,其中也会对美团联名卡审核未通过进行解释,如果能...