Advertisement
afrinahoque

Stack array

Dec 8th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.54 KB | None | 0 0
  1. #include<stdio.h>
  2. int top=-1, arr[100];
  3.  
  4. void push(int n)
  5. {
  6.     if(top>=100)
  7.     {
  8.         printf("Stack is overflown.\n");
  9.         return;
  10.     }
  11.     else
  12.         arr[++top]=n;
  13. }
  14.  
  15. int pop()
  16. {
  17.     if(top==-1)
  18.     {
  19.         printf("Stack is empty.\n");
  20.         return -1;
  21.     }
  22.     else
  23.         arr[top--];
  24. }
  25.  
  26. int main()
  27. {
  28.     int i,n,x,s;
  29.     scanf("%d", &n);
  30.     for(i=0; i<n; i++)
  31.     {
  32.         scanf("%d", &x);
  33.         push(x);
  34.     }
  35.     while(n--)
  36.     {
  37.         printf("%d ", arr[top]);
  38.         pop();
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement