目录
栈和队列
队列是先进先出,栈是先进后出。
如图所示:
栈先进后出,如图所示:
栈提供push 和 pop 等等接口,所有元素必须符合先进后出规则
对应的队列的情况是一样的,队列中先进先出的数据结构
leetcode232:用栈实现队列
题目:
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push
、pop
、peek
、empty
):
实现 MyQueue
类:
void push(int x)
将元素 x 推到队列的末尾int pop()
从队列的开头移除并返回元素int peek()
返回队列开头的元素boolean empty()
如果队列为空,返回true
;否则,返回false
说明:
- 你 只能 使用标准的栈操作 —— 也就是只有
push to top
,peek/pop from top
,size
, 和is empty
操作是合法的。 - 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
示例:
示例 1:
输入: ["MyQueue", "push", "push", "peek", "pop", "empty"] [[], [1], [2], [], [], []] 输出: [null, null, null, 1, 1, false] 解释: MyQueue myQueue = new MyQueue(); myQueue.push(1); // queue is: [1] myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue) myQueue.peek(); // return 1 myQueue.pop(); // return 1, queue is [2] myQueue.empty(); // return false
提示:
1 <= x <= 9
- 最多调用
100
次push
、pop
、peek
和empty
- 假设所有操作都是有效的 (例如,一个空的队列不会调用
pop
或者peek
操作)
解题思路:
需要两个栈一个输入栈,一个输出栈
1.定义两个int类型的栈,大小为100,in用来存放数据,out用来输出数据;两个指针intop,outtop分别指向栈顶
2.开辟一个队列的大小空间,将指针intop,outtop初始化为0,返回开辟的队列
3.元素存入第一个栈中,存入后栈顶指针加1
4.若输出栈为空且第一个栈中有元素,将第一个栈中元素复制到第二个栈中,将栈顶元素保存,当outtop>0时,将第二个栈中元素复制到第一个栈中
5.返回输入栈中的栈底元素
6.若栈顶指针均为0,如果进栈和出栈都为空的话,说明模拟的队列为空了
7.将栈顶指针置零
代码实现:
定义两个int类型的栈,大小为100,in用来存放数据,out用来输出数据;两个指针intop,outtop分别指向栈顶
typedef struct {
int intop,outtop;
int in[100],out[100];
} MyQueue;
开辟一个队列的大小空间,将指针intop,outtop初始化为0,返回开辟的队列
MyQueue* myQueueCreate() {
MyQueue* queue=(MyQueue*)malloc(sizeof(MyQueue));
queue->intop=0;
queue->outtop=0;
return queue;
}
元素存入第一个栈中,存入后栈顶指针加1
void myQueuePush(MyQueue* obj, int x) {
obj->in[(obj->intop)++]=x;
}
/*
1.若输出栈为空且当第一个栈中有元素(intop>0时),将第一个栈中元素复制到第二个栈中(out[outtop2++] = in[--intop])
2.将栈顶元素保存
3.当outtop>0时,将第二个栈中元素复制到第一个栈中(in[intop++] = out[--outtop])
*/
int myQueuePop(MyQueue* obj) {
//优化:复制栈顶指针,减少对内存的访问次数
int intop=obj->intop;
int outtop=obj->outtop;
while(outtop==0){
while(intop>0){
obj->out[outtop++]=obj->in[--intop];//将第一个栈中元素复制到第二个栈中
}
}
//将第二个栈中栈顶元素(队列的第一个元素)出栈,并保存
int top=obj->out[--outtop];
//将输出栈中元素放回输入栈中
while(outtop>0){
obj->in[intop++]=obj->out[--outtop];
}
//更新栈顶指针
obj->intop=intop;
obj->outtop=outtop;
//返回队列中第一个元素
return top;
}
//返回输入栈中的栈底元素
int myQueuePeek(MyQueue* obj) {
return obj->in[0];
}
//若栈顶指针均为0,则代表队列为空
bool myQueueEmpty(MyQueue* obj) {
return obj->intop==0&&obj->outtop==0;
}
//将栈顶指针置0
void myQueueFree(MyQueue* obj) {
obj->intop=0;
obj->outtop=0;
}
/**
* Your MyQueue struct will be instantiated and called as such:
* MyQueue* obj = myQueueCreate();
* myQueuePush(obj, x);
* int param_2 = myQueuePop(obj);
* int param_3 = myQueuePeek(obj);
* bool param_4 = myQueueEmpty(obj);
* myQueueFree(obj);
*/
leetcode225:用队列实现栈
题目:
请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push
、top
、pop
和 empty
)。
实现 MyStack
类:
void push(int x)
将元素 x 压入栈顶。int pop()
移除并返回栈顶元素。int top()
返回栈顶元素。boolean empty()
如果栈是空的,返回true
;否则,返回false
。-
注意:
- 你只能使用队列的标准操作 —— 也就是
push to back
、peek/pop from front
、size
和is empty
这些操作。 - 你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
示例:
示例:
输入: ["MyStack", "push", "push", "top", "pop", "empty"] [[], [1], [2], [], [], []] 输出: [null, null, null, 2, 2, false] 解释: MyStack myStack = new MyStack(); myStack.push(1); myStack.push(2); myStack.top(); // 返回 2 myStack.pop(); // 返回 2 myStack.empty(); // 返回 False
提示:
1 <= x <= 9
- 最多调用
100
次push
、pop
、top
和empty
- 每次调用
pop
和top
都保证栈不为空
解题思路:
方法一:两个队列
用两个队列来模拟栈,只不过区别于用栈实现队列,这里没有输入和输出的关系,另一个队列只是在弹出元素时用来备份的
方法二:一个队列
使用两个队列的目的只是在弹出元素时保存除最后一个元素之外的元素,那么用一个队列也是可以实现的,一个队列在模拟栈弹出元素的时候只要将队列头部的元素(除了最后一个元素外) 重新添加到队列尾部,此时在去弹出元素就是栈的顺序了。
代码实现:
两个队列
// 使用两个队列实现栈
// 使用两个数组(队列)和四个指针定义栈,指针分别指向对应的队首和队尾
typedef struct {
int queue1[100],queue2[100];
int front1,front2;
int rear1,rear2;
} MyStack;
// 开辟一个栈
MyStack* myStackCreate() {
MyStack* stack=(MyStack*)malloc(sizeof(MyStack));
stack->front1=0; stack->front2=0;
stack->rear1=0; stack->rear2=0;
return stack;
}
// 将元素存入队列中,存入后队尾指针 +1
void myStackPush(MyStack* obj, int x) {
obj->queue1[(obj->rear1)++]=x;
}
int myStackPop(MyStack* obj) {
// 优化:复制指针,减少对内存的访问次数
int front1=obj->front1; int front2=obj->front2;
int rear1=obj->rear1; int rear2=obj->rear2;
// 将 queue1 除最后面的元素以外的所有元素都备份到 queue2
while(rear1-front1>1){
obj->queue2[rear2++]=obj->queue1[front1++];
}
// 弹出 queue1 的最后一个元素并保存
int top=obj->queue1[front1++];
// 将其他元素从 queue2 导回 queue1
while(front2!=rear2){
obj->queue1[rear1++]=obj->queue2[front2++];
}
// 更新队首队尾指针
obj->front1=front1;obj->front2=front2;
obj->rear1=rear1;obj->rear2=rear2;
// 返回栈顶指针
return top;
}
// 直接返回队尾元素(注意队尾是rear-1)
int myStackTop(MyStack* obj) {
return obj->queue1[(obj->rear1)-1];
}
// 若队首队尾指针相等,则队列为空,即栈为空
bool myStackEmpty(MyStack* obj) {
return obj->rear1==obj->front1;
}
// 将队首队尾指针都归 0
void myStackFree(MyStack* obj) {
obj->front1=0;obj->front2=0;
obj->rear1=0;obj->rear2=0;
}
/**
* Your MyStack struct will be instantiated and called as such:
* MyStack* obj = myStackCreate();
* myStackPush(obj, x);
* int param_2 = myStackPop(obj);
* int param_3 = myStackTop(obj);
* bool param_4 = myStackEmpty(obj);
* myStackFree(obj);
*/
一个队列
// 使用一个队列实现栈
// 用一个数组(队列)定义栈,两个指针为队列的队首和队尾指针
typedef struct {
int queue[100];
int front;
int rear;
} MyStack;
// 开辟一个栈
MyStack* myStackCreate() {
MyStack* stack = malloc(sizeof(MyStack));
stack->front = 0;
stack->rear = 0;
return stack;
}
// 将元素存入队列中,存入后队尾指针 +1
void myStackPush(MyStack* obj, int x) {
obj->queue[(obj->rear)++] = x;
}
int myStackPop(MyStack* obj) {
// 优化:复制指针,减少对内存的访问次数
int front = obj->front;
int rear = obj->rear;
// 将队列头部的元素(除了最后一个元素外)重新添加到队列尾部
int size = rear - front;
while (size-- > 1) {
obj->queue[rear++] = obj->queue[front++];
}
// 弹出此时的一个元素(即原来的最后一个元素)并保存
int top = obj->queue[front++];
// 更新队首队尾指针
obj->front = front;
obj->rear = rear;
// 返回栈顶指针
return top;
}
// 直接返回队尾元素
int myStackTop(MyStack* obj) {
return obj->queue[(obj->rear) - 1];
}
// 若队首队尾指针相等,则队列为空,即栈为空
bool myStackEmpty(MyStack* obj) {
return obj->rear == obj->front;
}
// 将队首队尾指针都归 0
void myStackFree(MyStack* obj) {
obj->front = 0;
obj->rear = 0;
}