打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
数据结构(王道版本,主讲人:闲鱼学长)P19-P31

第三章 栈和队列

3.1.1 栈的基本概念





3.1.2 顺序栈的实现




#include <stdio.h>#define MaxSize 10typedef struct {int date[MaxSize];int top;}SqStack;void InitStack(SqStack& S){S.top = -1;}bool StackEmpty(SqStack S){if (S.top == -1){return true;}else{return false;}}bool Push(SqStack& S, int x){printf("请输入要进栈的数:");while(S.top!=MaxSize - 1){scanf_s("%d", &x);S.top = S.top   1;S.date[S.top] = x;}return true;}void print(SqStack& S){for (int i = 0; i <= S.top; i  ){printf("%d ", S.date[i]);}printf("\n");}bool Pop(SqStack& S, int &x){if (S.top == -1){return false;}x = S.date[S.top];S.top = S.top - 1;return true;}void main(){int x = 0;SqStack S;InitStack(S);StackEmpty(S);Push(S, x);printf("进栈的数有:");print(S);Pop(S,x);printf("出栈的栈顶为:%d\n",x);printf("新栈的数有:");print(S);}//请输入要进栈的数:65 43 43 76 65 34 76 23 43 12//进栈的数有 : 65 43 43 76 65 34 76 23 43 12//出栈的栈顶为 : 12//新栈的数有 : 65 43 43 76 65 34 76 23 43

#include <stdio.h>#define MaxSize 10typedef struct {int date[MaxSize];int top;}SqStack;void InitStack(SqStack& S){S.top = 0;}bool StackEmpty(SqStack S){if (S.top == 0){return true;}else{return false;}}bool Push(SqStack& S, int x){printf("请输入要进栈的数:");while(S.top!=MaxSize){scanf_s("%d", &x);S.date[S.top] = x;S.top = S.top   1;}return true;}void print(SqStack& S){for (int i = 0; i < S.top; i  ){printf("%d ", S.date[i]);}printf("\n");}bool Pop(SqStack& S, int &x){if (S.top ==0){return false;}S.top = S.top - 1;x = S.date[S.top];return true;}void main(){int x = 0;SqStack S;InitStack(S);StackEmpty(S);Push(S, x);printf("进栈的数有:");print(S);Pop(S,x);printf("出栈的栈顶为:%d\n",x);printf("新栈的数有:");print(S);}//请输入要进栈的数:65 43 43 76 65 34 76 23 43 12//进栈的数有 : 65 43 43 76 65 34 76 23 43 12//出栈的栈顶为 : 12//新栈的数有 : 65 43 43 76 65 34 76 23 43


3.1.3 链栈的实现




来源:https://www.icode9.com/content-4-862951.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
利用栈实现括号匹配算法!
数据结构项目——顺序栈与链栈
二叉树非递归遍历 - yzhenxiang的日志 - 网易博客
queue stack
栈的进制互换
C语言栈的表示与实现实例详解
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服