fork download
  1. include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. // Define the node structure for the stack
  5. struct node {
  6. int data;
  7. struct node* next;
  8. }*top=NULL;
  9.  
  10. // Function to push a value onto the stack
  11. void push( int ele) {
  12. if(top==NULL)
  13. {
  14. top= (struct node*)malloc(sizeof(struct node));
  15. top->data=ele;
  16. } top->next=NULL;
  17. else
  18. {
  19. new= (struct node*)malloc(sizeof(struct node));
  20. new->data = ele;
  21. new->next =top; // Point new node to the current top
  22. top = new;
  23. } // Update top to point to the new node
  24. }
  25.  
  26. // Function to pop a value from the stack
  27. void pop() {
  28. if (top == NULL) {
  29. printf("Stack underflow\n"); // Stack is empty
  30. } else {
  31. struct node* temp;
  32.  
  33. printf("Data deleted: %d\n", temp->data);
  34. temp=top;
  35. top=top->next; // Print the data of the deleted node
  36. free(temp); // Free the memory of the popped node
  37. }
  38. }
  39.  
  40. // Function to print the stack
  41. void printStack()
  42. {
  43. if (top == NULL)
  44. {
  45. printf("Stack is empty\n");
  46. } else
  47. {
  48. struct node* temp;
  49. temp=top;
  50. while (temp != NULL)
  51. {
  52. printf("%d ", temp->data); // Print the data of each node
  53. temp = temp->next; // Move to the next node
  54. }
  55. printf("\n");
  56. }
  57. }
  58.  
  59. // Main function to drive the menu
  60. void main()
  61. {
  62. struct node*top = NULL; // Initialize the stack (top is NULL)
  63. int choice, element;
  64. {
  65. // Display menu options
  66. printf("\nStack Operations Menu:\n");
  67. printf("1. Push\n");
  68. printf("2. Pop\n");
  69. printf("3. Print Stack\n");
  70. printf("4. Exit\n");
  71. for(;;)
  72. {
  73. printf("Enter your choice: ");
  74. scanf("%d", &choice);
  75.  
  76. switch (choice)
  77. {
  78. case 1: // Push operation
  79. printf("Enter the element to push: ");
  80. scanf("%d", &element);
  81. push( element);
  82. break;
  83. case 2: // Pop operation
  84. pop();
  85. break;
  86. case 3: // Print stack
  87. printf("Current stack: ");
  88. printStack();
  89. break;
  90. case 4: // Exit operation
  91. printf("Exiting program...\n");
  92. exit(0);
  93. default: // Invalid choice
  94. printf("Invalid choice! Please try again.\n");
  95. }
  96. }
  97.  
  98. }
  99. }
  100.  
Success #stdin #stdout 0.04s 25856KB
stdin
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");
            }
        }

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

    }
}