mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
添加 0232.用栈实现队列.md C语言版本
This commit is contained in:
@ -349,6 +349,83 @@ MyQueue.prototype.empty = function() {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
C:
|
||||||
|
```C
|
||||||
|
/*
|
||||||
|
1.两个type为int的数组(栈),大小为100
|
||||||
|
第一个栈stackIn用来存放数据,第二个栈stackOut作为辅助用来输出数据
|
||||||
|
2.两个指针stackInTop和stackOutTop,分别指向栈顶
|
||||||
|
*/
|
||||||
|
typedef struct {
|
||||||
|
int stackInTop, stackOutTop;
|
||||||
|
int stackIn[100], stackOut[100];
|
||||||
|
} MyQueue;
|
||||||
|
|
||||||
|
/*
|
||||||
|
1.开辟一个队列的大小空间
|
||||||
|
2.将指针stackInTop和stackOutTop初始化为0
|
||||||
|
3.返回开辟的队列
|
||||||
|
*/
|
||||||
|
MyQueue* myQueueCreate() {
|
||||||
|
MyQueue* queue = (MyQueue*)malloc(sizeof(MyQueue));
|
||||||
|
queue->stackInTop = 0;
|
||||||
|
queue->stackOutTop = 0;
|
||||||
|
return queue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
将元素存入第一个栈中,存入后栈顶指针+1
|
||||||
|
*/
|
||||||
|
void myQueuePush(MyQueue* obj, int x) {
|
||||||
|
obj->stackIn[(obj->stackInTop)++] = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
1.若输出栈为空且当第一个栈中有元素(stackInTop>0时),将第一个栈中元素复制到第二个栈中(stack2[stackTop2++] = stack1[--stackTop1])
|
||||||
|
2.将栈顶元素保存
|
||||||
|
3.当stackTop2>0时,将第二个栈中元素复制到第一个栈中(stack1[stackTop1++] = stack2[--stackTop2])
|
||||||
|
*/
|
||||||
|
int myQueuePop(MyQueue* obj) {
|
||||||
|
//优化:复制栈顶指针,减少对内存的访问次数
|
||||||
|
int stackInTop = obj->stackInTop;
|
||||||
|
int stackOutTop = obj->stackOutTop;
|
||||||
|
//若输出栈为空
|
||||||
|
if(stackOutTop == 0) {
|
||||||
|
//将第一个栈中元素复制到第二个栈中
|
||||||
|
while(stackInTop > 0) {
|
||||||
|
obj->stackOut[stackOutTop++] = obj->stackIn[--stackInTop];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//将第二个栈中栈顶元素(队列的第一个元素)出栈,并保存
|
||||||
|
int top = obj->stackOut[--stackOutTop];
|
||||||
|
//将输出栈中元素放回输入栈中
|
||||||
|
while(stackOutTop > 0) {
|
||||||
|
obj->stackIn[stackInTop++] = obj->stackOut[--stackOutTop];
|
||||||
|
}
|
||||||
|
//更新栈顶指针
|
||||||
|
obj->stackInTop = stackInTop;
|
||||||
|
obj->stackOutTop = stackOutTop;
|
||||||
|
//返回队列中第一个元素
|
||||||
|
return top;
|
||||||
|
}
|
||||||
|
|
||||||
|
//返回输入栈中的栈底元素
|
||||||
|
int myQueuePeek(MyQueue* obj) {
|
||||||
|
return obj->stackIn[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
//若栈顶指针均为0,则代表队列为空
|
||||||
|
bool myQueueEmpty(MyQueue* obj) {
|
||||||
|
return obj->stackInTop == 0 && obj->stackOutTop == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
//将栈顶指针置0
|
||||||
|
void myQueueFree(MyQueue* obj) {
|
||||||
|
obj->stackInTop = 0;
|
||||||
|
obj->stackOutTop = 0;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
Reference in New Issue
Block a user