数据结构 双向栈程序调试出现 program received signal SIGSEGV,Segmentation fault
#include <stdio.h>#include <stdlib.h>
#define Maxsize 10
typedef struct
{
int array[Maxsize];
int righttop;
int lefttop;
}stack;
stack *MakeEmpty(stack *s)//置空双向栈
{
s->lefttop=-1;
s->righttop=Maxsize;
return s;
}
int IsEmpty(stack s)//检测栈是否空
{
return s.lefttop==-1&&s.righttop==Maxsize;
}
int IsFull(stack s)//判断是否满了
{
return s.lefttop+1==s.righttop;
}
void Push(stack s)//入栈
{
int i,a;
printf("输入希望的操作序号:\n1、数组左端进\n2、数组右端进\n");
scanf("%d",&i);
printf("\n输入的元素为:\n");
scanf("%d",&a);
if(IsFull(s))
{
printf("栈满了!");
}
else if(i==1)
{
s.array[++s.lefttop]=a;
}
else if(i==2)
{
s.array[--s.righttop]=a;
}
else
{
printf("输入的序号不对!");
}
}
void Pop(stack s)
{
int i;
printf("输入希望的操作序号:\n1、数组左端出\n2、数组右端出\n");
scanf("%d",&i);
if(IsEmpty(s))
{
printf("空栈!");
}
else if(i==1)
{
s.lefttop--;
}
else if(i==2)
{
s.righttop--;
}
else
{
printf("输入的序号不对!");
}
}
void menu()
{
printf("\n1、进栈\n2、出栈\n");
}
int main()
{
stack s;
int num;
menu();
printf("\n输入希望的操作序号:\n");
scanf("%d",&num);
switch(num)
{
case 1:
Push(s);
break;
case 2:
Pop(s);
break;
default:
printf("序号有误!");
}
system("pause");
return 0;
}
[ 本帖最后由 cxy459508381 于 2014-11-4 19:51 编辑 ]