One-day-one-program(7)之顺序栈的实现
来源:互联网 发布:个体工商户域名备案 编辑:程序博客网 时间:2024/05/18 02:58
栈是一种特殊的线性表,它限定了线性表的插入、删除操作只能在线性表的一端进行这一端叫做栈顶。
栈的基本运算:
(1)栈初始化:Init_stack(s)
(2)判栈空:Empty_stack(s)
(3)入栈(压栈):Push_stack(s,x)
(4)出栈(弹出):Pop_stack(s)
(5)读栈顶元素:Top_stack(s)
#include<stdio.h>#include<stdlib.h>#define MAXSIZE 100typedef struct{int data[MAXSIZE];int top;}SeqStack;SeqStack* Init_SeqStack()//置空栈{SeqStack *S;S=(SeqStack*)malloc(sizeof(SeqStack));S->top=-1;return S;}int Push_SeqStack(SeqStack *S,int x)//入栈{if(S->top==MAXSIZE-1)return 0;else{S->top++;S->data[S->top]=x;return 1;}}int Empty_SeqStack(SeqStack *S)//判空栈{if(S->top==-1)return 1;elsereturn 0;}int Pop_SeqStack(SeqStack *S,int *x)//出栈{if(Empty_SeqStack(S)==1)return 0;else{*x=S->data[S->top];S->top--;return 1;}}int Top_SeqStack(SeqStack *S)//读栈顶元素{if(Empty_SeqStack(S)==1)return -1;elsereturn(S->data[S->top]);}void main(){SeqStack *S;S=Init_SeqStack();int x;int r1,r2;int i;int a;printf("请输入你要存入的数据:\n");scanf("%d",&x);while(x!=-1){r1=Push_SeqStack(S,x);scanf("%d",&x);}printf("此栈的栈顶元素为:\n");r2=Top_SeqStack(S);printf("%d\n",r2);printf("栈中存储的数据为:\n");while(Pop_SeqStack(S,&a)!=0){printf("%d\n",a);}}
0 0
- One-day-one-program(7)之顺序栈的实现
- One-day-one-program(1)之顺序表的实现
- One-day-one-program(4)之链表的实现
- One-day-one-program(5)之冒泡排序
- One-day-one-program(6)之快速排序
- One-day-one-program(2)之1至100之间的素数的求法
- One-day-one-program(3)之统计字符串中不同字母的个数
- One-day-one-program(8)之统计字符串中子串的个数
- One Day One Step 之 nothing
- One Day One Step 之大数阶乘
- One Day One Step 之Milking Cows
- One Day One Step 之Palindromic Squares
- One Day One Step 之Dual Palindromes
- One day, One week!
- O..One..One...Day
- O..One..One...Day
- One day
- ONE DAY
- 异常2error C2664: “sendto”: 不能将参数 2 从“CString”转换为“const char *”
- 用sockaddr_in 而不用sockadd的原因
- 【环境配置】SVN图标不显示问题终极解决方案
- Java接口和Java抽象类
- 求子数组的最大和
- One-day-one-program(7)之顺序栈的实现
- Android应用程序图标大小说明
- 批处理解决Ant自动下载CVS项目并打包
- 获取DataRow[]的值
- ios--OpenCV--标记姿态的模拟
- 截图工具类【】待修改
- performSelector调用方法和直接self 调用方法的区别
- IOS的NSUserDefaults和 android 的SharedPreference
- mysql中的coalesce用法