Home > OS >  How to do peek operation in stack of array in C?
How to do peek operation in stack of array in C?

Time:11-20

int peek()
{
    if(top == -1)
    {
        printf("\n \t STACK UNDERFLOW");
        return -1;
    }
    else
    {
        if(x != -1)
            printf("\n The top most element is %d",x);
        return x[top];
    }
}

My Peek Operation is not printing the correct output. The Output should be the topmost element in an array of stack, it prints random numbers. The Peek operation should print the topmost element in an array of stack inputted by the user.

For example:

----------------------------------------
         ARRAY OF STACK
----------------------------------------
 [1] PUSH
 [2] POP
 [3] PEEK
 [4] DISPLAY
 [5] EXIT
----------------------------------------
 Enter you Choice: 1
----------------------------------------
Enter element to add: Jennie
----------------------------------------
         ARRAY OF STACK
----------------------------------------
 [1] PUSH
 [2] POP
 [3] PEEK
 [4] DISPLAY
 [5] EXIT
----------------------------------------
 Enter you Choice: 1
----------------------------------------
Enter element to add: Lisa
----------------------------------------
         ARRAY OF STACK
----------------------------------------
 [1] PUSH
 [2] POP
 [3] PEEK
 [4] DISPLAY
 [5] EXIT
----------------------------------------
 Enter you Choice: 3

 The top most element is 4229504

The output should be "The topmost element is Lisa" How to correct this? I need to print the topmost element in the stack inputted by the user. I also get "Warning comparison between pointer and integer" as a warning in "if(x != -1)" in peek operation.

CodePudding user response:

Your stack, a, is an array of strings, and top is the current position:

char *peek() {
    if(top == -1) {
        printf("\n \t STACK UNDERFLOW");
        return NULL;
    }
    printf("\n The top most element is %s", a[top]);
    return a[top];
}

Here is an example run:


----------------------------------------
         ARRAY OF STACK 
----------------------------------------
 [1] PUSH
 [2] POP
 [3] PEEK
 [4] DISPLAY
 [5] EXIT
----------------------------------------
 Enter you Choice: 1
----------------------------------------
Enter element to add: test

----------------------------------------
         ARRAY OF STACK 
----------------------------------------
 [1] PUSH
 [2] POP
 [3] PEEK
 [4] DISPLAY
 [5] EXIT
----------------------------------------
 Enter you Choice: 3

 The top most element is test

x appears to be temporary variable that should be local to the functions that require it. In general it's a good idea to eliminate global variables. Create a struct to hold stack state, and pass a pointer to that struct to each function. Consider separating the UI from functions that does stuff. In this case, move the printf() to main(). This allows you to easily reuse your functions. For instance, pop() could be written as char *data = peek(); if(data) top--; return data;. Check the return value from scanf() otherwise you may be dealing with undefined data.

CodePudding user response:

printf("\n The top most element is: ");
            strcpy(x,a[top]);
            printf("%s",x);
  • Related