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

// Define the node structure for the stack
struct node {
    int data;
    struct node* next;
}*top=NULL;

// Function to push a value onto the stack
void push( int ele) {
    if(top==NULL)
    {
        top= (struct node*)malloc(sizeof(struct node));
        top->data=ele;
    }   top->next=NULL;
    else
    {
        new= (struct node*)malloc(sizeof(struct node));
        new->data = ele;
        new->next =top;  // Point new node to the current top
        top = new;   
    }     // Update top to point to the new node
}

// Function to pop a value from the stack
void pop() {
    if (top == NULL) {
        printf("Stack underflow\n");  // Stack is empty
    } else {
        struct node* temp;
        
        printf("Data deleted: %d\n", temp->data); 
        temp=top;
        top=top->next; // Print the data of the deleted node
        free(temp);  // Free the memory of the popped node
    }
}

// Function to print the stack
void printStack()
 {
    if (top == NULL) 
    {
        printf("Stack is empty\n");
    } else 
    {
        struct node* temp;
        temp=top;
        while (temp != NULL) 
        {
            printf("%d ", temp->data);  // Print the data of each node
            temp = temp->next;  // Move to the next node
        }
        printf("\n");
    }
}

// Main function to drive the menu
void main() 
{
    struct node*top = NULL;  // Initialize the stack (top is NULL)
    int choice, element;
     {
        // Display menu options
        printf("\nStack Operations Menu:\n");
        printf("1. Push\n");
        printf("2. Pop\n");
        printf("3. Print Stack\n");
        printf("4. Exit\n");
        for(;;)
        {
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice)
         {
            case 1:  // Push operation
                printf("Enter the element to push: ");
                scanf("%d", &element);
                push( element);
                break;
            case 2:  // Pop operation
                pop();
                break;
            case 3:  // Print stack
                printf("Current stack: ");
                printStack();
                break;
            case 4:  // Exit operation
                printf("Exiting program...\n");
                exit(0);
            default:  // Invalid choice
                printf("Invalid choice! Please try again.\n");
            }
        }

    }
}
    