Maxim_Leo

Untitled

Apr 27th, 2022
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. #include <iostream>
  2. #define n_ 10
  3. using namespace std;
  4.  
  5. class Queue {
  6. public:
  7. int value;
  8. Queue* next;
  9. Queue* head;
  10. Queue* tail;
  11. Queue() {
  12. next = nullptr;
  13. head = nullptr;
  14. tail = nullptr;
  15. }
  16. Queue* CreateElem(int x) {
  17. Queue* p;
  18. p = new Queue;
  19. p->value = x;
  20. p->next = nullptr;
  21. return p;
  22. }
  23. bool isEmpty() {
  24. if (tail == nullptr) return true;
  25. else return false;
  26. }
  27. Queue* push(int value) {
  28. Queue* p = CreateElem(value);
  29. if (this->tail == nullptr) {
  30. this->head = this->tail = p;
  31. return p;
  32. }
  33. this->tail->next = p;
  34. this->tail = p;
  35. return tail;
  36. }
  37. Queue* pop() {
  38. if (this->head == nullptr) return nullptr;
  39. Queue* p = this->head;
  40. this->head = this->head->next;
  41. delete p;
  42. if (this->head == nullptr) this->tail = nullptr;
  43. return this->head;
  44. }
  45. void front() {
  46. if (tail == nullptr) cout << "Стек пуст" << endl;
  47. else cout << "Вершина очереди: " << head->value << endl;
  48. }
  49. void back() {
  50. if (tail == nullptr) cout << "Стек пуст" << endl;
  51. else cout << "Конец очереди: " << tail->value << endl;
  52. }
  53. void show() {
  54. Queue* p = head;
  55. while (p->next != nullptr) {
  56. cout << p->value;
  57. p = p->next;
  58. }
  59. cout << p->value<<" ";
  60. }
  61. };
  62.  
  63. struct queue {
  64. int a[n_];
  65. int index;
  66. int head;
  67. int tail;
  68. };
  69. void Create(queue* q) {
  70. q->head = NULL;
  71. q->tail = NULL;
  72. q->index = NULL;
  73. }
  74. bool isEmpty_(queue* q) {
  75. if (q->tail == NULL) return true;
  76. else return false;
  77. }
  78. void push_(int elem, queue* q) {
  79. if (q->index == n_) cout<<"Нет места "<<endl;
  80. else {
  81. q->a[q->index] = elem;
  82. q->tail = q->tail + 1;
  83. q->index = q->index + 1;
  84. }
  85. }
  86. void pop_(queue* q) {
  87. if (q->index == 0) cout << "Нельзя удалить элемент из пустого массива";
  88. else {
  89. q->a[q->head] = NULL;
  90. q->head = q->head + 1;
  91. }
  92. if (q->head == n_ + 1) q->head = 0;
  93. }
  94. void show_(queue* q) {
  95. if (q->index == 0) cout << "Массив пуст";
  96. for (int i = q->head; i < q->index; i++) {
  97. if (q->a[i] != NULL) cout << q->a[i]<<" ";
  98. }
  99. }
  100.  
  101. int main() {
  102. setlocale(LC_ALL, "Russian");
  103. int select;
  104. cout << "Выберите способ: " << endl;
  105. cout << "1)Цепная очередь" << endl;
  106. cout << "2)Сплошная очередь" << endl;
  107. cin >> select;
  108. if (select == 1) {
  109. int n, n1, x;
  110. string string1;
  111. Queue queue;
  112. cout << "Пустая очередь создана" << endl;
  113. cout << "Проверка очереди на пустоту: " << queue.isEmpty() << endl;
  114. cout << "Введите количество элементов для заполнения очереди: ";
  115. cin >> n;
  116. for (int i = 0; i < n; i++) {
  117. cout << "Введите элемент: ";
  118. cin >> x;
  119. queue.push(x);
  120. }
  121. cout << "Проверка очереди на пустоту: " << queue.isEmpty() << endl;
  122. if (queue.isEmpty() == 0) queue.front();
  123.  
  124. else 0;
  125. cout << "Вывод элементов очереди: " << endl;
  126. queue.show();
  127. cout << endl;
  128. cout << "Хотите ли вы удалить элементы и сколько?" << endl;
  129. cin >> string1;
  130. if (string1 == "yes" or string1 == "Yes") {
  131. cin >> n1;
  132. for (int i = 0; i < n1; i++) {
  133. queue.pop();
  134. cout << endl;
  135. }
  136. queue.show();
  137. }
  138. else cout << "конец";
  139.  
  140. }
  141. else if (select == 2) {
  142. queue* q=new queue;
  143. int n, n1,x;
  144. string string1;
  145. Create(q);
  146. cout << "Пустая очередь создана" << endl;
  147. cout << "Проверка очереди на пустоту: " << isEmpty_(q) << endl;
  148. cout << "Введите количество элементов для заполнения очереди: ";
  149. cin >> n;
  150. for (int i = 0; i < n; i++) {
  151. cout << "Введите элемент: ";
  152. cin >> x;
  153. push_(x, q);
  154. }
  155. cout << "Проверка очереди на пустоту: " << isEmpty_(q) << endl;
  156. cout << "Вывод элементов очереди" << endl;
  157. show_(q);
  158. cout << endl;
  159. cout << "Хотите ли вы удалить элементы и сколько?" << endl;
  160. cin >> string1;
  161. if (string1 == "yes" or string1 == "Yes") {
  162. cin >> n1;
  163. for (int i = 0; i < n1; i++) {
  164. pop_(q);
  165. cout << endl;
  166. }
  167. show_(q);
  168.  
  169. }
  170.  
  171. else cout << "конец";
  172.  
  173. }
  174. else cout << "Неправильный номер";
  175. }
Advertisement
Add Comment
Please, Sign In to add comment