c实现顺序栈
2013-10-31 22:53
260 查看
// Stack.cpp : Defines the entry point for the console application. // #include "stdafx.h" typedef int SElemType; #define INC_SIZE 10 #define INIT_SIZE 10 ////栈的原理与实现 //栈(stack) 是只允许在表一端进行插入和删除的线性表。允许插入和删除的一端叫栈顶,而不允许插入和删除的另一端叫栈底 //所以栈是(LAST IN First Out) LIFO 的结构 // //栈根据存储分两种结构,顺序栈和链式栈 //顺序栈又分为:静态顺序栈和动态顺序栈 //静态栈存储结构 //1:静态顺序栈存储结构 /* typedef struct{ SElemType ele[SIZE]; int top; }SeqStack; */ //2:动态顺序栈存储结构: typedef struct { SElemType *elem; int maxSize,top; }SeqStack; //一下已动态栈实现动态栈的接口 //top指示最后加入的元素的存储位置,栈空top=-1;initSize 初始化栈大小,incSize当栈满的时候扩充栈的增加空间大小值 //初始化栈 bool InitStack( SeqStack &s ) { s.elem = new SElemType[INIT_SIZE]; if( s.elem == NULL )return false; s.maxSize = INIT_SIZE; s.top = -1; } //判断栈是否满 bool IsFull( SeqStack &s ) { return ( s.top + 1 == s.maxSize )?true:false; } //满栈就扩容 bool OverflowProcess( SeqStack &s ) { SElemType *newArray = new SElemType[s.maxSize+INC_SIZE]; if( newArray == NULL ) return false; for( int i = 0; i<=s.top; i++) { newArray[i] = s.elem[i]; } s.elem = newArray; s.maxSize += INC_SIZE; return true; } //压栈 bool Push(SeqStack &s, SElemType x ) { if(IsFull(s)) { if(!OverflowProcess(s))return false; } s.elem[++s.top] = x; return true; } //判断是否是空栈 bool IsEmpty( SeqStack &s ) { return (s.top == -1 )?true:false; } //出栈 bool Pop(SeqStack &s,SElemType &x) { if(IsEmpty(s))return false; x = s.elem[s.top--]; return true; } //得到栈顶元素 bool GetTop( SeqStack &s, SElemType &x ) { if( IsEmpty(s)) return false; x = s.elem[s.top]; return true; } int _tmain(int argc, _TCHAR* argv[]) { SeqStack s; InitStack(s); int i = 1; for(;i<30;i++) { Push(s,i); } SElemType popx; for(i--;i != 0; i-- ) { Pop(s,popx); printf("出栈第%d=%d\n",i,popx); } getchar(); return 0; }
相关文章推荐
- Cocos2d-x学习之---自定义图标(带触摸事件)
- 结构化预测(structured prediction)
- vc++/mfc socket网络通信多客户端和服务端安全问题,服务端只处理授权客户端
- No JNI_OnLoad问题解决
- 那些年用过的UI开发平台
- 2013第44周四目標優先
- Cocos2d-x学习之---自定义图标(带触摸事件)
- 微信不是淘宝杀手 腾讯难满足冲动性消费
- 去除UITableView多余分割线
- pthread多线程编程-1
- 如何给pdf文件添加目录/书签
- iMagic,开启属于你的魔灯时刻
- Hadoop跟Spark之间的持续整合
- MySQL DBA 招聘小结
- uva 10274 Fans and Gems
- SQL按照日、周、月、年统计数据的方法分享
- 20款精致的长阴影 LOGO 设计【附免费生成工具】
- Sqlserver链接
- Display the password in plaintext
- 05-EasyUI_登陆的实现2-Form控件的使用