我正在创建一个堆栈。但是当试图将数据推入堆栈时,它会发生运行时崩溃。您能解释为什么会发生这种情况并为我提供正确的代码吗?这是我的程序 -
#include<stdio.h>
#include<stdlib.h>
struct ArrayStack* create_stack(void);
int IsStackFull(struct ArrayStack *);
int IsStackEmpty(struct ArrayStack *);
struct ArrayStack * push(struct ArrayStack *,int );
struct ArrayStack
{
int capacity;
int top;
int *array;
};
int main()
{
int choice1=0;
int choice=0,data=0,var=0;
struct ArrayStack *s=create_stack();
printf("\n STACK CREATED");
do
{
printf("\n 1= Test If Stack Is Empty or not");
printf("\n 2= Test If Stack Is Full Or Not");
printf("\n 3= Push Element Into The Stack");
printf("\n 4= Pop Element from the stack");
printf("\n\n Enter yOur choice:: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
var=IsStackEmpty(s);
if(var)
{
printf("\n Yes Stack Is eMpty for Now");
break;
}
else
{
printf("\n No Stack Is Not Empty ");
break;
}
}
case 2:
{
var=IsStackFull(s);
if(var)
{
printf("\n Yes Stack Is Full for Now");
break;
}
else
{
printf("\n No Stack Is Not Full ");
break;
}
}
case 3:
{
printf("\n Provide The Input For stack::");
scanf("%d",&data);
struct ArrayStack *s=push(s,data);
printf("\n Element inerted into the Stack");
break;
}
case 4:
{
var=pop(s);
if(var)
{
printf("\n Removed The element from the Stack");
break;
}
break;
}
default:
{
printf("\n Wrong Input");
}
}
printf("\n Do you want to countinue(1 for yes/0 for no):: ");
scanf("%d",&choice1);
}while(choice1);
}
struct ArrayStack* create_stack()
{
struct ArrayStack *s=(struct ArrayStack *)malloc(sizeof(struct ArrayStack));
if(!s)
{
printf("\nNot enough Memory");
return NULL;
}
s->capacity=4;
s->top=-1;
s->array=(int *)malloc(s->capacity*sizeof(int));
if(!s->array)
{
return NULL;
}
else
return s;
}
int IsStackEmpty(struct ArrayStack *s)
{
if(s->top=-1)
return 1;
else
return 0;
}
int IsStackFull(struct ArrayStack *s)
{
if(s->top==s->capacity-1)
{
return 1;
}
return 0;
}
struct ArrayStack * push(struct ArrayStack *s,int data)
{
if(IsStackFull(s))
{
printf("\n Sorry. The stack is full already");
}
else
{
s->array[++s->top]=data;
printf("\n Element Inserted Successfully");
return s;
}
}
int pop(struct ArrayStack *s)
{
if(IsStackEmpty(s))
{
printf("\n Sorry. Trying to pop element from an empty stack");
return 0;
}
else
{
s->array=s->array[s->top--];
return 1;
}
}
答案 0 :(得分:1)
在Push元素的情况下,您正在创建一个s
的本地实例,这与您在do..while
循环之前创建的s混淆。
将开关案例3中的代码更改为
s=push(s,data); //(remove struct ArrayStack *)
答案 1 :(得分:0)
if
中的IsStackEmpty()
测试注定失败。与
if(s->top=-1)
您要分配而不是比较。这总是评估为true,因此您的IsStackEmpty()
函数总是表示堆栈为空。请改用==
。
您的pop
功能错误。你不想改变malloc
返回的指针,给定你的结构定义,我相信你会想要递减top
,并返回弹出的元素(注意你永远不会返回弹出的元素.. 。)
int pop(struct ArrayStack *s)
{
if(IsStackEmpty(s))
{
printf("\n Sorry. Trying to pop element from an empty stack");
return 0;
}
else
{
return s->top--;
}
}
这假定永远不会推送0
,因此您可以将错误条件与正常流程区分开来。否则,您将不得不找到其他报告错误的方法。
答案 2 :(得分:0)
case 3:
{
printf("\n Provide The Input For stack::");
scanf("%d",&data);
struct ArrayStack *s=push(s,data);
printf("\n Element inerted into the Stack");
break;
}
在上面的代码中,ArrayStack s 未初始化并且在IsStackEmpty()函数中使用,因此它会因访问冲突而崩溃,使用已经初始化的ArrayStack s 主程序使用Create_Stack()
struct ArrayStack *s=create_stack();