網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
題目描述
請(qǐng)你僅使用兩個(gè)棧實(shí)現(xiàn)先入先出隊(duì)列。隊(duì)列應(yīng)當(dāng)支持一般隊(duì)列支持的所有操作(push、pop、peek、empty)。
你只能使用標(biāo)準(zhǔn)的棧操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
題目鏈接
用棧實(shí)現(xiàn)隊(duì)列
思路分析
題目的意思是要用兩個(gè)棧來(lái)模擬實(shí)現(xiàn)一個(gè)隊(duì)列。僅可以用棧的基本功能實(shí)現(xiàn)隊(duì)列的基本功能。所以需要?jiǎng)?chuàng)建兩個(gè)棧。所以這兩個(gè)棧st1,st2可用一個(gè)結(jié)構(gòu)體包含。本質(zhì)就是用兩個(gè)后進(jìn)先出的棧,來(lái)模擬一個(gè)先進(jìn)先出的隊(duì)列。
思路:
1.st2這個(gè)棧用來(lái)壓棧,st1的作用:把st2的所有值壓到st1中,然后經(jīng)過(guò)st1出棧。這樣就達(dá)到了隊(duì)列先進(jìn)先出的性質(zhì)。
2.st2一直用來(lái)壓棧。如果st1為空則將st2里面的值全都轉(zhuǎn)移到st1,如果st1不為空,則繼續(xù)出棧,知道st1為空為止。
代碼實(shí)現(xiàn)
ypedef char STDataType; typedef struct Stack { STDataType* a; int top; int capacity; }ST; //初始化結(jié)構(gòu)體 void StackInit(ST* ps); //銷(xiāo)毀結(jié)構(gòu)體 void StackDestroy(ST* ps); //壓棧 void StackPush(ST* ps, STDataType x); //出棧 void StackPop(ST* ps); //得到棧頂?shù)闹? STDataType StackTop(ST* ps); //判斷棧是否為空 bool StackEmpty(ST* ps); //得到棧的長(zhǎng)度 int StackSize(ST* ps); //初始化結(jié)構(gòu)體 void StackInit(ST* ps) { assert(ps); ps->a = NULL; ps->capacity = 0; ps->top = 0; } //銷(xiāo)毀結(jié)構(gòu)體 void StackDestroy(ST* ps) { assert(ps); free(ps->a); ps->a = NULL; ps->capacity = 0; ps->top = 0; } //壓棧 void StackPush(ST* ps, STDataType x) { assert(ps); if (ps->top == ps->capacity) { int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2; STDataType* new = (STDataType*)realloc(ps->a, sizeof(STDataType) * newcapacity); if (new == NULL) { printf("realloc fail\n"); exit(-1); } ps->a = new; ps->capacity = newcapacity; } ps->a[ps->top] = x; ps->top++; } void StackPop(ST* ps) { assert(ps); assert(ps->top > 0); ps->top--; } STDataType StackTop(ST* ps) { assert(ps); assert(ps->top>0); return ps->a[ps->top-1]; } bool StackEmpty(ST* ps) { assert(ps); return ps->top == 0; } //得到棧的長(zhǎng)度 int StackSize(ST* ps) { assert(ps); return ps->top; } //創(chuàng)建了兩個(gè)棧 typedef struct { ST st1; ST st2; } MyQueue; //對(duì)兩個(gè)棧進(jìn)行初始化。 MyQueue* myQueueCreate() { MyQueue* newQueue = (MyQueue*)malloc(sizeof(MyQueue)); assert(newQueue); StackInit(&newQueue->st1); StackInit(&newQueue->st2); return newQueue; } void myQueuePush(MyQueue* obj, int x) { assert(obj); StackPush(&obj->st2, x); } int myQueuePop(MyQueue* obj) { assert(obj); if(StackEmpty(&obj->st1)) { while(!StackEmpty(&obj->st2)) { StackPush(&obj->st1, StackTop(&obj->st2)); StackPop(&obj->st2); } } int top = 0; if(!StackEmpty(&obj->st1)) { top = StackTop(&obj->st1); StackPop(&obj->st1); } return top; } int myQueuePeek(MyQueue* obj) { assert(obj); if(StackEmpty(&obj->st1)) { while(!StackEmpty(&obj->st2)) { StackPush(&obj->st1, StackTop(&obj->st2)); StackPop(&obj->st2); } } if(!StackEmpty(&obj->st1)) { return StackTop(&obj->st1); } return 0; } bool myQueueEmpty(MyQueue* obj) { return StackEmpty(&obj->st1) && StackEmpty(&obj->st2); } void myQueueFree(MyQueue* obj) { StackDestroy(&obj->st1); StackDestroy(&obj->st2); free(obj); }
原文鏈接:https://blog.csdn.net/qq2466200050/article/details/123843960
相關(guān)推薦
- 2022-06-06 HTTP中的請(qǐng)求頭和響應(yīng)頭各字段屬性解釋及狀態(tài)碼
- 2022-10-04 Android系統(tǒng)優(yōu)化Ninja加快編譯_Android
- 2022-07-09 利用go語(yǔ)言判斷是否是完全二叉樹(shù)_Golang
- 2022-04-16 C++順序表的基本操作實(shí)現(xiàn)_C 語(yǔ)言
- 2022-09-16 Pandas數(shù)據(jù)形狀df.shape的實(shí)現(xiàn)_python
- 2023-07-24 vxe-grid實(shí)現(xiàn) 二維數(shù)據(jù)聯(lián)動(dòng)
- 2022-06-17 Go語(yǔ)言使用Request,Response處理web頁(yè)面請(qǐng)求_Golang
- 2022-05-20 ElasticSearch 7.X系列之:Centos7中常見(jiàn)啟動(dòng)報(bào)錯(cuò)以及解決方法
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支