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;
}


栈的实现并不是很难,但是栈的应用却很广,并且也不是这么简单的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值