当前位置:网站首页>【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); */
边栏推荐
- Records of some problems encountered during unity development (continuously updated)
- Network communication problem locating steps
- Unity animator does not clip animation to play animation in segments
- Golan no tests were run: fmt Printf() < BUG>
- Pytorch的安装以及入门使用
- Nestjs introduction and environment construction
- Chapter 10 of OpenGL super classic (7th Edition) calculation shader
- Force buckle 209 Minimum length subarray
- Unity3d- use animator and code to control task walking
- VFPBS在IIS下调用EXCEL遇到的Access is denied
猜你喜欢

Introduction to mmcv common APIs

Unityshader learning notes - Basic Attributes

PWN Introduction (2) stack overflow Foundation

Records of problems encountered in unity + hololens development

Force buckle 209 Minimum length subarray

Ugui uses its own function to realize reverse mask

Unity2019.3.8f1 development environment configuration of hololens2

使用码云PublicHoliday项目判断某天是否为工作日

Force buckle 977 Square of ordered array

Unity packaging and publishing webgl error reason exception: failed building webgl player
随机推荐
Unity screenshot method
Redis cluster concept
RedisTemplate 常用方法汇总
C # three ways to obtain web page content
Unity dotween plug-in description
【 VCS + Verdi joint simulation】 ~ Taking Counter as an Example
How to use js to control the scroll bar of moving div
Unityshader learning notes - Basic Attributes
ParticleSystem in the official Manual of unity_ Collision module
Revit Secondary Development - - Project use Panel features not opened
【VCS+Verdi聯合仿真】~ 以計數器為例
Unity obtains serial port data
[typescript] defines the return value type of promise
Unity ontriggerenter does not call
产生 BUG 测试人员需要自己去分析原因吗?
Unity supports the platform # define instruction of script
Database base (Study & review for self use)
Chapter 11 advanced data management of OpenGL super classic (version 7)
Does the tester need to analyze the cause of the bug?
003-JS-DOM-Attr-innerText