当前位置:网站首页>【LeetCode】Easy | 232. Using stack to realize queue (pure C manual tearing stack)
【LeetCode】Easy | 232. Using stack to realize queue (pure C manual tearing stack)
2022-06-30 05:16:00 【XiYang-DING】
subject

Ideas
The characteristics of the two structures :① team : Tail insertion + Head deletion ② Stack : Tail insertion + Deletion at the end
The idea of tail insertion : If both stacks are empty , Pick any stackPush; If one of the two stacks is not empty , Then select a stack that is not emptyPush
The idea of header deletion : Found a stack that is not empty --> Before the n − 1 n − 1 n−1 ElementsPushTo empty stack --> Will be the firstnElementsPop--> because push The order to the empty stack is the reverse of the original order, so the remaining elementsPushTo the present empty stack
And questions 【225. Using queue to implement stack 】 The difference is , This needs to be reversed once .
Code
typedef int STDataType;
typedef struct Stack
{
STDataType* a;
int top;
int capacity;
}ST;
typedef struct {
ST s1;
ST s2;
} MyQueue;
// initialization
void StackInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->top = 0;
ps->capacity = 0;
}
// Push
void StackPush(ST* ps, STDataType x)
{
assert(ps);
// Judge whether there is enough space
// The space is insufficient
if (ps->top == ps->capacity)
{
int newCapacity = (ps->capacity == 0) ? 4 : ps->capacity * 2;
STDataType* tmp = realloc(ps->a, sizeof(STDataType) * newCapacity);
// Failed to open up
if (tmp == NULL)
{
printf("realloc fail!");
exit(0);
}
// It's a success
ps->a = tmp;
ps->capacity = newCapacity;
}
ps->a[ps->top] = x;
ps->top++;
}
// Out of the stack
void StackPop(ST* ps)
{
assert(ps);
assert(ps->top > 0);
ps->top--;
}
// Take the top element of the stack
STDataType StackTop(ST* ps)
{
assert(ps);
assert(ps->top > 0);
return ps->a[ps->top - 1];
}
// The size of the stack
int StackSize(ST* ps)
{
assert(ps);
return ps->top;// Attention is from 0 At the beginning
}
// Judge whether the stack is empty
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;//==0 True return true;!=0 Yes false return false
}
// Destruction of the stack
void StackDestroy(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->top = ps->capacity = 0;
}
MyQueue* myQueueCreate() {
MyQueue* mq=(MyQueue*)malloc(sizeof(MyQueue));
StackInit(&mq->s1);
StackInit(&mq->s2);
return mq;
}
void myQueuePush(MyQueue* obj, int x) {
if(StackEmpty(&obj->s1))
{
StackPush(&obj->s2,x);
}
else
{
StackPush(&obj->s1,x);
}
}
int myQueuePop(MyQueue* obj) {
ST *emptyS=&obj->s1;
ST* noEmptyS=&obj->s2;
if(!StackEmpty(emptyS))
{
emptyS=&obj->s2;
noEmptyS=&obj->s1;
}
while(StackSize(noEmptyS)>1)
{
StackPush(emptyS,StackTop(noEmptyS));
StackPop(noEmptyS);
}
int top=StackTop(noEmptyS);
StackPop(noEmptyS);
emptyS=&obj->s1;
noEmptyS=&obj->s2;
if(!StackEmpty(emptyS))
{
emptyS=&obj->s2;
noEmptyS=&obj->s1;
}
while(StackSize(noEmptyS)>0)
{
StackPush(emptyS,StackTop(noEmptyS));
StackPop(noEmptyS);
}
return top;
}
int myQueuePeek(MyQueue* obj) {
ST *emptyS=&obj->s1;
ST* noEmptyS=&obj->s2;
if(!StackEmpty(emptyS))
{
emptyS=&obj->s2;
noEmptyS=&obj->s1;
}
while(StackSize(noEmptyS)>0)
{
StackPush(emptyS,StackTop(noEmptyS));
StackPop(noEmptyS);
}
int top=StackTop(emptyS);
emptyS=&obj->s1;
noEmptyS=&obj->s2;
if(!StackEmpty(emptyS))
{
emptyS=&obj->s2;
noEmptyS=&obj->s1;
}
while(StackSize(noEmptyS)>0)
{
StackPush(emptyS,StackTop(noEmptyS));
StackPop(noEmptyS);
}
return top;
}
bool myQueueEmpty(MyQueue* obj) {
return StackEmpty(&obj->s1)&&StackEmpty(&obj->s2);
}
void myQueueFree(MyQueue* obj) {
StackDestroy(&obj->s1);
StackDestroy(&obj->s2);
free(obj);
}
/** * 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); */
边栏推荐
- Pit of smoothstep node in shadergraph
- Pytorchcnn image recognition and classification model training framework
- Basic operations of Oracle data
- Pytorch的安装以及入门使用
- 003-JS-DOM-Attr-innerText
- Unity Logitech steering wheel access
- Chinese pycharm changed to English pycharm
- VFPBS在IIS下调用EXCEL遇到的Access is denied
- pytorch中常用损失函数总结
- 产生 BUG 测试人员需要自己去分析原因吗?
猜你喜欢
![[vcs+verdi joint simulation] ~ take the counter as an example](/img/fb/214a4e65c53503ecbc38a5e43523cf.png)
[vcs+verdi joint simulation] ~ take the counter as an example

Unity shader flat shadow

Procedural animation -- inverse kinematics of tentacles

Unity scroll view element drag and drop to automatically adsorb centering and card effect

Pytorch的安装以及入门使用

PWN Introduction (2) stack overflow Foundation

终端便捷ssh(免密)连接
![[Motrix] download Baidu cloud files using Motrix](/img/d3/f3d29468367cf5011781f20f27a5c8.jpg)
[Motrix] download Baidu cloud files using Motrix
![[note] usage model tree of the unity resource tree structure virtualizingtreeview](/img/3e/fe5610c797a14554ad735172c3ab54.jpg)
[note] usage model tree of the unity resource tree structure virtualizingtreeview

Unity 3D model operation and UI conflict Scrollview
随机推荐
pytorch中常用损失函数总结
E: Topic focus
Detailed explanation of sorting sort method of JS array
Unrealeengine4 - about uobject's giant pit that is automatically GC garbage collected
Unity- the camera follows the player
Unity limited time use limited trial time and use times
Unity profiler performance analysis
Go Land no tests were Run: FMT cannot be used. Printf () & lt; BUG & gt;
Four methods of unity ugui button binding events
Nestjs introduction and environment construction
Virtual and pure virtual destructions
How can the international trading platform for frying US crude oil guarantee capital security?
Unity project hosting platform plasticscm (learn to use 1)
Singleton mode in unity
Introduction to mmcv common APIs
Unity obtains serial port data
Revit二次开发---未打开项目使用面板功能
Unity3d- use animator and code to control task walking
Passing values between classes using delegates and events
Configuration and use of controllers and routes in nestjs