文章目录
1. 概念
栈(stack)是一种形式的表,在这种数据结构中,所有的元素插入和删除都在表的一端进行,这一端称为栈顶(top)。向栈中增加一项时,叫入栈(push),在栈中删除一项时,称为出栈(pop)。最后压入到栈中的项总是最先弹出。这种性质叫后进先出(last in, first out, LIFO)。
比如食堂堆叠起来的餐盘,就餐时总是从最顶部拿走一个餐盘,工作人员将餐盘返回时放在堆顶上。最后放的餐盘在堆顶上,会最先被拿走;而最先放的餐盘在堆底,会最后一个被使用。(假设每次只存取一个)
2. 初体验
首先直接使用c++标准模板库(STL)中的stack,实现反转表(Reversing a List),体会stack的功能。
#include <iostream>
#include <stack>
using namespace std;
int main(){
int n;
double item;
stack<double> numbers;
cout << "输入数量n,并输入n个数,这些数会逆序输出。" << endl;
cin >> n;
for(int i = 0; i < n; i++){
cin >> item;
numbers.push(item);
}
cout << endl;
while(!numbers.empty()){
cout << numbers.top() << " ";
numbers.pop();
}
}
3. 栈的实现
3.1 Contiguous Stack
顺序栈,在内存中连续存储,在代码上的体现就是使用数组实现。
enum Error_code{success, underflow, overflow};
template <class Stack_entry>
class Stack{
public:
Stack(int size = 10);
~Stack();
bool empty() const;
bool full() const;
Error_code pop();
Error_code push(const Stack_entry &item);
Error_code top(Stack_entry &item) const;
private:
int size; // 最大容量
int top_; // 栈顶位置
Stack_entry* entries;
};
template <typename Stack_entry>
Stack<Stack_entry>::Stack(int s)
: size(s > 0 ? s : 10),
top_(-1),
entries(new Stack_entry[size]){
}
template <typename Stack_entry>
Stack<Stack_entry>::~Stack(){
delete [] entries;
}
template <typename Stack_entry>
bool Stack<Stack_entry>::empty() const{
return top_ == -1;
}
template <typename Stack_entry>
bool Stack<Stack_entry>::full() const{
return top_ >= size - 1;
}
template <typename Stack_entry>
Error_code Stack<Stack_entry>::pop(){
Error_code outcome = success;
if(empty()){
outcome = underflow;
}else{
top_--;
}
return outcome;
}
template <typename Stack_entry>
Error_code Stack<Stack_entry>::push(const Stack_entry &item){
Error_code outcome = success;
if(full()){
outcome = overflow;
}else{
entries[++top_] = item;
}
return outcome;
}
template <typename Stack_entry>
Error_code Stack<Stack_entry>::top(Stack_entry &item) const{
Error_code outcome = success;
if(empty()){
outcome = underflow;
}else{
item = entries[top_];
}
return outcome;
}
3.2 Linked Stack
链式栈,在内存中不连续存储,在代码上的体现是使用链实现。
链式的实现方法可以节约内存,需要多少就分配多少,不像顺序栈那样一开始就要指定内存大小。
但链还需要存储指向下一节点的指针,在某种意义上减小了内存利用率,并且在出栈入栈操作时还需要进行释放内存和指针的赋值操作,效率较低。
enum Error_code{success, underflow, overflow};
template <class Stack_entry>
class Stack{
public:
Stack();
~Stack();
bool empty() const;
Error_code pop();
Error_code push(const Stack_entry &item);
Error_code top(Stack_entry &item) const;
private:
struct Node{
Stack_entry entry;
Node *next;
Node(Stack_entry entry_, Node* next_ = NULL):
entry(entry_), next(next_){
}
};
Node *top_node; // 栈顶指针
};
template <typename Stack_entry>
Stack<Stack_entry>::Stack() :
top_node(NULL){
}
template <typename Stack_entry>
Stack<Stack_entry>::~Stack(){
Node *temp = top_node;
while(temp != NULL){
top_node = top_node->next;
delete temp;
temp = top_node;
}
}
template <typename Stack_entry>
bool Stack<Stack_entry>::empty() const{
return top_node == NULL;
}
template <typename Stack_entry>
Error_code Stack<Stack_entry>::pop(){
Error_code outcome = success;
if(empty()){
outcome = underflow;
}else{
Node *temp = top_node;
top_node = top_node->next;
delete temp;
}
return outcome;
}
template <typename Stack_entry>
Error_code Stack<Stack_entry>::push(const Stack_entry &item){
Node *temp = new Node(item, top_node);
Error_code outcome = success;
if(temp == NULL){
outcome = overflow;
}else{
top_node = temp;
}
return outcome;
}
template <typename Stack_entry>
Error_code Stack<Stack_entry>::top(Stack_entry &item) const{
Error_code outcome = success;
if(empty()){
outcome = underflow;
}else{
item = top_node->entry;
}
return outcome;
}
4. 相关算法
4.1 括号匹配
问题:输入一段带括号的字符串,判断其中的括号是否成对出现。
可以使用栈来实现,思路如下:
(1) 当遇到左括号时,入栈;
(2) 遇到右括号时
(2.1) 若栈为空,则不匹配,否则
(2.2) 判断一下栈顶是不是跟该右括号匹配,并将其出栈
(3) 循环(1)或(2)直到字符串结束,若最终没有出现不匹配的括号且栈为空,则所有括号成功匹配
代码实现如下:
(注:这里使用了上述自定义的栈实现)
#include <iostream>
#include "stack.hpp"
using namespace std;
int main(){
Stack<char> openings;
char symbol;
bool is_matched = true;
while((symbol = cin.get()) != '\n'){
if(symbol == '(' || symbol == '[' || symbol == '{'){ // 左括号
openings.push(symbol);
}else if(symbol == ')' || symbol == ']' || symbol == '}'){ // 右括号
if(openings.empty()){
is_matched = false;
cout << "括号 " << symbol <<" 不匹配。" << endl;
}else{
char top;
openings.top(top);
openings.pop();
if(!(symbol == ')' && top == '('
|| symbol == ']' && top == '['
|| symbol == '}' && top == '{')){
is_matched = false;
cout << "括号 " << symbol <<" 不匹配。" << endl;
}
}
}
}
if(is_matched && openings.empty()){
cout << "成功匹配";
}else{
cout << "匹配失败";
}
}
4.2 铁道交换网
文章链接:https://blog.csdn.net/Runner1st/article/details/86366866
4.3 逆波兰计算器(a reverse Polish calculator)
思想是,遇到操作数就入栈,遇到操作符就取栈顶的两个元素出来操作,并将结果入栈。具体代码实现可以参考别人的一篇文章:
https://blog.csdn.net/fuck_you_sb/article/details/79972746