Full width home advertisement

HTML

Tech News

Ad

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int stack[100];
  5. int top=-1;
  6.  
  7. void push();
  8. void pop();
  9. void peek();
  10. void Display();
  11.  
  12. int main(){
  13. int x;
  14.  
  15.  
  16. while(x!=5){
  17. printf("\n\n\nWelcome To stack\n\n");
  18. printf("\n1. Push\n");
  19. printf("2. Pop\n");
  20. printf("3. Peek\n");
  21. printf("4. Display\n");
  22. printf("5. Exit\n");
  23. scanf("%d",&x);
  24. printf("\n\n\n");
  25. switch(x){
  26. case 1:
  27. push();
  28. break;
  29. case 2:
  30. pop();
  31. break;
  32. case 3:
  33. peek();
  34. break;
  35. case 4:
  36. Display();
  37. break;
  38. case 5:
  39. exit(0);
  40. default:
  41. printf("Enter valid choice");
  42. }
  43. }
  44. return 0;
  45. }
  46.  
  47. void push(){
  48. int x;
  49. printf("Enter Data ");
  50. scanf("%d",&x);
  51. top++;
  52. stack[top]=x;
  53. }
  54. void pop(){
  55. int item;
  56. if(top==-1){
  57. printf("underflow");
  58. }
  59. else{
  60. item=stack[top];
  61. top--;
  62. printf("%d deleted\n",item);
  63. }
  64. }
  65. void peek(){
  66. if(top==-1){
  67. printf("underflow");
  68. }
  69. else{
  70. printf("\n%d\n",stack[top]);
  71. }
  72. }
  73. void Display(){
  74. int i;
  75. for(i=top;i>=0;i--){
  76. printf(" %d \n\n",stack[i]);
  77. }
  78. }

No comments:

Post a Comment

Bottom Ad [Post Page]