upsidedown

redef stack

Aug 30th, 2011
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.17 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define max 10
  4.  
  5. struct stack
  6. {
  7.     int count;
  8.     int top;
  9.     int items[max];
  10. };
  11.  
  12. void main()
  13. {
  14.     int i,j,b=1,a,pp,n;
  15.     struct stack p1;
  16.     p1.top=-1;
  17.     p1.count=1;
  18.     printf("enter choice\n1 to push\n2 to pop\n3 to display\n");
  19.     while(b!=0)
  20.     {
  21.     scanf("%d",&i);
  22.     switch(i)
  23.     {
  24.     case 1:
  25.         printf("enter the number of elements to push \n");
  26.         scanf("%d",&n);
  27.         printf("enter the element to push\n");
  28.         for(j=1;j<=n;j++)
  29.         {
  30.         scanf("%d",&a);
  31.         if(p1.top==max)
  32.         {
  33.             printf("stack is full\n only %d elements can be pushed\n",&j);
  34.  
  35.         }
  36.         else
  37.         {
  38.             p1.top++;
  39.             p1.items[p1.top]=a;
  40.             p1.count++;
  41.         }
  42.         }
  43.         break;
  44.  
  45.     case 2:
  46.        
  47.     if(p1.top==-1)
  48.     {
  49.         printf("stack is empty\n");
  50.    
  51.     }
  52.     else
  53.     {
  54.         pp=p1.items[p1.top];
  55.         p1.top--;
  56.         p1.count--;
  57.         printf("the popped element is %d\n",pp);
  58.     }
  59.        
  60.  
  61.         break;
  62.        
  63.     case 3:
  64.         printf("the elements of a stack are:\n");
  65.         for(j=1;j<(p1.count);j++)
  66.             printf("%d\n",(p1.items[j-1]));
  67.         break;
  68.  
  69.     default:
  70.         printf("invalid operator\n");
  71.         break;
  72.     }
  73.     printf("enter 1 to continue or 0 to exit\n");
  74.     scanf("%d",&b);
  75.     if(b==1)
  76.         printf("enter choice\n");
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment