C 做某事时的奇怪行为

C 做某事时的奇怪行为,c,arrays,stack,C,Arrays,Stack,我试图实现一个堆栈。我想到了这个。除尝试推送外,所有其他功能均按预期工作。当我试着推4时,一些奇怪的事情发生了 #include <stdio.h> #include <stdlib.h> #define MAX 10 typedef struct { int a[MAX]; int top; }stack; void init(stack *p) { p->top=-1; } int full(stack *p) { if(p

我试图实现一个堆栈。我想到了这个。除尝试推送外,所有其他功能均按预期工作。当我试着推4时,一些奇怪的事情发生了

#include <stdio.h>
#include <stdlib.h>
#define MAX 10

typedef struct
{
    int a[MAX];
    int top;
}stack;

void init(stack *p)
{
    p->top=-1;
}

int full(stack *p)
{
    if(p->top==MAX)
        return 1;
    else
        return 0;
}

int empty(stack *p)
{
    if(p->top==-1)
    {
        init(p);
        return 1;
    }
    else
        return 0;
}

void display(stack *p)
{
    if(!empty(p))
    {
        printf("Stack is::\n");
        for(int i=0;i<=p->top;++i)
            printf("%d\t",p->a[i]);
        printf("\n");
    }
    else
    {
        printf("Stack is empty.\n");
        init(p);
    }
}

void push(stack *p, int x)
{
    if(!full(p)) /*full() returns 1 is top==max, 0 otherwise*/
    {
        p->a[p->top++]=x;
        printf("%d pushed.\n",x);
    }
    else
        printf("Stack is full.\n");
}

void pop(stack *p)
{
    if(!empty(p))
        printf("%d popped.\n",p->a[p->top--]);
    else
    {
        printf("Stack is empty.\n");
        init(p);
    }
}

int main()
{
    stack p;
    int ch,x;
    printf("Hello world!\n");
    init(&p);
    printf("*****MENU*****\n");
    do{
        printf("1.Push\n2.Pop\n3.Display\n4.Exit\n");
        printf("Enter your choice:: ");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1:
                printf("Enter element to push:: ");
                scanf("%d",&x);
                push(&p,x);
                break;
            case 2:
                pop(&p);
                break;
            case 3:
                display(&p);
                break;
            case 4:
                exit(1);
        }
    }while(ch!=4);
    return 0;
}
#包括
#包括
#定义最大值10
类型定义结构
{
int a[MAX];
int top;
}堆叠;
void init(堆栈*p)
{
p->top=-1;
}
整数满(堆栈*p)
{
如果(p->top==最大值)
返回1;
其他的
返回0;
}
int空(堆栈*p)
{
如果(p->top==-1)
{
init(p);
返回1;
}
其他的
返回0;
}
无效显示(堆栈*p)
{
如果(!空(p))
{
printf(“堆栈为::\n”);
对于(int i=0;itop;++i)
printf(“%d\t”,p->a[i]);
printf(“\n”);
}
其他的
{
printf(“堆栈为空。\n”);
init(p);
}
}
无效推送(堆栈*p,整数x)
{
如果(!full(p))/*full()返回1为top==max,则返回0*/
{
p->a[p->top++]=x;
printf(“%d已推送。\n”,x);
}
其他的
printf(“堆栈已满。\n”);
}
void pop(堆栈*p)
{
如果(!空(p))
printf(“%d弹出。\n”,p->a[p->top--]);
其他的
{
printf(“堆栈为空。\n”);
init(p);
}
}
int main()
{
堆栈p;
int ch,x;
printf(“你好,世界!\n”);
init&p;
printf(“******菜单******\n”);
做{
printf(“1.Push\n2.Pop\n3.Display\n4.Exit\n”);
printf(“输入您的选择::”;
scanf(“%d”和“ch”);
开关(ch)
{
案例1:
printf(“输入要推送的元素::”;
scanf(“%d”和&x);
推送(&p,x);
打破
案例2:
流行音乐(p&p);
打破
案例3:
显示(&p);
打破
案例4:
出口(1);
}
}而(ch!=4);
返回0;
}
程序终止

我正在用ch(=1)而不是x(=4)测试while循环。那么为什么会发生这种情况呢???

这个函数

void push(stack *p, int x)
{
    if(!full(p)) /*full() returns 1 is top==max, 0 otherwise*/
    {
        p->a[p->top++]=x;
        printf("%d pushed.\n",x);
    }
    else
        printf("Stack is full.\n");
}
这是错误的。top的初始值是-1,因此在本语句中

    p->a[p->top++]=x;
您正试图将该值存储在索引等于
-1
的数组元素中

因此,程序具有未定义的行为

函数可能看起来像

void push( stack *p, int x )
{
    if ( !full( p ) ) /*full() returns 1 if top + 1 == max, 0 otherwise*/
    {
        p->a[++p->top]=x;
             ^^^^^^^^
        printf("%d pushed.\n",x);
    }
    else
    {
        printf("Stack is full.\n");
    }
}
int full( const stack *p )
{
    return p->top + 1 == MAX;
           ^^^^^^^^^^
}
考虑到在这种情况下,函数full应该是这样的

void push( stack *p, int x )
{
    if ( !full( p ) ) /*full() returns 1 if top + 1 == max, 0 otherwise*/
    {
        p->a[++p->top]=x;
             ^^^^^^^^
        printf("%d pushed.\n",x);
    }
    else
    {
        printf("Stack is full.\n");
    }
}
int full( const stack *p )
{
    return p->top + 1 == MAX;
           ^^^^^^^^^^
}
函数为空也可以编写得更简单

int empty( const stack *p )
{
    return p->top == -1;
}
堆栈通常按与输入元素的顺序相反的顺序打印

void display( const stack *p )
{
    if ( !empty( p ) )
    {
        printf( "Stack is::\n" );
        for ( int i = p->top; i != -1; --i )
            printf( "%d\t", p->a[i] );
        printf( "\n" );
    }
    else
    {
        printf( "Stack is empty.\n" );
    }
}

请不要发布截图,而是发布文本。你的电脑长脚跑掉了吗我称之为“奇怪的行为”。据估计,在代码未呈现的部分存在问题。发布了整个代码。
p->top=-1
init
错误。这应该是
p->top=0
p->a[++p->top]=x