Advertisement
Guest User

stack

a guest
Mar 19th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.15 KB | None | 0 0
  1. #include<stdio.h>
  2. int a[10000];
  3. int max;
  4. top=0;
  5. int push(int b)
  6. {
  7.     a[top]=b;
  8.  
  9. }
  10.  
  11. int pop()
  12. {
  13.     int temp;
  14.     temp=a[top-1];
  15.     a[top-1]=0;
  16.     return temp;
  17. }
  18. void display()
  19. {
  20.  
  21.     int i;
  22.  
  23.     printf("\n\n");
  24.     for(i=max-1;i>=0;i--){
  25.         printf("%d\n",a[i]);
  26.  
  27.     }
  28.     printf("\n\n");
  29. }
  30.  
  31. int main()
  32. {
  33.     int a,i,n,p;
  34.     printf("Enter the size of stack:");
  35.     scanf("%d",&max);
  36.     for(i=0;;i++){
  37.         printf("Enter 1 for push\nEnter 2 for pop\nEnter 3 for see stack\n");
  38.         scanf("%d",&a);
  39.         if(a==1){
  40.                 if(top>=max){
  41.                     printf("stack overflow\nCan't push\n");
  42.                     continue;
  43.                 }
  44.             printf("\nEnter the number for push:\n");
  45.             scanf("%d",&n);
  46.             push(n);
  47.             top++;
  48.         }
  49.         else if(a==2){
  50.                 if(top==0){
  51.                     printf("stack underflow\nCan't pop\n");
  52.                     continue;
  53.                 }
  54.             p=pop();
  55.             printf("\nThe poped item is %d\n",p);
  56.             top--;
  57.         }
  58.         else{
  59.            display();
  60.         }
  61.     }
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement