The ones who are crazy enough to think they can change the world are the ones who do.- Steve Jobs
The Pop operation is used to delete an element from the stack. The deletion can only be done on the element in the top most position in an array.
Here is the program to demonstrate Pop operation in stack.
#include <stdio.h> #include <malloc.h> #define MAX 10 int stack[MAX], top = -1; void push(int stack[], int val); int pop(int stack[]); void display(int stack[]); int main() { int val, choice; do { printf("\n 1. PUSH"); printf("\n 2. POP"); printf("\n 3. DISPLAY"); printf("\n 4. EXIT"); printf("\n Enter your option : "); scanf("%d", &choice); switch(choice) { case 1: printf("\n Enter the number to be pushed on stack : "); scanf("%d",&val); push(stack, val); break; case 2: val= pop(stack); if(val != -1); printf("\n The value deleted from stack is: %d",val); break; case 3: display(stack); break; } } while(choice != 4); return 0; } void push(int stack[], int val) { if(top == MAX-1) { printf("\n STACK OVERFLOW"); } else { top++; stack[top] = val; } } int pop(int stack[]) { int val; if(top == -1) { printf("\n STACK UNDERFLOW"); return -1; } else { val = stack[top]; top--; return val; } } void display(int stack[]) { int i; if(top == -1) printf("\n STACK IS EMPTY"); else { for(i = top;i >= 0;i--) printf("\n%d",stack[i]); } }
1. PUSH 2. POP 3. DISPLAY 4. EXIT Enter your option : 1 Enter the number to be pushed on stack : 1 1. PUSH 2. POP 3. DISPLAY 4. EXIT Enter your option : 1 Enter the number to be pushed on stack : 2 1. PUSH 2. POP 3. DISPLAY 4. EXIT Enter your option : 1 Enter the number to be pushed on stack : 3 1. PUSH 2. POP 3. DISPLAY 4. EXIT Enter your option : 3 3 2 1 1. PUSH 2. POP 3. DISPLAY 4. EXIT Enter your option : 2 The value deleted fron the stack is : 3 1. PUSH 2. POP 3. DISPLAY 4. EXIT Enter your option : 3 2 1 1. PUSH 2. POP 3. DISPLAY 4. EXIT Enter your option : 4
We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.
© Copyright 2019