Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. #include "Stack.h"
  2. #include <iostream>
  3. using namespace std;
  4. class Queue
  5. {
  6. Stack<int> s1, s2;
  7. int size;
  8. public:
  9. Queue()
  10. {
  11. size = 0;
  12. }
  13. bool Push(int data)
  14. {
  15. bool ok = s1.Push_Stack(data);
  16. if (!ok)
  17. {
  18. return false;
  19. }
  20. else
  21. {
  22. return true;
  23. }
  24. }
  25. bool Pop(int &a)
  26. {
  27. if (s1.isEmpty())
  28. return false;
  29. int temp;
  30. while (s1.Size_Stack() != 1)
  31. {
  32. s1.Pop_Stack(temp);
  33. s2.Push_Stack(temp);
  34. }
  35. s1.Pop_Stack(a);
  36. while (s2.Size_Stack() != 0)
  37. {
  38. s2.Pop_Stack(temp);
  39. s1.Push_Stack(temp);
  40. }
  41. return true;
  42. }
  43. // Lấy đỉnh của Queue
  44. bool Top(int &a)
  45. {
  46. if (s1.isEmpty())
  47. return false;
  48. int temp;
  49. while (s1.Size_Stack() != 1)
  50. {
  51. s1.Pop_Stack(temp);
  52. s2.Push_Stack(temp);
  53. }
  54. s1.Peak_Stack(a);
  55. while (s2.Size_Stack() != 0)
  56. {
  57. s2.Pop_Stack(temp);
  58. s1.Push_Stack(temp);
  59. }
  60. return true;
  61. }
  62. int Size()
  63. {
  64. return s1.Size_Stack();
  65. }
  66. bool isEmpty()
  67. {
  68. return Size() == 0;
  69. }
  70. void Display()
  71. {
  72. cout << "\tQueue: ";
  73. s1.Display_Items();
  74. }
  75. };
  76.  
  77. int main()
  78. {
  79. Queue Q;
  80. int choice = -1;
  81. int n, temp;
  82. bool ok;
  83. while (choice != 0)
  84. {
  85. cout << "Nhap lua chon cho queue: " << endl;
  86. cout << "0. Thoat khoi chuong trinh." << endl;
  87. cout << "1. Push." << endl;
  88. cout << "2. Pop." << endl;
  89. cout << "3. Top." << endl;
  90. cout << "4. Size. " << endl;
  91. cout << "5. Check empty." << endl;
  92. cout << "6. Display items." << endl;
  93. cin >> choice;
  94. switch (choice)
  95. {
  96. case 0:
  97. system("pause");
  98. return 0;
  99. case 1:
  100. cout << "\tNhap gia tri can push vao queue: ";
  101. cin >> n;
  102. ok = Q.Push(n);
  103. if (ok)
  104. {
  105. cout << "\t";
  106. cout << n << " da duoc push vao." << endl;
  107. }
  108. else
  109. {
  110. cout << "Khong the push!!!" << endl << endl;
  111. }
  112. break;
  113. case 2:
  114. ok = Q.Pop(temp);
  115. if (!ok)
  116. cout << "\tQueue trong." << endl;
  117. else
  118. cout << "\t" << temp << " duoc pop ra khoi queue. " << endl;
  119. break;
  120. case 3:
  121. ok = Q.Top(temp);
  122. if (!ok)
  123. cout << "\tQueue trong." << endl;
  124. else
  125. cout << "\t" << temp << " la top cua queue. " << endl;
  126. break;
  127.  
  128. case 4:
  129. cout << "\tKich thuoc cua queue la: " << Q.Size() << endl;
  130. break;
  131. case 5:
  132. ok = Q.isEmpty();
  133. if (!ok)
  134. cout << "\tQueue khong rong." << endl;
  135. else
  136. cout << "\tQueue rong!!!" << endl;
  137. break;
  138. case 6:
  139. ok = Q.isEmpty();
  140. if (ok)
  141. cout << "\tQueue rong!!!" << endl;
  142. else
  143. Q.Display();
  144. break;
  145. default:
  146. break;
  147. }
  148. }
  149. system("pause");
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement