共享栈

上传人:简****9 文档编号:101963173 上传时间:2019-09-30 格式:DOC 页数:6 大小:89KB
返回 下载 相关 举报
共享栈_第1页
第1页 / 共6页
共享栈_第2页
第2页 / 共6页
共享栈_第3页
第3页 / 共6页
共享栈_第4页
第4页 / 共6页
共享栈_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《共享栈》由会员分享,可在线阅读,更多相关《共享栈(6页珍藏版)》请在金锄头文库上搜索。

1、#include#include#include#define TRUE 1#define FALSE 0#define MAXSIZE 10 /* 共享栈中最大元素个数,应根据需要定义 */typedef int DataType; /* 共享栈中元素类型,应根据需要定义,暂定为整型 */typedef struct DSeqStack /* 共享栈类型定义 */DataType dataMAXSIZE; int LeftTop,RightTop; /* 左右栈顶位置指示器,整型 */DSeqStack;/* 共享栈初始化,置空栈 */DSeqStack *Init_DSeqStack()

2、DSeqStack *s=_; if (s=NULL) printf(Out of space! n); else _; _; return s;/* 判断s指向的共享栈是否为空栈 */int isEmpty_DSeqStack(DSeqStack *s) if(_) return TRUE;else return FALSE;/* 入栈操作:在栈顶位置压入元素x,status指示入左栈还是右栈 */int Push_DSeqStack(DSeqStack *s, char status,DataType x ) if(_) /共享栈满 printf( Overflow! 元素%d入栈失败,栈

3、满上溢!n,x ); return FALSE; else if (status=L|status=l) _=x; else if (status=R)|status=r) _=x;return TRUE; /* 删除栈顶元素 */int Pop_DSeqStack (DSeqStack *s, char status, DataType *x) if (status=L|status=l) if (_) printf( Underflow! 出栈失败,左栈栈空下溢!n,x ); return FALSE; else *x= _;/printf(左栈栈顶元素%d出栈n,*x); _; retu

4、rn TRUE; else if (status=R|status=r) if (_) printf( Underflow! 出栈失败,右栈栈空下溢!n,x ); return FALSE; else *x= _;/printf(右栈栈顶元素%d出栈n,*x); _;return TRUE; else return FALSE;void Traverse_DSeqStack (DSeqStack *s) int i;if(isEmpty_DSeqStack(s) printf(共享栈为空栈!左栈右栈长度均为0!nn);else if(_) printf(左栈为空栈!长度为0!n);else p

5、rintf(左栈长度为%d,栈顶位置LeftTop=%d,栈顶元素a%d=%dn, _, s-LeftTop, s-LeftTop+1, _);if(_) printf(右栈为空栈!长度为0!n);else printf(右栈长度为%d,栈顶位置RightTop=%d,栈顶元素b%d=%dn, _, s-RightTop, MAXSIZE-s-RightTop, _);printf(栈中元素: );for(i=0;idatai);printf( );for(i= _;idatai);printf(nn);int main() DSeqStack *ps;int n,m,v,i=1;char c

6、hoice,ch,dir;printf(n);while(choice!=Q & choice!=q) /当choice的值不为q且不为Q时循环 printf( * 共 享 栈 常 用 算 法 *n); printf( I.Init E.Push P.Pop T.Traverse Q.Exitn); if(i=1) printf(n注意:请先执行I(Init)一次n); i+; printf(请输入您要操作的步骤:);scanf(%c,&choice);while(ch = getchar() != n & ch != EOF); if(choice=I|choice=i) /初始化顺序栈 p

7、s=Init_DSeqStack(); printf(共享栈初始化成功nn);else if(choice=E|choice=e) do printf(请选择左栈(L)或右栈(R)入栈:); scanf(%c,&dir); while(ch = getchar() != n & ch != EOF); while(dir!=L & dir!=R & dir!=l & dir!=r); printf(请输入要入栈的元素个数:);printf(N=);scanf(%d,&n);printf(请输入要入栈的元素:) ;for(i=1;i LeftTop!= ps-RightTop-1)if (i=1

8、)printf(数据元素) ;if (n=1) Push_DSeqStack(ps, dir,m); printf(%d入栈成功n,m) ; break;if(i LeftTop!= ps-RightTop-2 ) Push_DSeqStack(ps, dir,m); printf(%d,m) ; else Push_DSeqStack(ps, dir,m); printf(%d依次入栈成功n,m) ; else Push_DSeqStack(ps, dir,m); while(ch = getchar() != n & ch != EOF); Traverse_DSeqStack (ps);

9、 else if(choice=P|choice=p) doprintf(请选择左栈(L)或右栈(R)出栈:); scanf(%c,&dir); while(ch = getchar() != n & ch != EOF); while(dir!=L & dir!=R & dir!=l & dir!=r); printf(请输入要出栈的元素个数:);printf(N=);scanf(%d,&n);for(i=1;i LeftTop=0) if (i=1) printf(数据元素) ;if (n=1 ) Pop_DSeqStack (ps,dir,&v); printf(%d出左栈成功n,v) ; break;if(i LeftTop =1) Pop_DSeqStack (ps,dir,&v); printf(%d,v) ; else Pop_DSeqStack (ps,dir,&v); printf(%d依次出左栈成功n,v) ;else if (dir=L | dir=l ) & ps- LeftTop RightTop =MAXSIZE-1) if (i=1) printf(数据元素) ;if (n=1 ) Pop_DSeqStack (ps,dir,&v); printf(%d出右栈成功n,v) ; break;if(i RightTop = MAXSIZE

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 管理学资料

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号