C++实现栈(Stack)(顺序栈+链栈+STL模板栈)

栈的定义

栈是只能在一端插入和删除元素的线性表。
特性:后进先出

栈的术语

入栈(压栈):指将元素压入栈内。
出栈(弹栈):指将元素弹出栈。
栈顶:栈的顶部。
栈底:栈的底部。

在这里插入图片描述

栈的基本运算

(1)初始化 :设置栈为空。
(2)判断栈为空:
若为空,则返回TRUE,否则返回FALSE.
(3)判断栈为满:
若为满,则返回TRUE,否则返回FALSE.
(4)取栈顶元素:取出栈顶元素。
条件:栈不空。
否则,应能明确给出标识,以便程序的处理。
(5)入栈:将元素入栈,即放到栈顶。
这里要注意栈满的情况。
(6)出栈:删除当前栈顶的元素。
这里要考虑栈空的情况

栈的实现

这里要注意栈的两种存储结构(线性存储和链式存储)
1.线性存储
源码:

#include<iostream>
using namespace std;
#define maxlen 100 //这里用来定义栈的最大容量

enum error_code {success,overflow,underflow};//用来表示栈功能的实现情况

template<typename T>
class Stack
{
private:
	int count; //用来记录栈中元素的数量
	T data[maxlen]; //保存栈的元素
public:
	Stack();   //构造函数
	bool empty() const;  //判空
	bool full()  const;  //判满
	error_code get_top(T &x) const; //取栈顶的元素
	error_code push(const T x);     //入栈
	error_code pop();               //出栈 

};

template<typename T>
Stack<T>::Stack()
{
	count = 0;
}

template<typename T>
bool Stack<T>::full() const
{
	return count == maxlen-1;
}

template<typename T>
bool Stack<T>::empty() const
{
	return count == 0;
}

template<typename T>
error_code Stack<T>::get_top(T &x) const
{
	if(empty())  //若栈空则返回underflow(下溢)
		return underflow;
	x = data[count-1];
	return success;	
}

template<typename T>
error_code Stack<T>::push(const T x)
{
	if(full())//若栈满则返回overflow(溢出)
		return overflow;
	data[count] = x;
	count++;
	return success;

}

template<typename T>
error_code Stack<T>::pop()
{
	if(empty())//若栈空则返回underflow(下溢)
		return underflow;
	count--;
	return success;
}


int main()
{


	return 0;
}

2.链式存储
这里要特别注意把栈顶放在链首还是链尾
为了减少入栈和出栈的时间,把栈顶放在链首

源码:

#include <iostream>
using namespace std;

enum error_code{success,underflow,overflow}; //用来表示栈功能的实现情况

struct node    //链的节点
{
	int data;
	node *next; 
};

class Stack
{
private:
	node *top; //用来记录栈顶(链首)
	int count;
public:
	Stack();
	~Stack();
	bool full() const;
	bool empty() const;
	error_code get_top(int &x) const;
	error_code push(int x);
	error_code pop();
};

Stack::Stack()
{
	count = 0;
	top = NULL;
}

bool Stack::empty() const
{
	return count == 0;
}

bool Stack::full() const
{	
	return false;   //因为是链式存储,每次插入仅仅需要申请新的节点即可,因此没有上限
}

error_code Stack::get_top(int &x) const
{
	if(empty())
		return underflow;
	x = top->data;
	return success;
}

error_code Stack::push(int x)
{
	node* s = new node;
	s->data = x;
	s->next = top;
	top = s;
	count++;
	return success;
}

error_code Stack::pop()
{
	if(empty())
		return underflow;
	node *s = top;
	top = top->next;
	delete s;
	count--;
	return success;
}

Stack::~Stack()
{
	while(!empty())
		pop();   //只要栈不空就持续出栈
}

int main()
{
	return 0;
}


标准模板库里的栈(STL)

1.头文件
2.定义方法:

stack<elementtype>+栈名

如,我要定义一个int型的栈 栈名 mystack
stack<int> mystack;

3.基本运算
push(elementtype x) 将x加入栈中,即入栈操作
pop() 出栈操作(删除栈顶),只是出栈,没有返回值
top() 返回第一个元素(栈顶元素)
size() 返回栈中的元素个数
empty() 当栈为空时,返回 true

stack<int> mystack;
mystack.push(1);
mystack.top();
mystack.size();
mtstack.empty();
mystack.pop();

结语

近期要进行数据结构的期末考,因此便总结了一下课堂上所学到的内容。可能会有很多的错误,感谢大家的指正,谢谢。

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
C++ STL中的是通过标准库中的`<stack>`头文件实现的。在C++中,被定义为`stack<Type>`,其中`Type`是中存储的元素的类型。 的主要操作有: - `push(a)`:将元素`a`推入顶。 - `pop()`:删除顶的元素,但不会返回被删除的元素。 - `top()`:返回顶的元素,但不会删除顶元素。 - `size()`:返回中元素的个数。 - `empty()`:检查是否为空,如果为空返回`true`,否则返回`false`。 在C++ STL中,实现使用了容器适配器的概念。基于的默认实现,它使用了`deque`(双端队列)作为其底层容器,但也可以选择使用`vector`或`list`作为底层容器。这意味着提供了一种在底层容器上进行堆操作的统一接口,无论使用哪种容器作为底层实现。 对于自定义的实现,您可以参考以下示例代码: ``` #include <iostream> #include <stack> using namespace std; class My_stack { private: stack<int> data; public: void push(int x) { data.push(x); } int pop() { int x = data.top(); data.pop(); return x; } int top() { return data.top(); } bool empty() { return data.empty(); } }; int main() { My_stack s; s.push(1); s.push(2); s.push(3); cout << "Top element: " << s.top() << endl; // 输出顶元素 cout << "Pop element: " << s.pop() << endl; // 输出弹出的元素 cout << "Is stack empty? " << (s.empty() ? "Yes" : "No") << endl; // 检查是否为空 return 0; } ``` 以上是关于C++ STL实现和用法的介绍。希望能对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值