Full width home advertisement

HTML

Tech News

Ad

  1. /*
  2. implemaintaction Of Queue using Stack
  3. */
  4. #include<stdio.h>
  5. #define N 5
  6.  
  7. int stack1[N],stack2[N];
  8. int top1=-1,top2=-1,count=0;
  9.  
  10. void Enqueue(int x);
  11. void Dequeue();
  12. void Display();
  13. void push1(int data);
  14. void push2(int data);
  15. int pop1();
  16. int pop2();
  17.  
  18. int main(){
  19. int choice,x;
  20. do{
  21. printf("\n***Circular Queue***\n");
  22. printf("1. Enqueue\n");
  23. printf("2. Dequeue\n");
  24. printf("3. Display\n");
  25. printf("0 To Exit\n");
  26. printf("Enter Choice ");
  27. scanf("%d",&choice);
  28. switch(choice){
  29. case 1:
  30. printf("Enter x ");
  31. scanf("%d",&x);
  32. Enqueue(x);
  33. break;
  34. case 2:
  35. Dequeue();
  36. break;
  37. case 3:
  38. Display();
  39. break;
  40. default:
  41. printf("\n!Wrong Choice!\n");
  42. }
  43. }while(choice!=0);
  44. return 0;
  45. }
  46.  
  47. void Enqueue(int x){
  48. push1(x);
  49. count++;
  50. }
  51. void Dequeue(){
  52. int i,a,b;
  53. if(top1==-1 && top2==-1){
  54. printf("\nQueue Is Empty");
  55. }
  56. else{
  57. for(i=0;i<count;i++){
  58. a=pop1();
  59. push2(a);
  60. }
  61. b=pop2();
  62. printf("Dequeued Element is %d",b);
  63. count--;
  64. for(i=0;i<count;i++){
  65. a=pop2();
  66. push1(a);
  67. }
  68. }
  69. }
  70. void push1(int data){
  71. if(top1==N-1){
  72. printf("\nOverflow\n");
  73. }
  74. else{
  75. top1++;
  76. stack1[top1]=data;
  77. }
  78. }
  79. void push2(int data){
  80. if(top2==N-1){
  81. printf("\nOverflow\n");
  82. }
  83. else{
  84. top2++;
  85. stack2[top2]=data;
  86. count++;
  87. }
  88. }
  89. int pop1(){
  90. return stack1[top1--];
  91. }
  92. int pop2(){
  93. return stack2[top2--];
  94. }
  95. void Display(){
  96. int i;
  97. for(i=0;i<=top1;i++){
  98. printf("%d ",stack1[i]);
  99. }
  100. }
OUTPUT
Queue using stack

No comments:

Post a Comment

Bottom Ad [Post Page]