Rakibul_Ahasan

Stack_Array_With_Function

Aug 21st, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.24 KB | None | 0 0
  1. #include<stdio.h>
  2. #define STACK_MAX 1000
  3.  
  4. int ara[STACK_MAX];
  5. int count = 0;
  6.  
  7. int isEmpty () {
  8.  
  9.     if (count == 0) return 1;
  10.     else return 0;
  11. }
  12.  
  13.  
  14. int isFull () {
  15.     if (count == 100000) return 1;
  16.     else return 0;
  17. }
  18.  
  19.  
  20. int size() {
  21.     return count;
  22. }
  23.  
  24.  
  25. void push (int num) {
  26.  
  27.     if (count < 100000) {
  28.         ara[count] = num;
  29.         count++;
  30.     }
  31.     else printf("Error! The Stack is already full.\n");
  32. }
  33.  
  34.  
  35. int pop () {
  36.  
  37.     if (!isEmpty()) return ara[--count];
  38.     else {
  39.         printf("Error! The Stack is empty.\n");
  40.         return 0;
  41.     }
  42. }
  43.  
  44.  
  45. int top () {
  46.     if (!isEmpty()) return ara[count-1];
  47.     else printf("Error! The Stack is empty.\n");
  48. }
  49.  
  50. int display () {
  51.  
  52.     int i;
  53.     if (count == 0) printf("The Stack is empty.\n");
  54.     for (i=0; i<count; i++) printf("%d ", ara[i]);
  55.     printf("\n");
  56. }
  57.  
  58. void menu () {
  59.      while (1) {
  60.         printf("Press 1 to push.\n");
  61.         printf("Press 2 to check if the Stack is empty.\n");
  62.         printf("Press 3 to pop.\n");
  63.         printf("Press 4 to show the top element.\n");
  64.         printf("Press 5 to display the Stack.\n");
  65.         printf("Press 6 to check if the Stack is full.\n");
  66.         printf("Press 0 to Exit.\n");
  67.  
  68.         int n;
  69.         scanf("%d", &n);
  70.  
  71.         if (n==0) break;
  72.  
  73.         else if (n==1) {
  74.             printf("Enter the number: ");
  75.             int num;
  76.             scanf("%d", &num);
  77.             push(num);
  78.         }
  79.  
  80.         else if (n==2) {
  81.             if (isEmpty()) printf("The Stack is empty.\n");
  82.             else printf("The Stack is not empty.\n");
  83.         }
  84.  
  85.         else if (n==3) {
  86.             if (!isEmpty()) printf("Popped value = %d\n", pop());
  87.             else printf("The Stack is empty.\n");
  88.         }
  89.  
  90.         else if (n==4) {
  91.             if (!isEmpty()) printf("The top element = %d\n", top());
  92.             else printf("The Stack is empty.\n");
  93.         }
  94.  
  95.         else if (n==5) display();
  96.  
  97.         else if (n==6) {
  98.             if (isFull()) printf("The Stack is Full.\n");
  99.             else printf("The Stack is not Full.\n");
  100.         }
  101.  
  102.         else {
  103.             printf("Invalid choice! Try again.\n");
  104.         }
  105.     }
  106. }
  107.  
  108.  
  109. int main () {
  110.     menu();
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment