栈
栈(stack)是限定仅在表尾进行插入和删操作的线性表,允许插入和删除的一端称为栈顶,另一端称为栈底。
栈的特性:最后入栈的最先出栈,即 先进后出
。
栈的顺序存储结构及实现
栈的顺序存储结构称为 顺序栈
。
通常,把数组下标为0的一端作为栈底,同时附设指针top指示栈顶元素在数组中的位置(或者将top指向栈中第一个空闲位置,此时栈空应该表示为top=0),则栈空时top=-1,栈满时top=StackSize(数组长度) - 1。入栈时,栈顶指针top + 1,出栈时,栈顶指针top - 1
。
实现:
SeqStack.h
//
// Created by 野尽 on 2020/3/29.
//
#ifndef SEQLIST_CLASSTEMPLATEINHFILE__SEQSTACK_H
#define SEQLIST_CLASSTEMPLATEINHFILE__SEQSTACK_H
#include <iostream>
using namespace std;
const int StackSize = 100;
template <class DataType>
class SeqStack
{
public:
// 构造函数,建立一个空顺序栈
SeqStack();
// // 构造函数,建立一个顺序栈
// SeqStack(DataType a[], int n);
// 析构函数
~SeqStack();
// 入栈操作,将元素 x 入栈
void Push(DataType x);
// 出栈操作,将栈顶元素弹出
DataType Pop();
// 取出栈顶元素
DataType GetTop();
// 判断栈是否为空
int Empty();
// // 遍历顺序栈
// void PrintSeqStack();
private:
DataType data[StackSize];
int top;
};
template <class DataType>
SeqStack<DataType>::SeqStack() {
top = -1;
}
template<class DataType>
SeqStack<DataType>::~SeqStack() {
cout<<"Sequence Stack cleaned!"<<endl;
}
template<class DataType>
void SeqStack<DataType>::Push(DataType x) {
if (top == StackSize - 1)
throw "Overflow!";
data[++top] = x;
}
template<class DataType>
DataType SeqStack<DataType>::Pop() {
if (top == -1)
throw "Underflow!";
DataType x = data[top--];
return x;
}
template<class DataType>
DataType SeqStack<DataType>::GetTop() {
return data[top];
}
template<class DataType>
int SeqStack<DataType>::Empty() {
if (top == -1)
return 1;
else
return 0;
}
//template<class DataType>
//void SeqStack<DataType>::PrintSeqStack() {
//
// while (data[top--]!=NULL)
// {
// cout<<" "<<data[top];
// }
//
// cout<<endl;
//}
//
//template<class DataType>
//SeqStack<DataType>::SeqStack(DataType a[], int n) {
//
// if (n < 0)
// throw "Size error!";
// if (n > StackSize)
// throw "beyond max StackSize!";
//
// for (top = 0; top < n; top++)
// {
// data[top] = a[top];
cout<< top <<" "<< a[top] <<" "<< data[top] <<endl;
// }
//}
#endif //SEQLIST_CLASSTEMPLATEINHFILE__SEQSTACK_H
main.cpp
#include "SeqStack.h"
int main() {
SeqStack <int> seqStack;
if (seqStack.Empty() == 0)
cout<< "不是空栈!" <<endl;
else if(seqStack.Empty() == 1)
cout<< "空栈!" <<endl;
int x = 1;
seqStack.Push(x);
cout<< "入栈元素为:" << x << endl;
int y = seqStack.GetTop();
cout<< "栈顶元素为:" << y <<endl;
y = seqStack.Pop();
cout<< "出栈元素为:" << y <<endl;
return 0;
}
测试结果:
空栈!
入栈元素为:1
栈顶元素为:1
出栈元素为:1
Sequence Stack cleaned!
两栈共享空间
如果一个程序中需要同时使用具有相同数据类型的两个栈,可以使用一个数组来存储两个栈,让一个栈的栈底为该数组的始端,另一个栈的栈底为该数组的末端,每个栈从各自的端点向中间延伸。
实现:
BothStack.h
//
// Created by 野尽 on 2020/3/30.
//
#ifndef SEQLIST_CLASSTEMPLATEINHFILE__BOTHSTACK_H
#define SEQLIST_CLASSTEMPLATEINHFILE__BOTHSTACK_H
#include <iostream>
using namespace std;
const int StackSize = 100;
template <class DataType>
class BothStack
{
public:
// 构造函数,初始化两个栈
BothStack();
// 析构函数
~BothStack();
// 入栈操作,将元素 x 压入栈 i
void Push(int i, DataType x);
// 出栈操作,对栈 i 执行出栈操作
DataType Pop(int i);
// 取出栈 i 的栈顶元素
DataType GetTop(int i);
// 判断栈 i 是否为空栈
int Empty(int i);
private:
DataType data[StackSize];
int top1, top2;
};
template<class DataType>
BothStack<DataType>::BothStack() {
top1 = -1;
top2 = StackSize;
}
template<class DataType>
BothStack<DataType>::~BothStack() {
cout<<"BothStack had heen cleaned!"<<endl;
}
template<class DataType>
void BothStack<DataType>::Push(int i, DataType x) {
if (top1 == top2 - 1)
throw "Overflow!";
if (i == 1)
data[++top1] = x;
if (i == 2)
data[--top2] = x;
}
template<class DataType>
DataType BothStack<DataType>::Pop(int i) {
if (i == 1)
{
if (top1 == -1)
throw "Underflow!";
return data[top1--];
}
if (i == 2)
{
if (top2 == StackSize)
throw "Underflow!";
return data[top2++];
}
}
template<class DataType>
DataType BothStack<DataType>::GetTop(int i) {
if (i == 1)
{
if (top1 == -1)
{
throw "Stack1 Null!";
} else
{
return data[top1];
}
}
if (i == 2)
{
if (top2 == StackSize)
{
throw "Stack2 Null!";
} else
{
return data[top2];
}
}
}
template<class DataType>
int BothStack<DataType>::Empty(int i) {
if (i == 1)
{
if (top1 == -1)
return 1;
else
return 0;
}
if (i == 2)
{
if (top2 == StackSize)
return 1;
else
return 0;
}
}
#endif //SEQLIST_CLASSTEMPLATEINHFILE__BOTHSTACK_H
main.cpp
#include "BothStack.h"
int main() {
// BothStack
BothStack <int> bothStack;
int i1 = bothStack.Empty(1);
int i2 = bothStack.Empty(2);
if (i1 == 1)
cout<<"共享栈1 为空!"<<endl;
else
cout<<"共享栈1 不空!"<<endl;
if (i2 == 1)
cout<<"共享栈2 为空!"<<endl;
else
cout<<"共享栈2 不空!"<<endl;
cout<<endl;
int x1 = 1;
int x2 = 2;
bothStack.Push(1, x1);
bothStack.Push(2, x2);
cout<<"入栈操作执行后:"<<endl;
i1 = bothStack.Empty(1);
i2 = bothStack.Empty(2);
if (i1 == 1)
cout<<"共享栈1 为空!"<<endl;
else
cout<<"共享栈1 不空!"<<endl;
if (i2 == 1)
cout<<"共享栈2 为空!"<<endl;
else
cout<<"共享栈2 不空!"<<endl;
cout<<endl;
cout<< "共享栈1 栈顶元素为:" << bothStack.GetTop(1)<<endl;
cout<< "共享栈2 栈顶元素为:" << bothStack.GetTop(2)<<endl;
cout<<endl;
cout<< "共享栈1 出栈元素为:" << bothStack.Pop(1)<<endl;
cout<< "共享栈2 出栈元素为:" << bothStack.Pop(2)<<endl;
cout<<"出栈操作执行后:"<<endl;
i1 = bothStack.Empty(1);
i2 = bothStack.Empty(2);
if (i1 == 1)
cout<<"共享栈1 为空!"<<endl;
else
cout<<"共享栈1 不空!"<<endl;
if (i2 == 1)
cout<<"共享栈2 为空!"<<endl;
else
cout<<"共享栈2 不空!"<<endl;
return 0;
}
测试结果:
共享栈1 为空!
共享栈2 为空!
入栈操作执行后:
共享栈1 不空!
共享栈2 不空!
共享栈1 栈顶元素为:1
共享栈2 栈顶元素为:2
共享栈1 出栈元素为:1
共享栈2 出栈元素为:2
出栈操作执行后:
共享栈1 为空!
共享栈2 为空!
BothStack had heen cleaned!
栈的链式存储结构及实现
栈的链接存储结构称为 链栈
。
通常链栈用单链表
表示,以单链表的头部为栈顶,只在栈顶执行插入和删除。
实现:
LinkStack.h
//
// Created by 野尽 on 2020/3/30.
//
#ifndef SEQLIST_CLASSTEMPLATEINHFILE__LINKSTACK_H
#define SEQLIST_CLASSTEMPLATEINHFILE__LINKSTACK_H
#include <iostream>
using namespace std;
template <class DataType>
struct Node
{
DataType data;
Node<DataType> * next;
};
template <class DataType>
class LinkStack
{
public:
// 构造函数,初始化一个空链栈
LinkStack();
// 析构函数
~LinkStack();
// 入栈操作,将元素 x 入栈
void Push(DataType x);
// 出栈操作,将栈顶元素出栈
DataType Pop();
// 取栈顶元素
DataType GetTop();
// 判断链栈是否为空
int Empty();
private:
// 栈顶指针及链栈的头指针
Node<DataType> * top;
};
template<class DataType>
LinkStack<DataType>::LinkStack() {
top = NULL;
}
template<class DataType>
LinkStack<DataType>::~LinkStack() {
cout<<"LinkStack had been cleaned!"<<endl;
}
template<class DataType>
void LinkStack<DataType>::Push(DataType x) {
Node<DataType> * s;
s = new Node<DataType>;
s->data = x;
s->next = top;
top = s;
}
template<class DataType>
DataType LinkStack<DataType>::Pop() {
if (top == NULL)
throw "Oveeflow!";
DataType x = top->data;
Node<DataType> * p = top;
top = top->next;
delete p;
return x;
}
template<class DataType>
DataType LinkStack<DataType>::GetTop() {
if (top != NULL)
return top->data;
}
template<class DataType>
int LinkStack<DataType>::Empty() {
if (top == NULL)
return 1;
else
return 0;
}
#endif //SEQLIST_CLASSTEMPLATEINHFILE__LINKSTACK_H
main.cpp
#include "LinkStack.h"
int main() {
// LinkStack
LinkStack <int> linkStack;
int i = linkStack.Empty();
if (i == 1)
cout<<"链栈 为空!"<<endl;
else
cout<<"链栈 不空!"<<endl;
int x = 1;
linkStack.Push(x);
cout<<"入栈操作执行后:"<<endl;
i = linkStack.Empty();
if (i == 1)
cout<<"链栈 为空!"<<endl;
else
cout<<"链栈 不空!"<<endl;
cout<< "链栈 栈顶元素为:" <<linkStack.GetTop()<<endl;
linkStack.Pop();
cout<<"出栈操作执行后:"<<endl;
i = linkStack.Empty();
if (i == 1)
cout<<"链栈 为空!"<<endl;
else
cout<<"链栈 不空!"<<endl;
return 0;
}
测试结果:
链栈 为空!
入栈操作执行后:
链栈 不空!
链栈 栈顶元素为:1
出栈操作执行后:
链栈 为空!
LinkStack had been cleaned!
顺序栈和链栈的比较
顺序栈和链栈的所有基本操作算法所需时间均为常数。当栈的使用过程中元素个数变化较大
时,用链栈
适宜;反之,应该用顺序栈。