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

#define MAX 5 // Maximum size of the stack

int stack[MAX]; // Array to store stack elements
int top = -1;   // Top is initialized to -1, indicating an empty stack

// Function to push an element onto the stack
void push(int element) 
{
    if (top == MAX - 1) 
{
        printf("Stack Overflow! Cannot push %d\n", element);
    }
 else 
{
        top++;
        stack[top] = element;
        printf("Pushed %d onto the stack\n", element);
    }
}

// Function to pop an element from the stack
void pop() 
{
    if (top == -1) 
{
        printf("Stack Underflow! Cannot pop an element\n");
    } 
else 
{
        printf("Popped %d from the stack\n", stack[top]);
        top--; // Decrease the top position to remove the element
    }
}

// Function to display the stack
void display() 
{
    if (top == -1) 
{
        printf("The stack is empty\n");
    } 
else 
{
        printf("Stack elements are:\n");
        for (int i = top; i >= 0; i--) 
{
            printf("%d\n", stack[i]);
        }
    }
}

int main() 
{
    int choice, element;
    while (1) 
{
        printf("\nStack Operations:\n");
        printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
        
        switch (choice) 
{
            case 1:
                printf("Enter the element to push: ");
                scanf("%d", &element);
                push(element);
                break;
            case 2:
                pop();
                break;
            case 3:
                display();
                break;
            case 4:
                exit(0);
            default:
                printf("Invalid choice! Please enter a valid option.\n");
        }
    }
    return 0;
}

Explanation of Code

  1. Definitions:
    • #define MAX 5: Defines the maximum size of the stack as 5.
    • int stack[MAX]: Declares an array stack of size 5, which will be used to store the stack elements.
    • int top = -1: Variable top keeps track of the topmost element in the stack. Initially, it is set to -1 indicating the stack is empty.
  2. Functions:
    • push():
      • This function inserts an element onto the stack. It first checks if the stack is full (top == MAX - 1), and if so, it prints “Stack Overflow!”.
      • If the stack is not full, it increments top and assigns the new element to stack[top], thus adding it to the top of the stack.
    • pop():
      • This function removes the top element from the stack. It checks if the stack is empty (top == -1), and if so, it prints “Stack Underflow!”.
      • If the stack is not empty, it prints the topmost element and decrements top, effectively removing the element from the stack.
    • display():
      • This function prints all the elements of the stack from top to bottom. It checks if the stack is empty and prints “The stack is empty” if there are no elements. If the stack has elements, it prints each element starting from the top.
  3. Main Function:
    • The main() function displays a menu with four options: Push, Pop, Display, and Exit. Based on the user’s input, it calls the respective function to perform the stack operation.

Sample Output

Stack Operations:
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter the element to push: 10
Pushed 10 onto the stack

Stack Operations:
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter the element to push: 20
Pushed 20 onto the stack

Stack Operations:
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 3
Stack elements are:
20
10

Stack Operations:
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 2
Popped 20 from the stack

Stack Operations:
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 3
Stack elements are:
10

Explanation of Output:

  1. The user first pushes the element 10 onto the stack. The program confirms this with the message: “Pushed 10 onto the stack”.
  2. The user then pushes 20, and it is added to the stack. The program prints the message: “Pushed 20 onto the stack”.
  3. The user chooses to display the stack, and the program shows the elements, starting from the top (20 and 10).
  4. The user then pops the top element (20) from the stack, and the program confirms with: “Popped 20 from the stack”.
  5. Finally, the user displays the stack again, which now contains only the element 10.

This simple stack implementation using arrays covers the basic operations: push, pop, and display. The stack is a Last In First Out (LIFO) data structure, meaning that the last element inserted is the first to be removed.

Leave a Reply

Your email address will not be published. Required fields are marked *

Verified by MonsterInsights