(C语言)栈实现队列元素逆置-洋葱先生-杨少通
发布日期:2021-10-03 07:58:42
浏览次数:2
分类:技术文章
本文共 2598 字,大约阅读时间需要 8 分钟。
注:本程序由Visual Studio 2015编写,与VC++6.0稍有区别,复制到VC++6.0注释掉“#include “stdafx.h””即可运行,复制到VS可直接运行。
#include "stdafx.h"#include#include using namespace std;#define OK 1#define ERROR 0#define OVERFLOW -1#define UNDERFLOW -2#define STACK_INIT_SIZE 80#define STACKINCREMENT 10typedef int status;#define ElemType chartypedef struct { ElemType *base; ElemType *top; int stacksize;}SqStack;SqStack S;#define MAXQSIZE 80typedef struct { ElemType *base; int front; int rear;}SqQueue;SqQueue Q;status InitStack(SqStack &S) //初始化栈{ S.base = (ElemType*)malloc(STACK_INIT_SIZE * sizeof(ElemType)); if (!S.base)exit(OVERFLOW); S.top = S.base; S.stacksize = STACK_INIT_SIZE; return OK;}status InitQueue(SqQueue &Q) { //初始化队列 Q.base = (ElemType*)malloc(MAXQSIZE * sizeof(ElemType)); if (!Q.base)exit(OVERFLOW); Q.front = Q.rear = 0; return OK;}status Push(SqStack &S, ElemType e) { //入栈 if (S.top-S.base == S.stacksize) { S.base = (ElemType*)realloc(S.base, (S.stacksize + STACKINCREMENT) * sizeof(ElemType)); if (!S.base)exit(OVERFLOW); S.top = S.base + S.stacksize; S.stacksize += STACKINCREMENT; } *S.top++ = e; return OK;}status EnQueue(SqQueue &Q, ElemType e) { //入队 if ((Q.rear + 1) % MAXQSIZE == Q.front) return ERROR;//队满 Q.base[Q.rear] = e; Q.rear = (Q.rear + 1) % MAXQSIZE; return OK;}status Pop(SqStack &S, ElemType &e) { //出栈 if (S.top == S.base)exit(UNDERFLOW); e = *(S.top=S.top-1); return OK;}status DeQueue(SqQueue &Q, ElemType &e) { //出队 if (Q.front == Q.rear)return ERROR; //队空 e = Q.base[Q.front]; Q.front = (Q.front + 1) % MAXQSIZE; return OK;}status StackEmpty(SqStack S) { //是否为空栈 return S.base == S.top;}status QueueEmpty(SqQueue Q) { //是否为空队 return Q.rear == Q.front;}void algo(SqQueue &Q) { //栈实现队的首尾逆置 ElemType e; InitStack(S); cout << " 栈逆置队列前元素如下:"; while (!QueueEmpty(Q)) { DeQueue(Q, e); cout << e; Push(S, e); } cout << endl; while (!StackEmpty(S)) { Pop(S, e); EnQueue(Q, e); }}int main() { InitQueue(Q); ElemType e; int num; cout << "\t\t\t\t*\t\t\t\t\t*"; cout << endl << "\t\t\t\t*\t计科1512-02210151232-杨少通\t*" << endl; cout << "\t\t\t\t*****************************************" << endl << endl; cout << "****************栈逆置队列元素****************" << endl << endl; cout << " 请输入初始化队列元素个数:"; cin >> num; for (int i = 0; i < num; i++) { cout << " 请输入第" << i + 1 << "个元素:"; cin >> e; EnQueue(Q, e); } algo(Q); cout << " 栈逆置队列后元素如下:"; while (!QueueEmpty(Q)) { DeQueue(Q, e); cout << e; } cout << endl << endl; return 0;}
如有转载请注明来源: www.dreamload.cn/blog/?p=251&preview=true (洋葱先生)
转载地址:https://blog.csdn.net/Kindergarten_Sir/article/details/108587478 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
第一次来,支持一个
[***.219.124.196]2024年04月22日 04时08分23秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Linux Android 常用shell命令汇总 不断更新
2019-04-27
触摸屏响应速度性能分析
2019-04-27
gcc c语言编译流程
2019-04-27
串口UART串行总线协议
2019-04-27
写给准备参加秋招的学弟学妹们~一定要来看哦~
2019-04-27
写给过得很辛苦很迷茫的你~一定要看啊
2019-04-27
排序算法时间复杂度、空间复杂度、稳定性比较
2019-04-27
I2C总线传输协议
2019-04-27
SPI协议详解
2019-04-27
## __VA_ARGS__ ... 宏和可变参数
2019-04-27
我的师傅是风清扬
2019-04-27
printf 宏 调试技巧
2019-04-27
Linux 内核自旋锁
2019-04-27
Secure CRT 自动记录日志
2019-04-27
recovery相关的FAQ总结
2019-04-27
C 实现 删除字符串空白符的函数 strtrim
2019-04-27
第一届 C语言比赛
2019-04-27
Unicode 和 UTF-8 有什么区别?
2019-04-27
C语言测试n的阶乘和x的n次方
2019-04-27
Android 充电LED控制
2019-04-27