Advertisement
muntasir007

Untitled

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