Advertisement
Jaagdish47

Stack Code

Nov 23rd, 2024
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.25 KB | Source Code | 0 0
  1. Stack Code
  2. 8A. Write a program to implement the concept of Stack with Push, Pop, Display
  3. and Exit Operation.
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <conio.h> // for getch() (specific to Turbo C)
  8. #define MAX 100
  9.  
  10. struct Stack {
  11.     int arr[MAX];
  12.     int top;
  13. };
  14.  
  15. void initStack(struct Stack *s) {
  16.     s->top = -1;
  17. }
  18.  
  19. int isFull(struct Stack *s) {
  20.     return s->top == MAX - 1;
  21. }
  22.  
  23. int isEmpty(struct Stack *s) {
  24.     return s->top == -1;
  25. }
  26.  
  27. void push(struct Stack *s, int value) {
  28.     if (isFull(s)) {
  29.         printf("Stack Overflow! Cannot push %d\n", value);
  30.     } else {
  31.         s->arr[++s->top] = value;
  32.         printf("%d pushed onto stack.\n", value);
  33.     }
  34. }
  35.  
  36. int pop(struct Stack *s) {
  37.     if (isEmpty(s)) {
  38.         printf("Stack Underflow! Cannot pop from an empty stack.\n");
  39.         return -1;
  40.     } else {
  41.         return s->arr[s->top--];
  42.     }
  43. }
  44.  
  45. void display(struct Stack *s) {
  46.     if (isEmpty(s)) {
  47.         printf("Stack is empty.\n");
  48.     } else {
  49.         printf("Stack elements:\n");
  50.         for (int i = s->top; i >= 0; i--) {
  51.             printf("%d\n", s->arr[i]);
  52.         }
  53.     }
  54. }
  55.  
  56. int main() {
  57.     struct Stack stack;
  58.     initStack(&stack);
  59.     int option, value;
  60.     do {
  61.         printf("\nStack Operations:\n");
  62.         printf("1. Push\n");
  63.         printf("2. Pop\n");
  64.         printf("3. Display\n");
  65.         printf("4. Exit\n");
  66.         printf("Choose an option: ");
  67.         scanf("%d", &option);
  68.        
  69.         switch (option) {
  70.             case 1:
  71.                 printf("Enter a value to push: ");
  72.                 scanf("%d", &value);
  73.                 push(&stack, value);
  74.                 break;
  75.             case 2:
  76.                 value = pop(&stack);
  77.                 if (value != -1) {
  78.                     printf("%d popped from stack.\n", value);
  79.                 }
  80.                 break;
  81.             case 3:
  82.                 display(&stack);
  83.                 break;
  84.             case 4:
  85.                 printf("Exiting program.\n");
  86.                 break;
  87.             default:
  88.                 printf("Invalid option! Please choose again.\n");
  89.         }
  90.     } while (option != 4);
  91.  
  92.     getch(); // Wait for user input before closing (Turbo C-specific)
  93.     return 0;
  94. }
Tags: Stack Code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement