Добавил:
vvrstcnho
Рад, если кому-то помог
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабораторные работы С++ (для ИВТ) / Готовые лабы С++ / Лаба1 / Laba 1 (8)
.cpp#include <iostream>
#include <windows.h>
using namespace std;
struct Node {
double data;
Node* next;
Node(double val) : data(val), next(nullptr) {}
};
class CircularList {
private:
Node* tail;
public:
CircularList() : tail(nullptr) {
cout << "Создан пустой кольцевой список" << endl;
}
void add(double value) {
Node* newNode = new Node(value);
if (!tail) {
tail = newNode;
tail->next = tail;
} else {
newNode->next = tail->next;
tail->next = newNode;
tail = newNode;
}
cout << "Добавлен элемент: " << value << endl;
}
bool remove(double value) {
if (!tail) {
cout << "Список пуст" << endl;
return false;
}
Node* current = tail->next;
Node* prev = tail;
do {
if (current->data == value) {
prev->next = current->next;
if (current == tail) {
if (current->next == current) tail = nullptr;
else tail = prev;
}
cout << "Удален элемент: " << value << endl;
delete current;
return true;
}
prev = current;
current = current->next;
} while (current != tail->next);
cout << "Элемент " << value << " не найден" << endl;
return false;
}
bool contains(double value) {
if (!tail) {
cout << "Список пуст" << endl;
return false;
}
Node* current = tail->next;
do {
if (current->data == value) {
cout << "Элемент " << value << " найден в списке" << endl;
return true;
}
current = current->next;
} while (current != tail->next);
cout << "Элемент " << value << " не найден в списке" << endl;
return false;
}
void print() {
if (!tail) {
cout << "Список пуст" << endl;
return;
}
Node* current = tail->next;
cout << "Элементы кольцевого списка: ";
do {
cout << current->data << " ";
current = current->next;
} while (current != tail->next);
cout << endl;
}
~CircularList() {
if (!tail) return;
Node* current = tail->next;
while (current != tail) {
Node* next = current->next;
delete current;
current = next;
}
delete tail;
cout << "Список удален" << endl;
}
};
int main() {
SetConsoleOutputCP(65001);
CircularList list;
cout << "=== Работа с кольцевым списком ===" << endl;
list.add(1.0);
list.add(2.0);
list.add(3.0);
list.print();
list.contains(2.0);
list.contains(5.0);
list.remove(2.0);
list.print();
list.remove(5.0); // Несуществующий элемент
return 0;
}
Соседние файлы в папке Лаба1
