当前位置:网站首页>Implement a queue with two stacks [C language]
Implement a queue with two stacks [C language]
2022-07-28 10:32:00 【Hard work Zhang Zhang】
Problem description :
Consider using two stacks to implement a special structure like queues
Problem analysis :
We rely on two stacks to realize the queue , It must be used to store the data of joining the team , One is used to get out of the stack , Here we mainly focus on these issues :
- When can the queue leave ?
- When the queue is empty ?
- When can you join the queue ?
- When is the queue full ?
Understanding these problems can easily solve the problem
Here's the answer ( This is not a pure code description , Just explain the problem ):
Implementation code :
Here is the definition and implementation of stack , And the specific code of realizing the queue with two stacks :
// Stack structure definition and method declaration
#define INITSIZE 5
typedef int ElemType;
typedef struct Stack
{
ElemType data[INITSIZE];
int top;
}Stack;
void InitStack(Stack *st, int init_size);
bool Empty(Stack *st);
bool Push(Stack *st, ElemType value);
bool Pop(Stack *st);
bool Top(Stack *st, ElemType *reval);
void DestroyStack(Stack *st);
// Method implementation of stack
#include "stack.h"
#include <stdio.h>
#include <stdlib.h>
void InitStack(Stack *st, int init_size)
{
if(st==NULL) exit(0);
st->top =0;
}
bool Empty(Stack *st)
{
if(st==NULL) exit(0);
return st->top ==0;
}
bool Full(Stack *st)
{
if(st==NULL) exit(0);
return st->top ==INITSIZE;
}
bool Push(Stack *st, ElemType value)
{
if(st==NULL) exit(0);
if(Full(st)) return false;
st->data [st->top ++]=value;
return true;
}
bool Pop(Stack *st)
{
if(st==NULL) exit(0);
if(Empty(st)) return false;
st->top --;
return true;
}
bool Top(Stack *st, ElemType *reval)
{
if(st==NULL) exit(0);
*reval=st->data[st->top];
return true;
}
void DestroyStack(Stack *st)
{
if(st==NULL) exit(0);
while(!Empty(st))
{
Pop(st);
}
}
// Queue structure definition and method implementation
typedef struct Queue
{
Stack enter;
Stack out;
}que;
void Init_que(que *q)
{
if(q==NULL) exit(0);
InitStack(&(q->enter),10); // Initialize a stack to store queued elements
InitStack(&(q->out),10); // Initialize a stack to queue out
}
bool Empty_que(que *q)
{
if(q==NULL) exit(0);
if(Empty(&q->enter) && Empty(&q->out))
return true;
else
return false;
}
bool Full_que(que *q)
{
if(q==NULL) exit(0);
if(!Empty(&q->out) && Full(&q->enter))
return true;
else
return false;
}
bool Push_que(que *q,ElemType value)
{
if(q==NULL) exit(0);
if(Full_que(q)) return false;// The team is full
if(Full(&q->enter))
{
ElemType val;
while(!Empty(&q->enter))
{
Top(&q->enter,&val);
Pop(&q->enter);
Push(&q->out,val);
}
}
Push(&q->enter,value);
return true;
}
static bool Pop_que(que *q)
{
if(q==NULL) exit(0);
if(Empty_que(q)) return false;// Team space
if(Empty(&q->out))
{
ElemType val;
while(!Empty(&q->enter))
{
Top(&q->enter,&val);
Pop(&q->enter);
Push(&q->out,val);
}
}
Pop(&q->out);
return true;
}
static bool Top_que(que *q,ElemType *revalue)
{
if(q==NULL) exit(0);
if(Empty_que(q)) return false;
if(Empty(&q->out))
{
ElemType val;
while(!Empty(&q->enter))
{
Top(&q->enter,&val);
Pop(&q->enter);
Push(&q->out,val);
}
}
Top(&q->out,revalue);
return true;
}
static void Destory_que(que *q)
{
if(q==NULL) exit(0);
DestroyStack(&q->enter);
DestroyStack(&q->out);
}
边栏推荐
- 5. Dynamic programming -- Fibonacci series
- 胡润发布2020中国芯片设计10强民营企业:华为海思竟然没有上榜!
- PHP generates QR code (learning)
- 逆元&组合数&快速幂
- 12. Double pointer -- merge two ordered linked lists
- 14. Double pointer - the container that holds the most water
- Read write separation standby backup error
- 漏洞分析丨HEVD-0x8.IntegerOverflow[win7x86]
- 简介
- [cloud based co creation] Huawei cloud: metastudio digital content production line, which seamlessly integrates the virtual world with the real world
猜你喜欢

Aqua Data Studio 18.5.0导出insert语句

ACM寒假集训#6

多线程与高并发(三)—— 源码解析 AQS 原理

11、链表反转

Record a parent-child project in idea, modify the name of project and module, and test it personally!

SQL Server 2016 学习记录 --- 数据定义

django-celery-redis异步发邮件

【微信小程序】项目实战—抽签应用

机器学习--手写英文字母2--导入与处理数据

7. Dichotomy -- find a set of repeated or ordered but rotating arrays
随机推荐
Add new startup logo and startup / shutdown animation in mt6735
Netease written test No. 2 -- typical application of European distance
配置树莓派,过程和遇到问题
利用正则表达式从文件路径中匹配文件名
(1) Summary of machine learning concepts
Aqua Data Studio 18.5.0导出insert语句
django-celery-redis异步发邮件
数据库安全 --- 创建登录名 用户+配置权限【笔记】
SQL Server 2016 学习记录 --- 数据定义
10. The penultimate node in the linked list
机器学习--手写英文字母2--导入与处理数据
SQL Server 2016 学习记录 --- 数据更新
Qt生成.exe文件 并 在无Qt环境下运行(Enigma Virtual Box进行绿色可执行软件封装)图文教程
第11届蓝桥杯本科组校赛(20200321)
14、双指针——盛最多水的容器
SQL Server 2016学习记录 --- 连接查询
Uni app advanced creation component / native rendering
Sleeping barber problem
Database security - create login user + configure permissions [notes]
UEditor V1.4.3控制文件压缩