顺序栈的基本操作
来源:互联网 发布:python idle使用技巧 编辑:程序博客网 时间:2024/05/30 05:22
代码
#include<stdio.h>#include<stdlib.h> #define MAXSIZE 10 typedef struct SqStack{ int data[MAXSIZE]; int top; } SqStack;//打印void printStack(SqStack S){ int i = 0; for(i=0;i<=S.top;i++){ printf("%3d",S.data[i]); } printf("\n"); } //初始化栈 void initStatck(SqStack *S){ S->top = -1 ;} //清空栈 void clearStack(SqStack *S){ S->top = -1;} //出栈 int pop(SqStack *S){ int elem; if(S->top == -1){ printf("error:空栈!\n"); exit(-1); } elem = S->data[S->top]; S->top--; return elem; } //进栈 void push(SqStack *S,int elem){ if(S->top == MAXSIZE-1){ printf("error:栈已满!\n"); exit(-1); } S->data[++S->top] = elem;} //获取栈的长度int stackLength(SqStack S){ return S.top+1;} int main(){ void printStack();//打印 void initStatck(SqStack *); //初始化栈 void clearStack(SqStack *);//清空栈 int pop(SqStack *);//进栈 void push(SqStack * ,int);//出栈 int stackLength(SqStack); SqStack S; int i=0; initStatck(&S); printf("初始化成功!\n"); printf("此时栈的长度为:%3d\n",S.top+1); for(i=0;i<5;i++){ push(&S,i+1); } printStack(S); printf("此时栈的长度为:%3d\n",S.top+1); printf("出栈:%3d\n",pop(&S)); printStack(S); printf("出栈:%3d\n",pop(&S)); printStack(S); printf("此时栈的长度为:%3d\n",S.top+1); printf("清空栈!\n"); clearStack(&S); printf("此时栈的长度为:%3d\n",S.top+1); pop(&S); return 0; }
0 0
- 顺序栈的基本操作
- 顺序栈的基本操作....
- 顺序栈的基本操作
- 顺序栈的基本操作
- 顺序栈的基本操作
- 顺序栈的基本操作
- 顺序栈的基本操作
- 顺序栈的基本操作
- 顺序栈的基本操作
- 顺序栈的基本操作
- 顺序栈的基本操作
- 顺序栈的基本操作
- 顺序栈的基本操作
- 顺序栈的基本操作
- 顺序栈的基本操作
- 顺序栈的基本操作
- 顺序栈的基本操作
- 顺序栈的基本操作
- android开发之PreferenceScreen使用详解
- 关于大型网站技术演进的思考(二十)--网站静态化处理—web前端优化—中(12)
- android新手必看:Android的系统架构详细解析
- android补间动画(Tween)的实现
- 史上最简单,js并获取手机型号
- 顺序栈的基本操作
- 关于大型网站技术演进的思考(二十一)--网站静态化处理—web前端优化—下【终篇】(13)
- Stream,byte[],LZMA
- 提高iOS开发效率的方法和工具
- 编码设置FrameLayout子View的layout_gravity,gravity
- Oracle 定时自动备份(windows任务计划,2016新增linux自动化备份)
- iOS8开发~UI布局(二)storyboard中autolayout和size class的使用详解 f
- Objective-C Runtime 运行时之四:Method Swizzling
- java对象的生命周期及回收