upsidedown

stacks

Aug 30th, 2011
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.13 KB | None | 0 0
  1. #include<stdio.h>
  2. #define stacksize 10
  3.  
  4.  
  5. struct stack
  6. {
  7.     int num[stacksize];
  8.     int top;
  9.     int count;
  10. }s;
  11. void push(void);
  12. void pop(void);
  13. void display(void);
  14.  
  15.  
  16.  
  17. void main()
  18. {
  19.     s.count=0;
  20.     s.top=-1;
  21.     int choice;
  22.  
  23. label:  printf("enter a choice to perform the following operations on stack\n 1)PUSH\n 2)POP\n 3)DISPLAY\n");
  24.     scanf("%d",&choice);
  25.  
  26.     switch(choice)
  27.     {
  28.     case 1:
  29.        
  30.             push();
  31.             break;
  32.  
  33.     case 2:
  34.             pop();
  35.             break;
  36.  
  37.  
  38.    case 3:
  39.             display();
  40.             break;
  41.  
  42.     default:
  43.         printf("wrong choice entered\n");
  44.         break;
  45.  
  46.     }
  47.  
  48. goto label;
  49.  
  50.  
  51. }
  52.  
  53. void push()
  54. {  
  55.     int x;
  56.     if(s.top==stacksize)
  57.         printf("the stack is full\n");
  58.  
  59.     else
  60.         s.top++;
  61.         printf("enter an element\n");
  62.         scanf("%d",&x);
  63.         s.num[s.top]=x;
  64. }
  65.  
  66. void pop()
  67. {
  68.     int b;
  69.     if(s.top==-1)
  70.         printf("the stack is empty\n");
  71.  
  72.     else
  73.         b=s.num[s.top];
  74.         s.top--;
  75.         s.count--;
  76.         printf("the elment %d is popped from the stack\n",b);
  77. }
  78.  
  79.  
  80. void display()
  81. {  
  82.     int i;
  83.     if(s.top==-1)
  84.         printf("the stack is empty\n");
  85.  
  86.     else
  87.         for(i=s.top;i>0;i--)
  88.             printf("the elements are:\n %d\n",s.num[i]);
  89. }
Advertisement
Add Comment
Please, Sign In to add comment