Nabstar
Nabstar

Reputation: 43

push operation fails in c stack data structure

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

Upvotes: 1

Views: 336

Answers (1)

cdlane
cdlane

Reputation: 41872

Specific issues: missing break statement on case 4:; inconsistent use of fflush(stdin) when fpurge(stdin) seems to make more sense; using success exit code on failure and printing fatal errors to stdout instead of stderr; not clear what location in peep() represents relative to stack, should be documented;

I didn't like your basic design (all stack operations have void return value and no arguments) so I redid it such that the stack operations are functional and the I/O to get values into or out of them is handled external to the routines in the switch statement in 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");
    }
}

Lots more error checking can, and should be done, this is not finished code.

Upvotes: 1

Related Questions