Advertisement
SOIKAT

Untitled

Feb 23rd, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.81 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define Size 5
  4. void push(int);
  5. int pop (void);
  6. void traverse(void);
  7. int isFull(void);
  8. int isEmpty(void);
  9. void traverse(void);
  10. int stack[Size];
  11. int top=-1;
  12. void main()
  13. {  int ch,item;
  14.     while(1)
  15.     {
  16.     printf("1.Push\n");
  17.     printf("2.Pop\n");
  18.     printf("3.Peek\n");
  19.     printf("4.Traverse\n");
  20.     printf("5.Quit\n");
  21.     printf("Enter Your Choice: ");
  22.     scanf("%d",&ch);
  23.     switch(ch)
  24.     {
  25.     case 1:printf("Enter Element:");
  26.            scanf("%d",&item);
  27.         push(item);
  28.          break;
  29.     case 2:item=pop();
  30.     if(item==0)
  31.     {
  32.         printf("stack is underflow\n");
  33.     }
  34.     else
  35.     {
  36.         printf("Popped item: %d\n",item);
  37.     }
  38.     break;
  39.     case 3:peek();
  40.     break;
  41.     case 4:traverse();
  42.     break;
  43.     case 5:exit(0);
  44.     break;
  45.         default:printf("Invalid Imput\n");
  46.     }
  47.     }
  48. }
  49. void push(int ele)
  50. {
  51.     if(isFull())
  52.     {
  53.          printf("Stack is overflow\n");
  54.     }
  55.     else
  56.     {
  57.         top++;
  58.         stack[top]=ele;
  59.         printf("%d Pushed\n",ele);
  60.     }
  61. }
  62. int isFull()
  63. {
  64.     if(top==Size-1)
  65.     {
  66.         return 1;
  67.     }
  68.     else
  69.     {
  70.         return 0;
  71.     }
  72. }
  73. int pop ()
  74. {
  75.     if(isEmpty())
  76.     {
  77.        return 0;
  78.     }
  79.     else{
  80.         return stack[top--];
  81.  
  82.     }
  83. }
  84. int isEmpty()
  85. {
  86.     if(top==-1)
  87.     {
  88.         return 1;
  89.     }
  90.     else{
  91.         return 0;
  92.     }
  93. }
  94. void peek()
  95. {
  96.      if(isEmpty())
  97.      {
  98.          printf("stack is empty\n");
  99.      }
  100.      else
  101.      {
  102.          printf("peek element :%d\n",stack[top]);
  103.      }
  104. }
  105. void traverse()
  106. {
  107.     if(isEmpty())
  108.     {
  109.  
  110.     }
  111.     else
  112.     {
  113.         int i;
  114.         printf("stack elements : \n");
  115.         for(i=0;i<=top;i++)
  116.         {
  117.             printf("%d\n",stack[i]);
  118.         }
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement