Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. #include<iostream>
  2. #include<conio.h>
  3. #include<cstdio>
  4. #include<cstdlib>
  5.  
  6. using namespace std;
  7.  
  8. struct CIRCULAR_QUEUE
  9. {
  10. int data[5], front_f, rear;
  11. };
  12.  
  13. CIRCULAR_QUEUE q;
  14.  
  15. void init();
  16. void Enqueue(int item);
  17. void Dequeue();
  18. void Print();
  19. void Size();
  20.  
  21. int main()
  22. {
  23. system("cls");
  24. int choice, input, i;
  25.  
  26. cout << "Circular queue operation"<<endl;
  27. init();
  28. do{
  29. cout<<"1. Enqueue"<<endl;
  30. cout<<"2. Dequeue"<<endl;
  31. cout<<"3. Print"<<endl;
  32. cout<<"4. Clear"<<endl;
  33. cout<<"5. Exit"<<endl;
  34. cout<<endl;
  35. cout<<"Menu : ";
  36. cin>>choice;
  37. if (choice == 5)
  38. {
  39. break;
  40. }
  41. else
  42. {
  43. switch(choice)
  44. {
  45. case 1:
  46. {
  47. Enqueue(input);
  48. cout<<endl;
  49. break;
  50. }
  51. case 2:
  52. {
  53. Dequeue();
  54. cout<<endl;
  55. break;
  56. }
  57. case 3:
  58. {
  59. Print();
  60. cout << endl;
  61. break;
  62. }
  63. case 4:
  64. {
  65. init();
  66. cout<<"Queue is empty ! "<<endl;
  67. cout<<endl;
  68. break;
  69. }
  70. default:
  71. {
  72. cout<<"Wrong menu ! "<<endl;
  73. }
  74. }
  75. }
  76. } while(choice != 5);
  77. return 0;
  78. }
  79.  
  80. void init()
  81. {
  82. q.front_f = q.rear = -1;
  83. }
  84.  
  85. void Enqueue(int item)
  86. {
  87. if(q.front_f == ((q.rear + 1)%5))
  88. {
  89. cout<<"Queue is full !"<<endl;
  90. }
  91. else
  92. {
  93. cout<<"Enter the data : ";
  94. cin>>item;
  95. if(q.front_f == -1)
  96. {
  97. q.front_f = q.rear = 0;
  98. }
  99. else
  100. {
  101. q.rear = (q.rear+1)%5;
  102. }
  103. q.data[q.rear] = item;
  104. cout << "Position : " << q.rear << ", enqueued value : " << item << endl;
  105. }
  106. }
  107.  
  108. void Dequeue()
  109. {
  110. if(q.front_f == -1 && q.rear == -1)
  111. {
  112. cout<<"Queue is empty !"<<endl;
  113. }
  114. else
  115. {
  116. cout<<"Dequeued data : "<<q.data[q.front_f]<< " position : "<<q.front_f<<endl;
  117. if(q.front_f == q.rear)
  118. {
  119. q.front_f = q.rear = -1;
  120. }
  121. else
  122. {
  123. q.front_f = (q.front_f + 1)%5;
  124. }
  125. }
  126. }
  127.  
  128. void Print()
  129. {
  130. int i;
  131. if(q.front_f == -1 && q.rear == -1)
  132. {
  133. cout<<"Queue is empty !"<<endl;
  134. }
  135. else if(q.front_f <= q.rear )
  136. {
  137. cout << "Queue : ";
  138. for( i=q.front_f; i<= q.rear ; i++)
  139. {
  140. cout<<q.data[i]<<"("<<i<<") ";
  141. }
  142. cout << endl << "Queue size : " ;
  143. Size();
  144. cout << endl;
  145. }
  146. else
  147. {
  148. cout << "Queue : ";
  149. for( i=q.front_f; i< 5 ; i++)
  150. {
  151. cout<<q.data[i]<<"("<<i<<") ";
  152. }
  153.  
  154. for( i=0; i<=q.rear ; i++)
  155. {
  156. cout<<q.data[i]<<"("<<i<<") ";
  157. }
  158. cout << endl << "Queue size : " ;
  159. Size();
  160. cout << endl;
  161. }
  162. }
  163.  
  164. void Size()
  165. {
  166. if(q.rear >= q.front_f)
  167. {
  168. cout<<(q.rear - q.front_f) + 1;
  169. }
  170. else
  171. {
  172. cout<< (5 - (q.front_f - q.rear) + 1);
  173. }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement