博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
_DataStructure_C_Impl:顺序栈
阅读量:4662 次
发布时间:2019-06-09

本文共 2058 字,大约阅读时间需要 6 分钟。

// _DataStructure_C_Impl:顺序栈#include
#include
#define StackSize 100typedef char DataType;typedef struct{ DataType stack[StackSize]; int top;}SeqStack;//将栈初始化为空栈只需要把栈顶指针top置为void InitStack(SeqStack *S){ S->top=0;//把栈顶指针置为0}//判断栈是否为空,栈为空返回1,否则返回0int StackEmpty(SeqStack S){ if(S.top==0) return 1; else return 0;}//取栈顶元素。将栈顶元素值返回给e,并返回1表示成功;否则返回0表示失败。int GetTop(SeqStack S,DataType *e){ if(S.top<=0){ //在取栈顶元素之前,判断栈是否为空 printf("栈已经空!\n"); return 0; }else{ *e=S.stack[S.top-1]; //在取栈顶元素 return 1; }}//将元素e进栈,元素进栈成功返回1,否则返回0int PushStack(SeqStack *S,DataType e){ if(S->top>=StackSize){ //在元素进栈前,判断是否栈已经满 printf("栈已满,不能进栈!\n"); return 0; }else{ S->stack[S->top]=e; //元素e进栈 S->top++; //修改栈顶指针 return 1; }}//出栈操作。将栈顶元素出栈,并将其赋值给e。出栈成功返回1,否则返回0int PopStack(SeqStack *S,DataType *e){ if(S->top<=0){ //元素出栈之前,判断栈是否为空 printf("栈已经没有元素,不能出栈!\n"); return 0; }else{ S->top--; //先修改栈顶指针,即出栈 *e=S->stack[S->top]; //将出栈元素赋值给e return 1; }}//求栈的长度,即栈中元素个数,栈顶指针的值就等于栈中元素的个数int StackLength(SeqStack S){ return S.top;}//清空栈的操作void ClearStack(SeqStack *S){ S->top=0;}//********************void main_SeqStack(){ SeqStack S; int i; DataType a[]={'a','b','c','d','e'}; DataType e; InitStack(&S); for(i=0;i
=0;i--) printf("%c",a[i]); printf("\n"); ClearStack(&S); system("pause");}//***************************int fact(int n){ int f,i; f=1; for(i=1;i<=n;i++) f=f*i; return f;}#define Max 100int fact_Stack(int n){ int s[Max][2],top=-1; top++; s[top][0]=n; s[top][1]=0; do{ if(s[top][0]==1) s[top][1]=1; if(s[top][0]>1&&s[top][1]==0){ top++; s[top][0]=s[top-1][0]-1; s[top][1]=0; } if(s[top][1]!=0){ s[top-1][1]=s[top][1]*s[top-1][0]; top--; } }while(top>0); return s[0][1];}void main_fact(){ int f,n; printf("请输入一个正整数(n<15):"); scanf("%d",&n); printf("递归实现n的阶乘:"); f=fact(n); printf("n!=%4d\n",f); f=fact_Stack(n); printf("利用栈非递归实现n的阶乘:"); printf("n!=%4d\n",f); system("pause");}//============void main(){ main_SeqStack(); main_LineEdit(); main_fact();}

转载于:https://www.cnblogs.com/javafly/p/6037158.html

你可能感兴趣的文章
[群内模拟4.8] 定点爆破 后宫着♂火 签到题
查看>>
黑马程序员--java基础加强之类加载器
查看>>
自我介绍
查看>>
[SCSS] Reuse Styles with the SCSS @mixin Directive
查看>>
4. Add override methods to class
查看>>
直播视频插件--sewise player
查看>>
Two Sum
查看>>
spring boot 启动原理详细解析
查看>>
ltp执行过程总结
查看>>
10套免费的响应式布局 Bootstrap 模版
查看>>
【Zookeeper】windows环境下zookeeper安装
查看>>
金额计算
查看>>
Tomcat 性能优化之APR插件安装 -- [转]
查看>>
字符串操作、文件操作,英文词频统计预处理
查看>>
web学习篇之http协议
查看>>
MVC4中使用Html.DropDownList实现级联
查看>>
线程的启动的两种方法,Runnable接口,run()的调用
查看>>
获取华为OID
查看>>
gitbook serve命令找不到fontsettings.js
查看>>
20145321 《网络对抗技术》 网络欺诈技术防范
查看>>