Full width home advertisement

HTML

Tech News

Ad

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define N 5
  4.  
  5. //FUNCTIONS
  6. void push();
  7. void pop();
  8. void peep();
  9. void change();
  10. void Display();
  11.  
  12.  
  13. //GLOBAL
  14. int stack[N];
  15. int top=0;
  16.  
  17. //DRIVER PROGRAM
  18. int main(){
  19. int choice=0;
  20. while(choice!=6){
  21. printf("\n***STACK MENU***\n\n");
  22. printf("\n1. PUSH");
  23. printf("\n2. POP");
  24. printf("\n3. PEEK");
  25. printf("\n4. CHANGE");
  26. printf("\n5. DISPLAY");
  27. printf("\n6. EXIT");
  28. printf("\nENTER CHOICE ");
  29. scanf("%d",&choice);
  30. switch(choice){
  31. case 1:
  32. push();
  33. break;
  34. case 2:
  35. pop();
  36. break;
  37. case 3:
  38. peep();
  39. break;
  40. case 4:
  41. change();
  42. break;
  43. case 5:
  44. Display();
  45. break;
  46. case 6:
  47. exit(0);
  48. default:
  49. printf("\nERROR:-> WRONG CHOICE ");
  50. break;
  51. }
  52. }
  53. return 0;
  54. }
  55.  
  56. //TO PUSH THE ELEMENTS IN STACK
  57. void push(){
  58. int x;
  59. if(top==N){
  60. printf("\nOVERFLOW ON PUSH");
  61. }
  62. else{
  63. printf("ENTER X ");
  64. scanf("%d",&x);
  65. top++;
  66. stack[top]=x;
  67. Display();
  68. }
  69. }
  70.  
  71. //TO POP THE FIRST ELEMENT FROM THE STACK
  72. void pop(){
  73. if(top==0){
  74. printf("\n UNDERFLOW ON POP");
  75. }
  76. else{
  77. printf("\n%d Deleted",stack[top]);
  78. top--;
  79. }
  80. }
  81.  
  82. //TO SEE ELEMENT AT GIVEN POSITION (LIFO)
  83. void peep(){
  84. int pos,x;
  85. printf("ENTER POSITION ");
  86. scanf("%d",&pos);
  87. if(top-pos+1<=0){
  88. printf("\nUNDERFLOW ON PEEP");
  89. }
  90. else if(top-pos+1>N){
  91. printf("\nOVERFLOW ON PEEP");
  92. }
  93. else{
  94. x=stack[top-pos+1];
  95. printf("%d",x);
  96. }
  97. }
  98.  
  99. //TO CHANGE ELEMENT AT GIVEN POSITION (LIFO)
  100. void change(){
  101. int pos,x;
  102. printf("ENTER POSITION ");
  103. scanf("%d",&pos);
  104. if(top-pos+1<=0){
  105. printf("\nUNDERFLOW ON CHANGE");
  106. }
  107. else if(top-pos+1>N){
  108. printf("\nOVERFLOW ON CHANGE");
  109. }
  110. else{
  111. printf("ENTER X ");
  112. scanf("%d",&x);
  113. stack[top-pos+1]=x;
  114. }
  115. }
  116.  
  117.  
  118. //TO DISPLAY STACK
  119. void Display(){
  120. int i;
  121. for(i=top;i>0;i--){
  122. printf("%d ",stack[i]);
  123. }
  124. }

Stack implementation using array


No comments:

Post a Comment

Bottom Ad [Post Page]