Добавил:
TaskeFox
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:2 курс / Kyrs_2
.cpp#include<iostream>
#include<fstream>
#include<string>
#include<Windows.h>
using namespace std;
template<typename T>
struct Node
{
T data; //данные
Node* next; //указатель на следующее звено
Node* prev; //указатель на предыдущее звено
};
template<typename T>
class NodeList
{
private:
Node<T>* first;
Node<T>* last;
public:
NodeList() //конструктор
{
first = nullptr;
last = nullptr;
}
NodeList(const NodeList& list) //конструктор копии
{
this->head = this->tail = nullptr;
if (list.first)
{
Node<T>* temp = list.first;
do
{
pushfront(temp->data);
temp = temp->prev;
} while (temp != list.first);
}
}
NodeList operator=(const NodeList& list) //перегрузка оператора =
{
if (list.first)
{
Node<T>* temp = list.first;
do
{
pushfront(temp->data);
temp = temp->prev;
} while (temp != list.first);
}
return *this;
}
NodeList(NodeList && list) //конструктор move
{
this->head = this->tail = nullptr;
if (list.first)
{
Node<T>* temp = list.first;
do
{
pushfront(temp->data);
temp = temp->prev;
} while (temp != list.first);
}
list.Clear();
}
NodeList & operator =(NodeList && list) //перегрузка оператора = з move
{
if (this == &list)
{
return *this;
}
this->head = this->tail = nullptr;
if (list.first)
{
Node<T>* temp = list.first;
do
{
pushfront(temp->data);
temp = temp->prev;
} while (temp != list.first);
}
list.Clear();
return *this;
}
~NodeList() //деструктор
{
while (first != last)
{
Node<T>* temp = first;
first = first->prev;
delete temp;
}
delete last;
}
Node<T>* GetFirst()const
{
return first;
}
Node<T>* GetLast()const
{
return last;
}
void push_back(const T& obj) //добавление данных в конец списка
{
Node<T>* newNode = new Node<T>;
newNode->data = obj;
if (first)
{
newNode->prev = first;
first->next = newNode;
}
else
{
last = newNode;
}
newNode->next = last;
last->prev = newNode;
first = newNode;
}
void push_front(const T& obj) //добавление данных в начало списка
{
Node<T>* newNode = new Node<T>;
newNode->data = obj;
if (last)
{
newNode->next = last;
last->prev = newNode;
}
else
{
first = newNode;
}
newNode->prev = first;
first->next = newNode;
last = newNode;
}
void show_front() //показать список с начала
{
Node<T>* temp = last;
if (first)
{
do
{
cout << temp->data << endl;
temp = temp->next;
} while (temp != last);
}
}
void show_back() //показать список с конца
{
Node<T>* temp = first;
if (first)
{
do
{
cout << temp->data << endl;
temp = temp->prev;
} while (temp != first);
}
}
void pop_front() //вытолкнуть первое звено из списка
{
Node<T>* temp = first;
delete_node(first);
}
void pop_back() //вытолкнуть последнее звено из списка
{
Node<T>* temp = last;
delete_node(last);
}
void delete_node(Node<T>* temp) //удаление звена из списка
{
if (first) //если в списке есть данные
{
if (temp->next == temp) //если в списке единственный элемент
{
first = nullptr;
last = nullptr;
}
else
{
if (temp == last) //если элемент на удаление последний
{
last = last->next;
}
if (temp == first) //если элемент на удаление первый
{
first = first->prev;
}
temp->next->prev = temp->prev;
temp->prev->next = temp->next;
delete temp;
}
}
}
Node<T>* find_data(const T& dt) //найти данные в списке
{
Node<T>* temp = first;
if (first)
{
do
{
if (temp->data == dt)
{
return temp;
}
temp = temp->prev;
} while (temp != first);
}
temp = nullptr;
return temp;
}
void sort() //сортитровка списка
{
Node<T>* temp1 = first;
int n = count();
for (int x = 0; x < n - 1; x++)
{
Node<T>* temp2 = temp1->prev;
for (int i = x; i < n - 1; i++)
{
if (temp1->data < temp2->data)
{
swap(temp1->data, temp2->data);
}
temp2 = temp2->prev;
}
temp1 = temp1->prev;
}
}
int count() //посчитать звенья в списке
{
if (first)
{
Node<T>* temp = first;
int i = 0;
do
{
temp = temp->prev;
i++;
} while (temp != first);
return i;
}
else
{
return 0;
}
}
void clear() //очистить список
{
while (first != last)
{
Node<T>* temp = first;
first = first->prev;
delete temp;
}
delete last;
last = nullptr;
first = nullptr;
}
friend ostream& operator << (ostream &os, NodeList& obj) // перегрузка оператора вывода
{
int с = obj.count();
int i = 0;
Node<T>* temp = obj.GetLast();
if (obj.GetFirst())
{
do
{
os << temp->data;
i++;
if (i != с)
os << endl;
temp = temp->next;
} while (temp != obj.GetLast());
}
return os;
}
friend istream& operator >> (istream &is, NodeList& obj) // перегрузка оператора ввода
{
obj.clear();
while (!is.eof())
{
string tmp;
is >> tmp;
obj.push_back(tmp);
}
return is;
}
};
int main()
{
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
NodeList<string> list;
while (true)
{
system("cls");
cout << "Выберите действие:" << endl;
cout << "1. Добавить елемент в начало списка" << endl;
cout << "2. Добавить елемент в конец списка" << endl;
cout << "3. Показать список(начиная с первого звена)" << endl;
cout << "4. Показать список(начиная с последнего звена)" << endl;
cout << "5. Удалить элемент из начала списка" << endl;
cout << "6. Удалить элемент из конца списка" << endl;
cout << "7. Удалить элемент из списка" << endl;
cout << "8. Найти элемент в списке" << endl;
cout << "9. Сортировать список" << endl;
cout << "10. Считать данные из файла в список" << endl;
cout << "11. Сохранить список в файл" << endl;
cout << "12. Очистить список" << endl;
cout << "0. Выход" << endl;
cout << "Ваш выбор: ";
int choose;
cin >> choose;
switch (choose)
{
case 0:
{
exit(0);
break;
}
case 1:
{
system("cls");
string data;
cout << "Введите данные:\n";
cin >> data;
list.push_front(data);
system("pause");
break;
}
case 2:
{
system("cls");
string data;
cout << "Введите данные:\n";
cin >> data;
list.push_back(data);
system("pause");
break;
}
case 3:
{
system("cls");
if (list.count() != 0) //если список создан
{
list.show_front(); //показать список
}
else
{
cout << "Список пуст" << endl;
}
system("pause");
break;
}
case 4:
{
system("cls");
if (list.count() != 0) //если список создан
{
list.show_back(); //показать список
}
else
{
cout << "Список пуст" << endl;
}
system("pause");
break;
}
case 5:
{
system("cls");
if (list.count() != 0) //если список создан
{
list.pop_back();
cout << "Элемент удален" << endl;
}
else
{
cout << "Список пуст" << endl;
}
system("pause");
break;
}
case 6:
{
system("cls");
if (list.count() != 0) //если список создан
{
list.pop_front();
cout << "Элемент удален" << endl;
}
else
{
cout << "Список пуст" << endl;
}
system("pause");
break;
}
case 7:
{
system("cls");
string data;
if (list.count() != 0)
{
cout << "Введите данные которые хотите удалить : " << endl;
cin >> data;
Node<string>* el = list.find_data(data);
if (el)
{
list.delete_node(el);
cout << "Элемент удален" << endl;
}
else
{
cout << "Данных нет в списке" << endl;
}
}
else
{
cout << "Список пуст" << endl;
}
system("pause");
break;
}
case 8:
{
system("cls");
string data;
cout << "Введите данные которые необходимо найти : " << endl;
cin >> data;
if (list.find_data(data))
{
cout << "Данные есть в списке" << endl;
}
else
{
cout << "Данных нет в списке" << endl;
}
system("pause");
break;
}
case 9:
{
system("cls");
if (list.count() != 0) //если список создан
{
list.sort();
cout << "Список отсортирован" << endl;
}
else
{
cout << "Список пуст" << endl;
}
system("pause");
break;
}
case 10:
{
system("cls");
cout << "Введите название файла : ";
string path;
cin >> path;
ifstream fin(path + ".txt");
if (!fin.is_open()) // если файл не открыт
{
cout << "Файл не найден" << endl;
}
else
{
fin >> list;
}
fin.close();
system("pause");
break;
}
case 11:
{
system("cls");
cout << "Введите название файла : ";
string path;
cin >> path;
ofstream fout(path + ".txt");
fout << list;
fout.close();
system("pause");
break;
}
case 12:
{
system("cls");
list.clear();
cout << "Список очищен" << endl;
system("pause");
break;
}
default:
break;
}
}
return 0;
}
Соседние файлы в папке 2 курс