Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define MAX 5
  4. int push(int[],int);
  5. int pop(int[],int);
  6. void display(int[],int);
  7. int main()
  8. {
  9. system("clear");
  10. int stack[MAX],top,item,ch;
  11. top=-1;
  12. while(1)
  13. {
  14. printf("\n0: Quit");
  15. printf("\n1: Push");
  16. printf("\n2: Pop");
  17. printf("\n3: Display");
  18. printf("\nEnter choice:");
  19. scanf("%d",&ch);
  20. switch(ch)
  21. {
  22. case 0: return 0;
  23. case 1: top=push(stack,top);
  24. break;
  25. case 2: top=pop(stack,top);
  26. break;
  27. case 3: display(stack,top);
  28. break;
  29. default:printf("\nInvalid choice");
  30. }
  31. }
  32. }
  33. int push(int stack[],int top)
  34. {
  35. int item;
  36. if(top==MAX-1)
  37. {
  38. printf("\nOverflow");
  39. return 0;
  40. }
  41. printf("\nEnter item: ");
  42. scanf("%d",&item);
  43.  
  44. top++;
  45. stack[top]=item;
  46.  
  47. return top;
  48. }
  49. int pop(int stack[],int top)
  50. {
  51. int item;
  52. if(top==-1)
  53. {
  54. printf("\nUnderflow");
  55. return 0;
  56. }
  57. item=stack[top];
  58.  
  59. top--;
  60. printf("\nItem poped= %d",item);
  61. return top;
  62. }
  63.  
  64. void display(int stack[],int top)
  65. {
  66. int i;
  67. printf("\nStack elements are: ");
  68. for(i=0;i<=top;i++)
  69. printf("%d ",stack[i]);
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement