網站首頁 編程語言 正文
下面舉例代碼:
提到的Q是一個隊列,S是一個空棧,實現將隊列中的元素逆置的算法
#include<stdio.h> #define MaxSize 10 typedef int ElemType; typedef struct{ ?? ?ElemType data[MaxSize]; ?? ?int front,rear; }Queue; typedef struct{ ?? ?ElemType data[MaxSize]; ?? ?int top; }SqStack; ? void InitStack(SqStack &S) //初始化棧 { ?? ?S.top = -1; } ? bool EmptyStack(SqStack S) //判斷棧空 { ?? ?if(S.top == -1) ?? ??? ?return true; ?? ?else ?? ??? ?return false; } ? bool OverflowStack(SqStack S) //判斷棧是否滿 { ?? ?if(S.top == MaxSize-1) ?? ??? ?return true; ?? ?else ?? ??? ?return false; } ? bool Push(SqStack &S,ElemType x) //進棧 { ?? ?if(OverflowStack(S)) ?? ??? ?return false; ?? ?S.data[++S.top] = x; ?? ?return true; } ? bool Pop(SqStack &S,ElemType &x) //出棧 { ?? ?if(EmptyStack(S)) ?? ??? ?return false; ?? ?x = S.data[S.top--]; ?? ?return true; } ? void InitQueue(Queue &Q) //初始化隊列 { ?? ?Q.front = Q.rear = 0; } ? bool IsEmpty(Queue Q) //判斷隊列是否為空 { ?? ?if(Q.front == Q.rear) ?? ??? ?return true; ?? ?else ?? ??? ?return false; } ? bool IsOverflow(Queue Q) //判斷隊列是否滿 { ?? ?if((Q.rear+1)%MaxSize == Q.front) ?? ??? ?return true; ?? ?else ?? ??? ?return false; } ? bool EnQueue(Queue &Q,ElemType x) //進隊列 { ?? ?if(IsOverflow(Q)) ?? ??? ?return false; ?? ?Q.data[Q.rear] = x; ?? ?Q.rear = (Q.rear+1)%MaxSize; ?? ?return true; } ? bool DeQueue(Queue &Q,ElemType &x) //出隊列 { ?? ?if(IsEmpty(Q)) ?? ??? ?return false; ?? ?x = Q.data[Q.front]; ?? ?Q.front = (Q.front+1)%MaxSize; ?? ?return true; } ? bool ReverseQueue(Queue &Q)? { ?? ?SqStack S; ?? ?ElemType x; ?? ?InitStack(S); ?? ?while(!IsEmpty(Q)) //當隊列不為空時,將隊列中的元素依次放入棧中 ?? ?{ ?? ??? ?DeQueue(Q,x); ?? ??? ?Push(S,x); ?? ?} ?? ?while(!EmptyStack(S)) //當棧不為空時,再將棧中元素依次放入隊列中 ?? ?{ ?? ??? ?Pop(S,x); ?? ??? ?EnQueue(Q,x); ?? ?} ?? ?return true; } ? void main() { ?? ?Queue Q; ?? ?InitQueue(Q); ?? ?EnQueue(Q,1); ?? ?EnQueue(Q,2); ?? ?EnQueue(Q,3); ?? ?ReverseQueue(Q); ?? ?printf("%d ",Q.data[Q.front]); }
(根據主函數中代碼)演示:
例如一開始隊列中元素為:3 2 1 ->出
1. 將隊列中元素依次放到棧中,此時棧:1 2 3 ->出
2. 再將棧中的元素依次放入隊列中,此時隊列:1 2 3 ->出?
原文鏈接:https://blog.csdn.net/qq_61706112/article/details/120231560
相關推薦
- 2022-09-19 C語言中switch語句基本用法實例_C 語言
- 2022-04-18 css實現 快速定位父元素下最后面的幾個子元素,匹配選擇最后幾個子元素
- 2022-07-10 python日志管理loguru模塊實操
- 2022-12-04 WxPython中控件隱藏與顯示的小技巧_python
- 2022-07-11 Android?studio實現單選按鈕_Android
- 2022-11-20 Android類加載流程分析_Android
- 2022-06-15 django?settings.py配置文件的詳細介紹_python
- 2024-03-21 SpringBoot +MyBatis批量插入數據
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支