Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // C++ program to illustrate inserting a Node in
- // a Cicular Doubly Linked list in begging, end
- // and middle
- #include "pch.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <algorithm>
- #include <iostream>
- #include <sstream>
- #include <iomanip>
- #include <fstream>
- #include <string>
- #include <Windows.h>
- #define PATH "birds.txt"
- using namespace std;
- // Structure of a Node
- struct Node
- {
- int id;
- char type[256];
- char gender[256];
- int age;
- struct Node *next;
- struct Node *prev;
- } *start;
- // Проверка файла на отсутствие данных
- bool is_empty_file(ifstream& pFile) {
- return pFile.peek() == ifstream::traits_type::eof();
- }
- // Вывод данных в файл
- void saveBirdsInFile() {
- ofstream file;
- file.open(PATH);
- struct Node *s;
- if (start == NULL) {
- cout << setw(70) << "=== Список пуст ===" << endl;
- return;
- }
- file << setw(3) << "Номер" << setw(15) << "Тип птицы" << setw(15) << "Пол птицы" << setw(25) << "Возраст\n";
- s = start->next;
- while (s != start) {
- file << setw(3) << s->id << setw(15) << s->type << setw(20) << s->gender << setw(20) << s->age << "\n";
- s = s->next;
- }
- file << setw(3) << s->id << setw(15) << s->type << setw(20) << s->gender << setw(20) << s->age << "\n";
- file.close();
- }
- // Function to insert at the end
- void insertEnd(int id, char type[], char gender[], int age)
- {
- // If the list is empty, create a single node
- // circular and doubly list
- if (start == NULL)
- {
- struct Node* new_node = new Node;
- new_node->id = id;
- strcpy_s(new_node->type, type);
- strcpy_s(new_node->gender, gender);
- new_node->age = age;
- new_node->next = new_node->prev = new_node;
- start = new_node;
- return;
- }
- // If list is not empty
- /* Find last node */
- Node *last = start->prev;
- // Create Node dynamically
- struct Node* new_node = new Node;
- new_node->id = id;
- strcpy_s(new_node->type, type);
- strcpy_s(new_node->gender, gender);
- new_node->age = age;
- // Start is going to be next of new_node
- new_node->next = start;
- // Make new node previous of start
- start->prev = new_node;
- // Make last preivous of new node
- new_node->prev = last;
- // Make new node next of old last
- last->next = new_node;
- saveBirdsInFile();
- }
- void outputBirdsFromFile() {
- ifstream file;
- string line;
- string word;
- string list[4] = {};
- file.open(PATH);
- file.ignore(256, '\n');
- if (is_empty_file(file)) {
- cout << setw(70) << "=== Файл не создан или пустой === \n\n";
- return;
- }
- cout << "\n";
- // Считываем данные из файла
- if (file.is_open()) {
- struct Node *s;
- start = NULL; // Удаляем список
- // Вытаскиваем все данные из файлы и сохраняем в список
- while (getline(file, line)) {
- stringstream str(line);
- int count = 0;
- // Вытаскиваем из строки необходимые данные
- while (str >> word) {
- list[count] = word;
- count++;
- }
- char type[256];
- char gender[256];
- strcpy_s(type, list[1].c_str());
- strcpy_s(gender, list[2].c_str());
- insertEnd(stoi(list[0]), type, gender, stoi(list[3]));
- }
- cout << setw(35) << "Номер" << setw(35) << "Тип птицы" << setw(15) << "Пол птицы" << setw(25) << "Возраст\n";
- /*Выводим список в консоль*/
- s = start->next;
- while (s != start) {
- cout << setw(35) << s->id << setw(15) << s->type << setw(20) << s->gender << setw(20) << s->age << "\n";
- s = s->next;
- }
- cout << setw(35) << s->id << setw(15) << s->type << setw(20) << s->gender << setw(20) << s->age << "\n";
- file.close();
- }
- cout << "\n";
- }
- // Function to insert Node at the beginning
- // of the List,
- void insertBegin(int id, char type[], char gender[], int age) {
- if (start == NULL) {
- insertEnd(id, type, gender, age);
- return;
- }
- // Pointer points to last Node
- struct Node *last = start->prev;
- struct Node* new_node = new Node;
- new_node->id = id;
- strcpy_s(new_node->type, type);
- strcpy_s(new_node->gender, gender);
- new_node->age = age;
- // setting up previous and next of new node
- new_node->next = start;
- new_node->prev = last;
- // Update next and previous pointers of start
- // and last.
- last->next = start->prev = new_node;
- // Update start pointer
- start = new_node;
- saveBirdsInFile();
- }
- // Function to insert node with value as value1.
- // The new node is inserted after the node with
- // with value2
- void insertAfter(int id, char type[], char gender[], int age, int id_exist)
- {
- struct Node* new_node = new Node;
- new_node->id = id;
- strcpy_s(new_node->type, type);
- strcpy_s(new_node->gender, gender);
- new_node->age = age;
- // Find node having value2 and next node of it
- struct Node *temp = start;
- while (temp->id != id_exist)
- temp = temp->next;
- struct Node *next = temp->next;
- // insert new_node between temp and next.
- temp->next = new_node;
- new_node->prev = temp;
- new_node->next = next;
- next->prev = new_node;
- saveBirdsInFile();
- }
- bool isExistBird(int id) {
- bool isExist = false;
- if (start == NULL) return isExist;
- struct Node *s = start;
- s = start->next;
- while (s != start) {
- if (s->id == id) isExist = true;
- s = s->next;
- }
- if (s->id == id) {
- isExist = true;
- }
- return isExist;
- }
- void inputBird() {
- int answer;
- int id;
- char type[256];
- char gender[256];
- int age;
- int number_bird_after;
- while (true) {
- cout << "\nВнести новые данные? 1 - Да | 0 - Нет\n";
- cin >> answer;
- if (answer) {
- outputBirdsFromFile();;
- while (true) {
- cout << "Введите номер птицы\n";
- cin >> id;
- if (isExistBird(id)) {
- cout << setw(70) << " === Данный тип птицы уже есть в списке === " << endl;
- }
- else break;
- }
- cout << "Введите тип птицы\n";
- cin >> type;
- cout << "Введите пол птицы\n";
- cin >> gender;
- cout << "Введите возраст птицы\n";
- cin >> age;
- cout << "\nКуда добавить новый элемент? 0 - После элемента | 1 - В конец списка | 2 - В начало списка\n";
- cin >> answer;
- switch (answer) {
- case 0: {
- cout << "\nВведите номер элемента после которого нужно вставить новый элемент \n";
- cin >> number_bird_after;
- insertAfter(id, type, gender, age, number_bird_after);
- break;
- }
- case 1: {
- insertEnd(id, type, gender, age);
- cout << "\n === Обновленный список ===\n";
- // Выводим обновленный список
- outputBirdsFromFile();
- break;
- }
- case 2: {
- insertBegin(id, type, gender, age);
- cout << "\n === Обновленный список ===\n";
- // Выводим обновленный список
- outputBirdsFromFile();
- break;
- }
- default: cout << "=== Неправильнный выбор === \n"; break;
- }
- }
- else break;
- }
- cout << "\n";
- }
- /* Driver program to test above functions*/
- int main()
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- int choice;
- start = NULL;
- while (1)
- {
- cout << "\n-------------------------------" << endl;
- cout << "Операции над двунаправленным циклическим списком:" << endl;
- cout << "\n-------------------------------" << endl;
- cout << "1.Внести данные" << endl;
- cout << "2.Удалить элемент из списка" << endl;
- cout << "5.Обновить узел" << endl;
- cout << "6.Найти элемент" << endl;
- cout << "7.Сортировка" << endl;
- cout << "8.Вывесим данные" << endl;
- cout << "9.Выход" << endl;
- cout << "Введите Ваш выбор: ";
- cin >> choice;
- switch (choice) {
- case 1: inputBird(); break;
- case 6: outputBirdsFromFile(); break;
- case 7: exit(1); break;
- default: cout << "Неправильнный выбор\n"; break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement