您的位置:首页 > 理论基础 > 数据结构算法

数据结构笔记1顺序栈

2015-12-02 16:46 489 查看
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 100
typedef struct{
int data[MAX];
int top;
}Stack;
Stack s;
//初始化 成功1
int InitStack(Stack s){
s.top=-1;
return 1;
}
//判空 非空1
int StackEmpty(Stack s){
if(-1==s.top)
return 0;
return 1;
}
//判满 未满1
int StackFull(Stack s){
if(MAX-1==s.top)
return 0;
return 1;
}
//求长度 成功1输出长度
int StackLength(Stack s){
printf("长度为%d",s.top+1);
return 1;
}
//访问栈顶 返回1输出元素
int GetTop(Stack s,int *e){
e=s.data;
printf("栈顶元素为%d",*e);
return 1;
}
//入栈 成功1
int Push(Stack s,int e){
++s.top;
s.data[s.top]=e;
printf("入栈成功!\n");
return 0;
}
//出栈 成功1
int Pop(Stack s,int *e){
*e=s.data[s.top];
s.top--;
return 1;
}
int main(){
int i,E,*e1,*e2;
Stack L;
printf("现在开始进行站的操作!\n");
if(1!=InitStack(L)){
printf("初始化失败,按任意键退出!\n");
getch();
exit(0);
}
if(1!=StackEmpty(L)){
printf("栈的空间为空!\n");
exit(0);
}
printf("是否进行入栈操作?1-是");
scanf("%d",&i);
if(1==i){
loop:
printf("请输入你要入栈的元素:");
scanf("%d",&E);
if(1!=StackFull(L)){
printf("栈空间已满!\n");
printf("是否进行出栈操作1或者访问栈顶操作2?\n");
printf("请输入你的选择:");
scanf("%d",&i);
if(i==1)
goto pop;
else if(i==2)
goto gettop;
else{
printf("操作失败,按任意键退出!\n");
getch();
exit(0);
}
}
else{
Push(L,E);
printf("是否继续入栈?1-是");
scanf("%d",&i);
if(i==1)
goto loop;
}
}
else{
printf("是否进行出栈操作1或者访问栈顶操作2?\n");
printf("请输入你的选择:");
scanf("%d",&i);
if(i==1)
goto pop;
else if(i==2)
goto gettop;
else{
printf("操作失败,按任意键退出!\n");
getch();
exit(0);
}
}
pop:
Pop(L,e1);
printf("是否继续进行出栈操作?1是");
scanf("%d",&i);
if(1==i)
goto pop;
gettop:
GetTop(L,e2);
printf("顺序栈的操作已经结束!");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: