c - c堆栈数据结构中的推送操作失败

标签 c arrays data-structures stack

Hello everyone i got some issues here with the following code actually the code compiles successfully but when i called the display function it outputs one's instead of the actual content of the stack. can anyone explains to me what wrong with the display function.

thank you

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

最佳答案

具体问题:缺少break关于 case 4: 的声明; fflush(stdin) 的使用不一致什么时候fpurge(stdin)似乎更有意义;在失败时使用成功 退出代码并将 fatal error 打印到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");
    }
}

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

关于c - c堆栈数据结构中的推送操作失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39670989/

相关文章:

c - C 中 write(2) 的返回值 0 是错误吗?

c - 如何更改数组的 "geometry"?

c++ - 使用数组的文件输出不正确

java - 列为 Java 中 hashMap 的值

actionscript-3 - 快速确定数据在数组中的位置

c - 为什么 Ctrl-Z 不触发 EOF?

c++ - 原子操作需要硬件支持吗?

c - 如何使用数组索引作为运算符?

arrays - 按嵌套对象数组中的属性过滤对象数组

c - 尝试和后缀树实现