简体   繁体   English

do while的奇怪行为

[英]Strange behaviour of do while

I was trying to implement a stack. 我试图实现一个堆栈。 I came up with this. 我想到了这个。 All the other functions work as expected except when is try to push. 其他所有功能均按预期工作,但尝试推送时除外。 When i try to push 4 some thing strange happens. 当我尝试推动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;
}

The program terminates. 程序终止。

I am testing the while loop with ch(=1) and not x(=4). 我正在用ch(= 1)而不是x(= 4)测试while循环。 So why is this happening?? 那么为什么会这样呢?

This function 该功能

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");
}

is wrong. 是错的。 The initial value of top is -1 so in this statement top的初始值为-1,因此在此语句中

    p->a[p->top++]=x;

you are trying to store the value in the element of the array with the index equal to -1 . 您试图将值存储在索引为-1的数组元素中。

Thus the program has undefined behaviour. 因此,该程序具有未定义的行为。

The function could look like 该功能可能看起来像

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");
    }
}

Take into account that in this case function full should look like 考虑到在这种情况下功能满应该看起来像

int full( const stack *p )
{
    return p->top + 1 == MAX;
           ^^^^^^^^^^
}

Function empty also can be written simpler 函数为空也可以写得更简单

int empty( const stack *p )
{
    return p->top == -1;
}

And a stack usually is printed in the reverse order relative to the order of entering elements 通常,堆栈相对于输入元素的顺序以相反的顺序打印

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" );
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM