在C++中栈与队列的常用函数有:
栈 (stack
)
函数 | 返回值 | 描述 |
---|---|---|
push(element) | 无 | 将元素放入栈顶。 |
pop() | 无 | 移除栈顶元素。注意这个操作不会返回被移除的元素。 |
top() | 栈顶元素的引用 | 返回栈顶元素。栈必须非空。 |
empty() | 布尔值 (true 或 false ) | 如果栈为空,则返回 true ;否则返回 false 。 |
size() | 栈中元素的数量 | 返回栈中元素的数量。 |
队列 (queue
)
函数 | 返回值 | 描述 |
---|---|---|
push(element) | 无 | 将元素放入队列的末尾。 |
pop() | 无 | 移除队列的第一个元素。注意这个操作不会返回被移除的元素。 |
front() | 队列第一个元素的引用 | 返回队列的第一个元素。队列必须非空。 |
back() | 队列最后一个元素的引用 | 返回队列的最后一个元素。队列必须非空。 |
empty() | 布尔值 (true 或 false ) | 如果队列为空,则返回 true ;否则返回 false 。 |
size() | 队列中元素的数量 | 返回队列中元素的数量。 |
这些表格可以直接复制并在支持 Markdown 的环境中粘贴使用。
LeetCode232:用栈实现队列
思路:两个栈来实现队列,一个只负责进,另一个只负责pop和peek。我这里稍微复杂了点,完成了一次pop和push之后都将out里面的元素在倒回in里面。
关键点:几个栈和队列的函数方法要熟记。
class MyQueue {
// 用两个栈实现队列,
// 一个负责进 一个负责出
// 这里就是在每次对out进行pop peek之前,先将in元素全部转换到out,
// 再将 Out里面的元素全部还到in里面。
// 进的只负责push,另一个负责pop, peek,
Stack<Integer> stackIn;
Stack<Integer> stackOut;
public MyQueue() {
stackIn = new Stack<>();
stackOut = new Stack<>();
}
public void push(int x) {
stackIn.push(x);
}
public int pop() {
stackInToStackOut();
int ans = stackOut.pop();
stackOutToStackIn();
return ans;
}
public int peek() {
stackInToStackOut();
int ans = stackOut.peek();
stackOutToStackIn();
return ans;
}
public boolean empty() {
if(stackIn.isEmpty() && stackOut.isEmpty()){ //
return true;
}
else{
return false;
}
}
public void stackInToStackOut(){
// 将in的元素倒放到out里面去
while(!stackIn.isEmpty()){ // 要保证in内部的元素,全部放到out里面
stackOut.push(stackIn.pop());
}
}
public void stackOutToStackIn(){
// 将in的元素倒放到out里面去
while(!stackOut.isEmpty()){ // 要保证in内部的元素,全部放到out里面
stackIn.push(stackOut.pop());
}
}
}
怎么感觉代码变得简单了。
class MyQueue {
public:
stack<int> in;
stack<int> out;
MyQueue() {
}
void push(int x) {
in.push(x);
}
int pop() {
if(out.empty()){
while(!in.empty()){
out.push(in.top());
in.pop();
}
}
int res = out.top();
out.pop();
return res;
}
int peek() {
int res = this->pop();
out.push(res);
return res;
}
bool empty() {
return in.empty() && out.empty();
}
};
代码随想录中对于out没有在倒回in的操作,而是直接对out操作pop或者peek,只要out中有元素就可一直操作,直到out中没有元素了,就将in中的元素都倒过来。
class MyQueue {
// 用两个栈实现队列,
// 一个负责进 一个负责出.(没有必要在每次倒到out之后进行pop.peek操作在倒回来。)
// 这里就是out只管pop和peek,里面有元素也没关系。
// 进的只负责push,另一个负责pop, peek,
Stack<Integer> stackIn;
Stack<Integer> stackOut;
public MyQueue() {
stackIn = new Stack<>();
stackOut = new Stack<>();
}
public void push(int x) {
stackIn.push(x);
}
public int pop() {
stackInToStackOut();
return stackOut.pop();
}
public int peek() {
stackInToStackOut();
return stackOut.peek();
}
public boolean empty() {
if(stackIn.isEmpty() && stackOut.isEmpty()){ //
return true;
}
else{
return false;
}
}
public void stackInToStackOut(){
// 将in的元素倒放到out里面去
if(!stackOut.isEmpty()){ // 如果stackOut不为空,就不作任何操作。此时out里面还有元素,直接对out做pop和peek就可以了。
return;
}
while(!stackIn.isEmpty()){ // 要保证in内部的元素,全部放到out里面
stackOut.push(stackIn.pop());
}
}
}
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/
LeetCode225:用队列实现栈
思路:用一个栈实现。关键点就是在pop和top操作时,先将queue中最后一个元素(将要出战的元素)之前的所有元素先弹出队列,在从后面入队列。此时,队列第一个就是要pop或者peek的元素了。
关键点:熟悉LinkedList, Deque, ArrayDeque和对应的韩式方法的使用。
class MyStack {
// 用一个队列实现栈
// 重点就是pop、peek操作
// 只要将队列最末尾元素(将要出栈的元素)之前的的元素凑队列里出来,然后在从后加入到队列,接着弹出队列第一个元素(将要出栈元素)。
Queue<Integer> queue;
public MyStack() {
queue = new LinkedList<>();
}
public void push(int x) {
queue.add(x);
}
public int pop() {
int size = queue.size();
while(size-1>0){
int temp = queue.poll();
size--;
queue.add(temp);
}
return queue.poll();
}
public int top() {
int size = queue.size();
while(size-1>0){
int temp = queue.poll();
size--;
queue.add(temp);
}
int temp = queue.poll();
queue.add(temp);
return temp;
}
public boolean empty() {
return queue.isEmpty();
}
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/
class MyStack {
public:
queue<int> que;
MyStack() {
}
void push(int x) {
que.push(x);
}
int pop() {
int size = que.size();
while(size>1){
int temp = que.front();
size--;
que.push(temp);
que.pop();
}
int res = que.front();
que.pop();
return res;
}
int top() {
return que.back();
}
bool empty() {
return que.empty();
}
};
代码随想录的区别是:在push的时候就将其放到首位,那么后面pop和top的时候直接弹出首位就可以了。