Advertisement
erfanul007

Stack array implementation

Oct 31st, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. #include<stdio.h>
  2. #define max 5
  3. int stack[max+1]={0,0,0,0,0,0};
  4. int top=0;
  5.  
  6. void push()
  7. {
  8. if(top==max) printf("\nStack Overflow\n");
  9. else-{
  10. int num;
  11. printf("Enter the number: ");
  12. scanf("%d",&num);
  13. top++;
  14. stack[top]=num;
  15. }
  16. }
  17.  
  18. void pop()
  19. {
  20. if(top==0) printf("\nUnderflow\n");
  21. else
  22. {
  23. int item;
  24. item=stack[top];
  25. stack[top]=0;
  26. top--;
  27. printf("The deleted item is: %d\n",item);
  28. }
  29. }
  30.  
  31. void display()
  32. {
  33. int i;
  34. printf("\nThe stack is:\n");
  35. for(i=max;i>0;i--)
  36. {
  37. printf("%d\n",stack[i]);
  38. }
  39. printf("\n");
  40. }
  41.  
  42. int main()
  43. {
  44. int x;
  45. while(1)
  46. {
  47. printf("Press 1 to insert element\n");
  48. printf("Press 2 to remove element\n");
  49. scanf("%d",&x);
  50. if(x==1) push();
  51. else if(x==2) pop();
  52. display();
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement