我正在创建一个堆栈。但是,当尝试将数据推送到堆栈中时,它会遇到运行时崩溃。你能解释为什么会发生这种情况并为我提供正确的代码吗?这是我的程序-
#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("nn 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;
}
}
在 Push 元素的情况下,您正在创建 s
的一个本地实例,这与您在循环之前创建的 s 混淆do..while
。
将开关大小写 3 中的代码更改为
s=push(s,data); //(remove struct ArrayStack *)
你在IsStackEmpty()
if
测试注定要失败。跟
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
永远不会被推送,因此您可以将错误条件与正常流区分开来。否则,您将不得不找到其他方法来报告错误。
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() 函数,因此它因访问冲突而崩溃,使用已经在主程序中使用 Create_Stack() 初始化的 ArrayStack s
struct ArrayStack *s=create_stack();