数据结构笔记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;
}
#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;
}
相关文章推荐
- 数据结构笔记2链栈
- 数据结构笔记3双向栈
- 数据结构笔记5带头结点单链表
- 数据结构笔记4双向链表
- 数据结构笔记6链队列
- 黑马程序员——关于数据结构栈的实现方式的探讨
- 数据结构与算法-----冒泡排序
- 数据结构 字符串的长度
- 数据结构之 哈希表
- 数据结构之 哈希表
- 数据结构之 循环表线性表
- 数据结构之 循环表线性表
- 数据结构(Java)——图的基础算法
- 数据结构(Java)——Set和Map的应用
- 【C/C++学院】0828-STL入门与简介/STL容器概念/容器迭代器仿函数算法STL概念例子/栈队列双端队列优先队列/数据结构堆的概念/红黑树容器
- 文章标题
- Linux c 算法与数据结构--双向链表
- “数据结构与算法”有点难
- 数据结构之创建二叉树
- 数据结构之创建二叉树