c堆栈数据结构中的push操作失败

时间:2016-09-23 23:38:24

标签: c arrays data-structures stack

  

大家好我在这里遇到了一些问题以下代码实际上代码编译成功但是当我调用显示功能时它会输出一个而不是堆栈的实际内容。任何人都可以向我解释显示功能有什么问题。

     

谢谢

#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 10

int stack[MAXSIZE];
int top = -1;

int menu();
void push();
void pop();
void peep();
void display();

void main() {
    char ch;
    int item;
    do{
        switch(menu()) {
        case 1:
            push();
        break;
        case 2:
            pop();
        break;
        case 3:
            peep();
        break;
        case 4:
            display();
        default:
            printf("Invalid choice try again\n");
        break;
    }

    printf("Do you want to continue ? (Y/N): ");
    printf("top value is %d", top);
    fflush(stdin);
    scanf("%c", &ch);

    }while(ch == 'Y' || ch == 'y');
}


int menu() {
    int choice;
    printf("Welcome to stack program \n\n");
    printf("\n #1. push");
    printf("\n #2. pop");
    printf("\n #3. peep");
    printf("\n #4. display");
    printf("\nchoice: ");
    scanf("%d", &choice);

    return choice;
}

void push() {
    int item;
    printf("Enter element to add to stack: ");
    item = scanf("%d", &item);
    if(top == MAXSIZE - 1) {
        printf("stack overflow can't add any more item\n");
        exit(0);
    } else {
        top++;
        stack[top] = item;
    } 
}

void pop() {
    if(top == -1) {
        printf("stack underflow deletion not possible\n");
        exit(0);
    } else {
        printf("Element %d is deleted from the stack\n", stack[top]);
        top--;
    }
}

void peep() {
    int i;
    int element;
    printf("Enter the location that you want to peep");
    fflush(stdin);
    scanf("%d", &i);

    if(top - i + 1 < 0) {
        printf("Location not valid");
        exit(0);
    } else {
        element = stack[top - i + 1];
        printf("The location %d contains the element %d \n", i, element);
    }
}

void display() {
    if(top != -1){
        int j;
        printf("Elements in the stack\n");
        for(j = top; j >= 0; j--) {
            printf("%d\n", stack[j]);
        }
    } else {
        printf("Stack is empty\n");
    }
}

1 个答案:

答案 0 :(得分:1)

具体问题:break上缺少case 4:声明;当fflush(stdin)似乎更有意义时,fpurge(stdin)的使用不一致;在失败时使用成功退出代码并将致命错误打印到stdout而不是stderr;不清楚peep()中的位置表示相对于堆栈的情况,应该记录在案;

我不喜欢你的基本设计(所有堆栈操作都有void返回值而没有参数)所以我重新编写它以使堆栈操作正常运行并且I / O可以将值输入或输出它们在switch中的main语句中的例程外部处理:

#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 10

int stack[MAXSIZE];
int top = -1;

int menu();
void push(int item);
int pop();
int peep(int location);
void display();

int main() {
    char ch = 'Y';
    int temporary;

    while (ch == 'Y' || ch == 'y') {

        switch(menu()) {
            case 1:
                printf("Enter element to add to stack: ");
                (void) scanf("%d", &temporary);
                (void) fpurge(stdin);
                push(temporary);
                break;
            case 2:
                temporary = pop();
                printf("Element %d is deleted from the stack\n", temporary);
                break;
            case 3:
                printf("Enter the location that you want to peep: ");
                (void) scanf("%d", &temporary);
                (void) fpurge(stdin);
                printf("The location %d ", temporary);
                temporary = peep(temporary);
                printf("contains the element %d\n", temporary);
                break;
            case 4:
                display();
                break;
            default:
                printf("Invalid choice try again\n");
                break;
        }

        printf("Value of 'top' is %d\n", top);
        printf("Do you want to continue? (Y/N): ");
        (void) scanf("%c", &ch);
        (void) fpurge(stdin);
    }

    return EXIT_SUCCESS;
}

int menu() {
    int choice;

    printf("Welcome to stack program\n");
    printf("\n #1. push");
    printf("\n #2. pop");
    printf("\n #3. peep");
    printf("\n #4. display");
    printf("\nchoice: ");

    (void) scanf("%d", &choice);
    (void) fpurge(stdin);

    return choice;
}

void push(int item) {
    if (top + 1 == MAXSIZE) {
        fprintf(stderr, "stack overflow can't add any more item\n");
        exit(EXIT_FAILURE);
    }

    stack[++top] = item;
}

int pop() {
    if (top == -1) {
        fprintf(stderr, "stack underflow deletion not possible\n");
        exit(EXIT_FAILURE);
    }

    return stack[top--];
 }

int peep(int location) {

    if (top - location + 1 < 0) {
        fprintf(stderr, "Location not valid");
        exit(EXIT_FAILURE);
    }

    int element = stack[top - location + 1];

    return element;
}

void display() {
    if (top != -1) {

        printf("Elements in the stack\n");

        for (int j = top; j > -1; j--) {
            printf("%d\n", stack[j]);
        }
    } else {
        printf("Stack is empty\n");
    }
}

更多的错误检查可以,而且应该完成,这不是完成的代码。