Skip to content

Commit 0501160

Browse files
committed
add (solution): add queue_new task
1 parent de8bab4 commit 0501160

1 file changed

Lines changed: 95 additions & 151 deletions

File tree

04_week/tasks/queue/queue.cpp

Lines changed: 95 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -1,176 +1,120 @@
11
#include <vector>
22
#include <stack>
3-
#include <stdexcept>
43
#include <algorithm>
54

65
class Queue {
76
private:
8-
std::vector<int> in;
9-
std::vector<int> out;
10-
11-
// Вспомогательный метод для перекладывания элементов
12-
void PrepareOut();
7+
std::vector<int> in;
8+
std::vector<int> out;
139

10+
void PrepareOut() {
11+
if (out.empty() && !in.empty()) {
12+
out.insert(out.end(), in.rbegin(), in.rend());
13+
in.clear();
14+
}
15+
}
16+
1417
public:
1518
// Конструкторы
1619
Queue() = default;
17-
explicit Queue(size_t capacity);
18-
explicit Queue(const std::vector<int>& vec);
19-
explicit Queue(const std::stack<int>& st);
20-
Queue(std::initializer_list<int> list);
21-
22-
//Методы
23-
void Push(int value);
24-
bool Pop();
25-
26-
int& Front();
27-
const int& Front() const;
28-
int& Back();
29-
const int& Back() const;
30-
31-
bool Empty() const;
32-
size_t Size() const;
33-
void Clear();
34-
void Swap(Queue& other);
35-
36-
// Операторы сравнения
37-
bool operator==(const Queue& other) const;
38-
bool operator!=(const Queue& other) const;
39-
};
40-
41-
// Вспомогательный метод для перекладывания элементов
42-
void Queue::PrepareOut() {
43-
if (out.empty() && !in.empty()) {
44-
while (!in.empty()) {
45-
out.push_back(in.back());
46-
in.pop_back();
20+
21+
explicit Queue(std::stack<int> st) {
22+
std::vector<int> temp;
23+
while (!st.empty()) {
24+
temp.push_back(st.top());
25+
st.pop();
4726
}
27+
in.insert(in.end(), temp.rbegin(), temp.rend());
4828
}
49-
}
50-
51-
// Конструктор от размера с резервированием памяти
52-
Queue::Queue(size_t capacity) {
53-
in.reserve(capacity / 2 + 1);
54-
out.reserve(capacity / 2 + 1);
55-
}
56-
57-
// Конструктор от вектора
58-
Queue::Queue(const std::vector<int>& vec) {
59-
for (int value : vec) {
60-
Push(value);
29+
30+
explicit Queue(std::vector<int>& vec) : in(vec) {}
31+
32+
Queue(std::initializer_list<int> ls) : in(ls) {}
33+
34+
explicit Queue(size_t capacity) {
35+
in.reserve(capacity);
36+
out.reserve(capacity);
6137
}
62-
}
63-
64-
// Конструктор от стека
65-
Queue::Queue(const std::stack<int>& st) {
66-
std::stack<int> temp = st;
67-
std::vector<int> tempVec;
68-
69-
while (!temp.empty()) {
70-
tempVec.push_back(temp.top());
71-
temp.pop();
38+
39+
// Методы
40+
void Push(int value) {
41+
in.push_back(value);
7242
}
7343

74-
for (auto it = tempVec.rbegin(); it != tempVec.rend(); ++it) {
75-
Push(*it);
44+
bool Pop() {
45+
if (Empty()) return false;
46+
PrepareOut();
47+
out.pop_back();
48+
return true;
7649
}
77-
}
78-
79-
// Конструктор от initializer_list
80-
Queue::Queue(std::initializer_list<int> list) {
81-
for (int value : list) {
82-
Push(value);
50+
51+
bool Empty() const {
52+
return in.empty() && out.empty();
8353
}
84-
}
85-
86-
// добавляет элемент в конец очереди
87-
void Queue::Push(int value) {
88-
in.push_back(value);
89-
}
90-
91-
// убирает элемент из начала очереди
92-
bool Queue::Pop() {
93-
if (Empty()) {
94-
return false;
54+
55+
int& Front() {
56+
PrepareOut();
57+
return out.back();
9558
}
9659

97-
PrepareOut();
98-
out.pop_back();
99-
return true;
100-
}
101-
102-
// обеспечивает доступ на запись и чтение к элементу в начале очереди (неконстантная версия)
103-
int& Queue::Front() {
104-
PrepareOut();
105-
return out.back();
106-
}
107-
108-
// обеспечивает доступ на запись и чтение к элементу в начале очереди (константная версия)
109-
const int& Queue::Front() const {
110-
// Используем const_cast для вызова PrepareOut на временном объекте
111-
Queue* mutableThis = const_cast<Queue*>(this);
112-
mutableThis->PrepareOut();
113-
return out.back();
114-
}
115-
116-
// обеспечивает доступ на запись и чтение к элементу в конце очереди (неконстантная версия)
117-
int& Queue::Back() {
118-
if (!in.empty()) {
119-
return in.back();
60+
const int& Front() const {
61+
const_cast<Queue*>(this)->PrepareOut();
62+
return out.back();
12063
}
121-
return out.front();
122-
}
123-
124-
// обеспечивает доступ на запись и чтение к элементу в конце очереди (константная версия)
125-
const int& Queue::Back() const {
126-
if (!in.empty()) {
127-
return in.back();
64+
65+
int& Back() {
66+
if (!in.empty()) {
67+
return in.back();
68+
}
69+
PrepareOut();
70+
return out.front();
12871
}
129-
return out.front();
130-
}
131-
132-
// возвращает результат проверки очереди на отсутствие элементов
133-
bool Queue::Empty() const {
134-
return in.empty() && out.empty();
135-
}
136-
137-
// возвращает количество элементов в очереди
138-
size_t Queue::Size() const {
139-
return in.size() + out.size();
140-
}
141-
142-
// очищает очередь
143-
void Queue::Clear() {
144-
in.clear();
145-
out.clear();
146-
}
147-
148-
// меняется элементами с другой очередью (без копирования)
149-
void Queue::Swap(Queue& other) {
150-
in.swap(other.in);
151-
out.swap(other.out);
152-
}
153-
154-
// Оператор сравнения на равенство
155-
bool Queue::operator==(const Queue& other) const {
156-
if (Size() != other.Size()) {
157-
return false;
72+
73+
const int& Back() const {
74+
if (!in.empty()) {
75+
return in.back();
76+
}
77+
const_cast<Queue*>(this)->PrepareOut();
78+
return out.front();
15879
}
159-
//копии для сравнения
160-
Queue q1 = *this;
161-
Queue q2 = other;
16280

163-
while (!q1.Empty()) {
164-
if (q1.Front() != q2.Front()) {
165-
return false;
81+
size_t Size() const {
82+
return in.size() + out.size();
83+
}
84+
85+
void Clear() {
86+
in.clear();
87+
out.clear();
88+
}
89+
90+
void Swap(Queue& other) {
91+
in.swap(other.in);
92+
out.swap(other.out);
93+
}
94+
95+
bool operator==(const Queue& other) const {
96+
if (Size() != other.Size()) return false;
97+
if (Empty()) return true;
98+
99+
Queue q1 = *this;
100+
Queue q2 = other;
101+
102+
q1.PrepareOut();
103+
q2.PrepareOut();
104+
105+
auto it1 = q1.out.rbegin();
106+
auto it2 = q2.out.rbegin();
107+
108+
while (it1 != q1.out.rend() && it2 != q2.out.rend()) {
109+
if (*it1 != *it2) return false;
110+
++it1;
111+
++it2;
166112
}
167-
q1.Pop();
168-
q2.Pop();
113+
114+
return true;
169115
}
170-
return true;
171-
}
172-
173-
// Оператор сравнения на неравенство
174-
bool Queue::operator!=(const Queue& other) const {
175-
return !(*this == other);
176-
}
116+
117+
bool operator!=(const Queue& other) const {
118+
return !(*this == other);
119+
}
120+
};

0 commit comments

Comments
 (0)