1.栈的定义
栈是一种特殊的线性表,在这种线性表中的插入和删除运算限定在表的一段进行。允许插入和删除的一端称为栈顶,另一端称为端底,处于栈顶位置的元素被称为栈顶元素,一个栈如果没有元素,则被称为是空栈。在栈中插入一个元素被称为入栈,删除一个元素为出栈。 栈一定满足后进先出。
栈的基本运算:
1.创建一个栈 create
2.进栈push(x)
3.出栈pop(x)
4.读栈顶元素top()
5.判断栈空isEmpty()
栈的抽象类:
template<class elemType>
class stack{
public:
virtual bool isEmpty() = 0;
virtual void push(const elemType&x) = 0;
virtual elemType pop() = 0;
virtual elemType top()const = 0;
virtual ~stack(){}
};
2.栈的顺序实现
template <class elemType>
class stack
{
public:
virtual bool isEmpty() const= 0;
virtual void push(const elemType &x) = 0;
virtual elemType pop() = 0;
virtual elemType top() const = 0;
virtual ~stack() {}
};
template <class elemType>
class seqStack : public stack<elemType>
{
private:
elemType *elem;
int top_p;
int maxSize;
void doubleSpace();
public:
seqStack(int initSize = 10);
~seqStack();
bool isEmpty() const;
void push(const elemType &x);
elemType pop();
elemType top() const;
};
/**
* 构造函数
*/
template <class elemType>
seqStack<elemType>::seqStack(int initSize)
{
elem = new elemType[initSize];
maxSize = initSize;
top_p = -1;
}
/**
* 析构函数
*/
template <class elemType>
seqStack<elemType>::~seqStack()
{
delete[] elem;
}
/**
* isEmpty 函数 判断栈是否为空
*/
template <class elemType>
bool seqStack<elemType>::isEmpty() const
{
return top_p == -1;
}
/**
* push 函数 进栈
*/
template <class elemType>
void seqStack<elemType>::push(const elemType &x)
{
if (top_p == maxSize - 1)
doubleSpace();
elem[++top_p] = x;
}
/**
* pop 函数 出栈
*/
template <class elemType>
elemType seqStack<elemType>::pop()
{
return elem[top_p--];
}
/**
* top 函数 获得栈顶元素
*/
template <class elemType>
elemType seqStack<elemType>::top() const
{
return elem[top_p];
}
/**
* doubleSpace 函数 扩展空间
*/
template <class elemType>
void seqStack<elemType>::doubleSpace()
{
elemType *tmp = elem;
maxSize *= 2;
elem = new elemType[maxSize];
for (int i = 0; i < maxSize / 2; ++i)
elem[i] = tmp[i];
delete[] tmp;
}
3.栈的链接实现:
#pragma once
template <class elemType>
class stack
{
public:
virtual bool isEmpty() const = 0;
virtual void push(const elemType& x) = 0;
virtual elemType pop() = 0;
virtual elemType top() const = 0;
virtual ~stack() {}
};
template <class elemType>
class linkStack : public stack<elemType>
{
private:
struct node
{
elemType data;
node* next;
node(const elemType& x, node* n = nullptr)
{
data = x;
next = n;
}
node() : next(nullptr) {}
~node() {}
};
node* top_p;
public:
linkStack();
~linkStack();
bool isEmpty() const;
void push(const elemType& x);
elemType pop();
elemType top() const;
};
/**
* 构造函数
*/
template <class elemType>
linkStack<elemType>::linkStack() {
top_p = nullptr;
}
/**
* 析构函数
*/
template<class elemType>
linkStack<elemType>::~linkStack() {
node* tmp;
while (top_p != nullptr) {
tmp = top_p;
top_p = top_p->next;
delete tmp;
}
}
/**
* isEmpty 函数 判断是否为空栈
*/
template<class elemType>
bool linkStack<elemType>::isEmpty()const {
return top_p == nullptr;
}
/**
* push 函数 元素进栈
*/
template<class elemType>
void linkStack<elemType>::push(const elemType& x) {
top_p = new node(x, top_p);
}
/**
* pop 函数 元素出栈
*/
template<class elemType>
elemType linkStack<elemType>::pop() {
node* tmp = top_p;
elemType x = tmp->data;
top_p = top_p->next;
delete tmp;
return x;
}
/**
* top 函数 取栈顶元素
*/
template<class elemType>
elemType linkStack<elemType>::top()const {
return top_p->data;
}
栈的实现并不是很难,但是栈的应用却很广,并且也不是这么简单的。