Advertisement
p32929

Stack

Apr 12th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.25 KB | None | 0 0
  1.  
  2. #include<stdio.h>
  3. int top=0, max=49, item;
  4. int stack[50];
  5. void show()
  6. {
  7.     int i;
  8.     printf("The stack is: ");
  9.     if(top>0)
  10.     {
  11.         for(i=1; i<=top; i++)
  12.         {
  13.             printf("%d ", stack[i]);
  14.         }
  15.     }
  16.     else {
  17.         printf("empty");
  18.     }
  19.  
  20.     printf("\n\n");
  21. }
  22. void push()
  23. {
  24.     printf("Enter an element:\n");
  25.     scanf("%d", &item);
  26.     if(top==max)
  27.     {
  28.         printf("Overflow\n");
  29.  
  30.     }
  31.     else
  32.     {
  33.         top++;
  34.         stack[top]=item;
  35.     }
  36.     show();
  37. }
  38. void pop()
  39. {
  40.     if(top==0)
  41.     {
  42.         printf("Underflow\n");
  43.     }
  44.     else
  45.     {
  46.         item=stack[top];
  47.         top--;
  48.         printf("Popped item = %d\n", item);
  49.     }
  50.     show();
  51. }
  52. menu(void)
  53. {
  54.     int choice;
  55.     do
  56.     {
  57.         printf("1-Push\n2-Pop\n3-Exit\n");
  58.         scanf("%d", &choice);
  59.     }
  60.     while(choice <1 || choice >3);
  61.     return (choice);
  62. }
  63. main()
  64. {
  65.     int choice;
  66.     do
  67.     {
  68.         choice=menu();
  69.         switch(choice)
  70.         {
  71.         case 1:
  72.             push();
  73.             break;
  74.         case 2:
  75.             pop();
  76.             break;
  77.         case 3:
  78.             printf("end of operation\n");
  79.             break;
  80.         }
  81.     }
  82.     while(choice!=3);
  83.     return;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement