实现顺序栈各种基本运算
2014-04-15 10:51
218 查看
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 20
#define ElemType int
typedef struct SqStack
{
ElemType elem[MaxSize];
int top;
}SqStack;
void Init_SqStack(SqStack *s)
{s->top=-1;}
void Push(SqStack *s,ElemType x)
{
if(s->top<MaxSize-1)
{
s->top=s->top+1;
s->elem[s->top]=x;
}
else
printf("栈已满,不能入栈!\n");
}
int Pop(SqStack *s)
{
ElemType x;
if(s->top!=-1)
{x=s->elem[s->top];s->top=s->top-1;
return x;}
else
{printf("栈为空,不能出栈!\n"); return 0;}
}
int Get_Top(SqStack *s,ElemType x)
{
if(s->top!=-1)
{x=s->elem[s->top]; return
1;}
else
{printf("栈为空!\n"); return 0;}
}
void Display_SqStack(SqStack *s)
{
int m;
if(s->top==-1)
printf("顺序栈为空!\n");
else
{
for(m=s->top;m>=0;m--)
printf("-",s->elem[m]);
printf("\n");
}
}
void main()
{
SqStack s;
int x,y,cord;
ElemType a;
Init_SqStack(&s);
Push(&s,1);Push(&s,2);Push(&s,3);Push(&s,4);
printf("初始化\n依次进栈元素为 1 2 3 4:\n");
printf("从栈顶到栈底元素为:\n");
Display_SqStack(&s);
do
{
printf("\n
主菜单
\n");
printf(" 1
入栈
\n");
printf(" 2
出栈
\n");
printf(" 3
读栈顶元素
\n");
printf(" 4
结束程序
\n");
printf("---------------------------------\n");
printf("请输入您选择的菜单号<1, 2, 3, 4>:
");
scanf("%d",&cord);
switch(cord)
{
case 1:
printf("请输入入栈元素!"); scanf("%d",&a);
Push(&s,a);
printf("由栈顶到栈底元素为:");Display_SqStack(&s);
break;
case 2:
x=Pop(&s);
printf("出栈元素为:%d\n",x);
printf("由栈顶到栈底元素为:");Display_SqStack(&s);
break;
case 3:
y=Get_Top(&s,x); printf("栈顶元素为%d:
\n",y);
printf("由栈顶到栈底元素为:");
Display_SqStack(&s);
break;
case 4:
exit(0);break;
default:
printf("输入有误!");
}
}while(cord<=4);
}
#include<stdlib.h>
#define MaxSize 20
#define ElemType int
typedef struct SqStack
{
ElemType elem[MaxSize];
int top;
}SqStack;
void Init_SqStack(SqStack *s)
{s->top=-1;}
void Push(SqStack *s,ElemType x)
{
if(s->top<MaxSize-1)
{
s->top=s->top+1;
s->elem[s->top]=x;
}
else
printf("栈已满,不能入栈!\n");
}
int Pop(SqStack *s)
{
ElemType x;
if(s->top!=-1)
{x=s->elem[s->top];s->top=s->top-1;
return x;}
else
{printf("栈为空,不能出栈!\n"); return 0;}
}
int Get_Top(SqStack *s,ElemType x)
{
if(s->top!=-1)
{x=s->elem[s->top]; return
1;}
else
{printf("栈为空!\n"); return 0;}
}
void Display_SqStack(SqStack *s)
{
int m;
if(s->top==-1)
printf("顺序栈为空!\n");
else
{
for(m=s->top;m>=0;m--)
printf("-",s->elem[m]);
printf("\n");
}
}
void main()
{
SqStack s;
int x,y,cord;
ElemType a;
Init_SqStack(&s);
Push(&s,1);Push(&s,2);Push(&s,3);Push(&s,4);
printf("初始化\n依次进栈元素为 1 2 3 4:\n");
printf("从栈顶到栈底元素为:\n");
Display_SqStack(&s);
do
{
printf("\n
主菜单
\n");
printf(" 1
入栈
\n");
printf(" 2
出栈
\n");
printf(" 3
读栈顶元素
\n");
printf(" 4
结束程序
\n");
printf("---------------------------------\n");
printf("请输入您选择的菜单号<1, 2, 3, 4>:
");
scanf("%d",&cord);
switch(cord)
{
case 1:
printf("请输入入栈元素!"); scanf("%d",&a);
Push(&s,a);
printf("由栈顶到栈底元素为:");Display_SqStack(&s);
break;
case 2:
x=Pop(&s);
printf("出栈元素为:%d\n",x);
printf("由栈顶到栈底元素为:");Display_SqStack(&s);
break;
case 3:
y=Get_Top(&s,x); printf("栈顶元素为%d:
\n",y);
printf("由栈顶到栈底元素为:");
Display_SqStack(&s);
break;
case 4:
exit(0);break;
default:
printf("输入有误!");
}
}while(cord<=4);
}
相关文章推荐
- 实现顺序栈各种基本运算的算法
- 实现环形队列各种基本运算的算法(用顺序结构)
- 编写一个程序,实现顺序表的各种基本运算
- 编写一个程序实现顺序栈的各种基本运算(假设顺序表的元数基本类型为Char)
- 实现顺序队列各种基本运算的算法
- 实现顺序队列各种基本运算的算法
- 实现顺序表各种基本运算算法
- 编写一个程序,实现顺序栈的各种基本运算。
- 实现顺序串各种基本运算的算法
- 3.1实现顺序栈的各种基本运算
- 编写一个程序实现顺序表的各种基本运算(假设顺序表的元数基本类型为Char)
- 实现顺序表各种基本运算的算法
- 实现顺序表的各种基本运算
- 实现顺序栈的各种基本运算的算法
- 实现顺序表的各种基本运算(利用线性表来实现)
- 编写一个程序,实现顺序环形队列的各种基本运算。
- 实现顺序栈的各种基本运算的算法
- 顺序表的应用举例(利用顺序表的基本运算,实现如果在顺序表A中出现的元素,在顺序表B中也出现,则将A中该元素删除)
- 顺序串各种基本运算的算法
- 顺序环形队列的各种基本运算