今天学习内容:栈和队列理论基础、232.用栈实现队列、225. 用队列实现栈。今天很轻松哦~
栈和队列理论基础
队列是先进先出,栈是先进后出。
栈
C++标准库提供了一个名为 std::stack
的栈容器适配器,想使用它,只需要引入<stack
头文件即可。
#include <stack> // 引入stack头文件
然后就可以通过stack数据类型 栈名称
这样的形式来创建一个栈并进行操作了
stack<int> st; // 创建一个int类型的栈
栈的常用操作主要有以下几种:
empty()
: 判断栈是否为空栈,如果为空栈返回true
, 否则或者false
push()
: 进栈操作,将新的元素放入到栈中,新的元素成为栈顶元素。pop()
: 出栈操作,栈顶元素从栈中离开top()
: 获取栈顶元素,但是不会移除它size()
: 获取栈的长度,即栈中元素的数量
st.push(1);
st.push(10);
st.push(100); // 往栈中添加元素,现在栈底元素是1,栈顶元素是100
st.pop(); // 移除栈顶元素100,新的栈顶元素是10
int topNumber = st.top(); // 获取栈顶元素10
bool isEmpty = st.empty(); // 如果栈为空,返回true;否则返回false
int stackSize = st.size(); // 获取栈的长度(元素数量)
队列
在C++中,你可以使用标准库提供的 std::queue
来创建和操作队列,不过这需要引入头文件queue
// 引入queue头文件
#include <queue>
创建一个队列和创建一个栈的写法是相似的,需要指定队列中元素的类型,不过这也意味着队列中的元素必须是相同的数据类型,下面的代码表示创建一个字符串类型的队列。
queue<string> q; // 创建一个字符串类型的队列
队列的常用操作主要有以下几种:
empty()
: 判断队列是否为空,如果队列为空返回true
, 否则返回false
push()
: 入队操作,将新的元素添加到队列的尾部。pop()
: 出队操作,移除队列的头部元素。front()
: 访问队列的头部元素,但不会将其移除。size()
: 获取队列的长度,即队列中元素的数量。
q.push("Jack");
q.push("Mike"); // 入队了两个名称字符串
q.pop(); // 移除队列头部的元素
string name = q.front(); // 获取队列头部的元素但是不会将其移除
bool isEmpty = q.empty(); // 如果队列为空,返回true;否则返回false
int queueSize = q.size(); // 获取队列中元素的数量
顺序存储和链式存储的实现方法不同,更多内容可见以下链接:
数据结构:栈和队列(Stack & Queue)【详解】_UniqueUnit的博客-CSDN博客
232.用栈实现队列
力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台
使用栈实现队列的下列操作:
push(x) -- 将一个元素放入队列的尾部。
pop() -- 从队列首部移除元素。
peek() -- 返回队列首部的元素。
empty() -- 返回队列是否为空。
只能使用标准的栈操作 -- 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
思路如图所示。(图片来自代码随想录)在push数据的时候,只要数据放进输入栈就好,但在pop的时候,操作就复杂一些,输出栈如果为空,就把进栈数据全部导入进来(注意是全部导入),再从出栈弹出数据,如果输出栈不为空,则直接从出栈弹出数据就可以了。
最后如何判断队列为空呢?如果进栈和出栈都为空的话,说明模拟的队列为空了。peek()的实现,直接复用了pop()。
class MyQueue {
public:
stack<int> stIn;
stack<int> stOut;
/** Initialize your data structure here. */
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
stIn.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
// 只有当stOut为空的时候,再从stIn里导入数据(导入stIn全部数据)
if (stOut.empty()) {
// 从stIn导入数据直到stIn为空
while(!stIn.empty()) {
stOut.push(stIn.top());
stIn.pop();
}
}
int result = stOut.top();
stOut.pop();
return result;
}
/** Get the front element. */
int peek() {
int res = this->pop(); // 直接使用已有的pop函数
stOut.push(res); // 因为pop函数弹出了元素res,所以再添加回去
return res;
}
/** Returns whether the queue is empty. */
bool empty() {
return stIn.empty() && stOut.empty();
}
};
225. 用队列实现栈
力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台
使用队列实现栈的下列操作:
- push(x) -- 元素 x 入栈
- pop() -- 移除栈顶元素
- top() -- 获取栈顶元素
- empty() -- 返回栈是否为空
只能使用队列的基本操作-- 也就是 push to back, peek/pop from front, size, 和 is empty 这些操作是合法的。
方法一:用两个队列实现
用两个队列que1和que2实现队列的功能,que2其实完全就是一个备份的作用,把que1最后面的元素以外的元素都备份到que2,然后弹出最后面的元素,再把其他元素从que2导回que1。
class MyStack {
public:
queue<int> que1;
queue<int> que2; // 辅助队列,用来备份
/** Initialize your data structure here. */
MyStack() {
}
/** Push element x onto stack. */
void push(int x) {
que1.push(x);
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
int size = que1.size();
size--;
while (size--) { // 将que1 导入que2,但要留下最后一个元素
que2.push(que1.front());
que1.pop();
}
int result = que1.front(); // 留下的最后一个元素就是要返回的值
que1.pop();
que1 = que2; // 再将que2赋值给que1
while (!que2.empty()) { // 清空que2
que2.pop();
}
return result;
}
/** Get the top element. */
int top() {
return que1.back();
}
/** Returns whether the stack is empty. */
bool empty() {
return que1.empty();
}
};
方法二:用一个队列实现
一个队列在模拟栈弹出元素的时候只要将队列头部的元素(除了最后一个元素外) 重新添加到队列尾部,此时再去弹出元素就是栈的顺序了。
class MyStack {
public:
queue<int> que;
/** Initialize your data structure here. */
MyStack() {
}
/** Push element x onto stack. */
void push(int x) {
que.push(x);
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
int size = que.size();
size--;
while (size--) { // 将队列头部的元素(除了最后一个元素外) 重新添加到队列尾部
que.push(que.front());
que.pop();
}
int result = que.front(); // 此时弹出的元素顺序就是栈的顺序了
que.pop();
return result;
}
/** Get the top element. */
int top() {
return que.back();
}
/** Returns whether the stack is empty. */
bool empty() {
return que.empty();
}
};
今日总结
1.了解栈和队列的基础知识,知道栈和队列的基本操作。
2.实现栈和队列的互相实现,具体理解可以画图加深了解。