fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // Cấu trúc một nút trong danh sách liên kết đôi
  5. struct Node {
  6. int data;
  7. Node* next;
  8. Node* prev;
  9.  
  10. Node(int value) {
  11. data = value;
  12. next = prev = nullptr;
  13. }
  14. };
  15.  
  16. // Lớp hàng đợi sử dụng danh sách liên kết đôi
  17. class Deque {
  18. private:
  19. Node* front;
  20. Node* rear;
  21.  
  22. public:
  23. Deque() {
  24. front = rear = nullptr;
  25. }
  26.  
  27. // Kiểm tra hàng đợi rỗng
  28. bool isEmpty() {
  29. return front == nullptr;
  30. }
  31.  
  32. // Thêm vào đầu hàng đợi
  33. void enqueueFront(int value) {
  34. Node* newNode = new Node(value);
  35. if (isEmpty()) {
  36. front = rear = newNode;
  37. } else {
  38. newNode->next = front;
  39. front->prev = newNode;
  40. front = newNode;
  41. }
  42. cout << "Đã thêm " << value << " vào đầu hàng đợi.\n";
  43. }
  44. void enqueueRear(int value) {
  45. Node* newNode = new Node(value);
  46. if (isEmpty()) {
  47. front = rear = newNode;
  48. } else {
  49. rear->next = newNode;
  50. newNode->prev = rear;
  51. rear = newNode;
  52. }
  53. cout << "Đã thêm " << value << " vào cuối hàng đợi.\n";
  54. }
  55. void dequeueFront() {
  56. if (isEmpty()) {
  57. cout << "Hàng đợi rỗng, không thể xóa!\n";
  58. return;
  59. }
  60. Node* temp = front;
  61. front = front->next;
  62. if (front == nullptr) {
  63. rear = nullptr;
  64. } else {
  65. front->prev = nullptr;
  66. }
  67. cout << "Đã xóa " << temp->data << " khỏi đầu hàng đợi.\n";
  68. delete temp;
  69. }
  70. void dequeueRear() {
  71. if (isEmpty()) {
  72. cout << "Hàng đợi rỗng, không thể xóa!\n";
  73. return;
  74. }
  75. Node* temp = rear;
  76. rear = rear->prev;
  77. if (rear == nullptr) {
  78. front = nullptr;
  79. } else {
  80. rear->next = nullptr;
  81. }
  82. cout << "Đã xóa " << temp->data << " khỏi cuối hàng đợi.\n";
  83. delete temp;
  84. }
  85. void printQueue() {
  86. if (isEmpty()) {
  87. cout << "Hàng đợi đang rỗng.\n";
  88. return;
  89. }
  90. Node* temp = front;
  91. cout << "Hàng đợi: ";
  92. while (temp) {
  93. cout << temp->data << " ";
  94. temp = temp->next;
  95. }
  96. cout << endl;
  97. }
  98. ~Deque() {
  99. while (!isEmpty()) {
  100. dequeueFront();
  101. }
  102. }
  103. };
  104.  
  105. // Chương trình kiểm tra
  106. int main() {
  107. Deque dq;
  108.  
  109. dq.enqueueFront(10);
  110. dq.enqueueRear(20);
  111. dq.enqueueFront(5);
  112. dq.enqueueRear(30);
  113.  
  114. dq.printQueue(); // Output: 5 10 20 30
  115.  
  116. dq.dequeueFront(); // Xóa 5
  117. dq.printQueue(); // Output: 10 20 30
  118.  
  119. dq.dequeueRear(); // Xóa 30
  120. dq.printQueue(); // Output: 10 20
  121.  
  122. dq.dequeueFront(); // Xóa 10
  123. dq.dequeueRear(); // Xóa 20
  124.  
  125. dq.dequeueFront(); // Xóa khi rỗng
  126.  
  127. return 0;
  128. }
  129.  
Success #stdin #stdout 0.01s 5304KB
stdin
Standard input is empty
stdout
Đã thêm 10 vào đầu hàng đợi.
Đã thêm 20 vào cuối hàng đợi.
Đã thêm 5 vào đầu hàng đợi.
Đã thêm 30 vào cuối hàng đợi.
Hàng đợi: 5 10 20 30 
Đã xóa 5 khỏi đầu hàng đợi.
Hàng đợi: 10 20 30 
Đã xóa 30 khỏi cuối hàng đợi.
Hàng đợi: 10 20 
Đã xóa 10 khỏi đầu hàng đợi.
Đã xóa 20 khỏi cuối hàng đợi.
Hàng đợi rỗng, không thể xóa!