Advertisement
Guest User

Untitled

a guest
Jan 19th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.19 KB | None | 0 0
  1. // C++ program to illustrate inserting a Node in
  2. // a Cicular Doubly Linked list in begging, end
  3. // and middle
  4. #include "pch.h"
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <algorithm>
  8. #include <iostream>
  9. #include <sstream>
  10. #include <iomanip>
  11. #include <fstream>
  12. #include <string>
  13. #include <Windows.h>
  14. #define PATH "birds.txt"
  15. using namespace std;
  16.  
  17. // Structure of a Node
  18. struct Node
  19. {
  20. int id;
  21. char type[256];
  22. char gender[256];
  23. int age;
  24. struct Node *next;
  25. struct Node *prev;
  26. } *start;
  27.  
  28. // Проверка файла на отсутствие данных
  29. bool is_empty_file(ifstream& pFile) {
  30. return pFile.peek() == ifstream::traits_type::eof();
  31. }
  32. // Вывод данных в файл
  33. void saveBirdsInFile() {
  34. ofstream file;
  35. file.open(PATH);
  36.  
  37. struct Node *s;
  38.  
  39. if (start == NULL) {
  40. cout << setw(70) << "=== Список пуст ===" << endl;
  41. return;
  42. }
  43.  
  44. file << setw(3) << "Номер" << setw(15) << "Тип птицы" << setw(15) << "Пол птицы" << setw(25) << "Возраст\n";
  45.  
  46. s = start->next;
  47.  
  48. while (s != start) {
  49.  
  50. file << setw(3) << s->id << setw(15) << s->type << setw(20) << s->gender << setw(20) << s->age << "\n";
  51. s = s->next;
  52. }
  53.  
  54. file << setw(3) << s->id << setw(15) << s->type << setw(20) << s->gender << setw(20) << s->age << "\n";
  55.  
  56. file.close();
  57. }
  58. // Function to insert at the end
  59. void insertEnd(int id, char type[], char gender[], int age)
  60. {
  61. // If the list is empty, create a single node
  62. // circular and doubly list
  63. if (start == NULL)
  64. {
  65. struct Node* new_node = new Node;
  66. new_node->id = id;
  67. strcpy_s(new_node->type, type);
  68. strcpy_s(new_node->gender, gender);
  69. new_node->age = age;
  70. new_node->next = new_node->prev = new_node;
  71. start = new_node;
  72. return;
  73. }
  74.  
  75. // If list is not empty
  76.  
  77. /* Find last node */
  78. Node *last = start->prev;
  79.  
  80. // Create Node dynamically
  81. struct Node* new_node = new Node;
  82. new_node->id = id;
  83. strcpy_s(new_node->type, type);
  84. strcpy_s(new_node->gender, gender);
  85. new_node->age = age;
  86.  
  87. // Start is going to be next of new_node
  88. new_node->next = start;
  89.  
  90. // Make new node previous of start
  91. start->prev = new_node;
  92.  
  93. // Make last preivous of new node
  94. new_node->prev = last;
  95.  
  96. // Make new node next of old last
  97. last->next = new_node;
  98.  
  99. saveBirdsInFile();
  100. }
  101.  
  102. void outputBirdsFromFile() {
  103.  
  104. ifstream file;
  105. string line;
  106. string word;
  107. string list[4] = {};
  108.  
  109. file.open(PATH);
  110.  
  111. file.ignore(256, '\n');
  112.  
  113. if (is_empty_file(file)) {
  114. cout << setw(70) << "=== Файл не создан или пустой === \n\n";
  115. return;
  116. }
  117.  
  118. cout << "\n";
  119.  
  120. // Считываем данные из файла
  121. if (file.is_open()) {
  122.  
  123. struct Node *s;
  124.  
  125. start = NULL; // Удаляем список
  126.  
  127. // Вытаскиваем все данные из файлы и сохраняем в список
  128. while (getline(file, line)) {
  129.  
  130. stringstream str(line);
  131.  
  132. int count = 0;
  133. // Вытаскиваем из строки необходимые данные
  134. while (str >> word) {
  135. list[count] = word;
  136. count++;
  137. }
  138. char type[256];
  139. char gender[256];
  140. strcpy_s(type, list[1].c_str());
  141. strcpy_s(gender, list[2].c_str());
  142. insertEnd(stoi(list[0]), type, gender, stoi(list[3]));
  143. }
  144.  
  145. cout << setw(35) << "Номер" << setw(35) << "Тип птицы" << setw(15) << "Пол птицы" << setw(25) << "Возраст\n";
  146. /*Выводим список в консоль*/
  147. s = start->next;
  148. while (s != start) {
  149. cout << setw(35) << s->id << setw(15) << s->type << setw(20) << s->gender << setw(20) << s->age << "\n";
  150. s = s->next;
  151. }
  152.  
  153. cout << setw(35) << s->id << setw(15) << s->type << setw(20) << s->gender << setw(20) << s->age << "\n";
  154.  
  155.  
  156.  
  157. file.close();
  158. }
  159.  
  160. cout << "\n";
  161. }
  162.  
  163. // Function to insert Node at the beginning
  164. // of the List,
  165. void insertBegin(int id, char type[], char gender[], int age) {
  166.  
  167.  
  168. if (start == NULL) {
  169. insertEnd(id, type, gender, age);
  170. return;
  171. }
  172.  
  173. // Pointer points to last Node
  174. struct Node *last = start->prev;
  175.  
  176. struct Node* new_node = new Node;
  177. new_node->id = id;
  178. strcpy_s(new_node->type, type);
  179. strcpy_s(new_node->gender, gender);
  180. new_node->age = age;
  181.  
  182. // setting up previous and next of new node
  183. new_node->next = start;
  184. new_node->prev = last;
  185.  
  186. // Update next and previous pointers of start
  187. // and last.
  188. last->next = start->prev = new_node;
  189.  
  190. // Update start pointer
  191. start = new_node;
  192.  
  193. saveBirdsInFile();
  194. }
  195.  
  196. // Function to insert node with value as value1.
  197. // The new node is inserted after the node with
  198. // with value2
  199. void insertAfter(int id, char type[], char gender[], int age, int id_exist)
  200. {
  201. struct Node* new_node = new Node;
  202. new_node->id = id;
  203. strcpy_s(new_node->type, type);
  204. strcpy_s(new_node->gender, gender);
  205. new_node->age = age;
  206.  
  207. // Find node having value2 and next node of it
  208. struct Node *temp = start;
  209. while (temp->id != id_exist)
  210. temp = temp->next;
  211. struct Node *next = temp->next;
  212.  
  213. // insert new_node between temp and next.
  214. temp->next = new_node;
  215. new_node->prev = temp;
  216. new_node->next = next;
  217. next->prev = new_node;
  218.  
  219. saveBirdsInFile();
  220. }
  221.  
  222. bool isExistBird(int id) {
  223. bool isExist = false;
  224.  
  225. if (start == NULL) return isExist;
  226.  
  227. struct Node *s = start;
  228.  
  229. s = start->next;
  230. while (s != start) {
  231. if (s->id == id) isExist = true;
  232. s = s->next;
  233. }
  234.  
  235. if (s->id == id) {
  236. isExist = true;
  237. }
  238.  
  239. return isExist;
  240. }
  241.  
  242. void inputBird() {
  243. int answer;
  244. int id;
  245. char type[256];
  246. char gender[256];
  247. int age;
  248. int number_bird_after;
  249. while (true) {
  250. cout << "\nВнести новые данные? 1 - Да | 0 - Нет\n";
  251. cin >> answer;
  252.  
  253. if (answer) {
  254.  
  255. outputBirdsFromFile();;
  256.  
  257. while (true) {
  258.  
  259. cout << "Введите номер птицы\n";
  260. cin >> id;
  261.  
  262. if (isExistBird(id)) {
  263. cout << setw(70) << " === Данный тип птицы уже есть в списке === " << endl;
  264. }
  265. else break;
  266. }
  267. cout << "Введите тип птицы\n";
  268. cin >> type;
  269. cout << "Введите пол птицы\n";
  270. cin >> gender;
  271. cout << "Введите возраст птицы\n";
  272. cin >> age;
  273.  
  274. cout << "\nКуда добавить новый элемент? 0 - После элемента | 1 - В конец списка | 2 - В начало списка\n";
  275. cin >> answer;
  276. switch (answer) {
  277. case 0: {
  278. cout << "\nВведите номер элемента после которого нужно вставить новый элемент \n";
  279. cin >> number_bird_after;
  280. insertAfter(id, type, gender, age, number_bird_after);
  281. break;
  282. }
  283. case 1: {
  284. insertEnd(id, type, gender, age);
  285. cout << "\n === Обновленный список ===\n";
  286. // Выводим обновленный список
  287. outputBirdsFromFile();
  288. break;
  289. }
  290. case 2: {
  291. insertBegin(id, type, gender, age);
  292. cout << "\n === Обновленный список ===\n";
  293. // Выводим обновленный список
  294. outputBirdsFromFile();
  295. break;
  296. }
  297. default: cout << "=== Неправильнный выбор === \n"; break;
  298. }
  299. }
  300. else break;
  301. }
  302. cout << "\n";
  303. }
  304. /* Driver program to test above functions*/
  305. int main()
  306. {
  307. SetConsoleCP(1251);
  308. SetConsoleOutputCP(1251);
  309. int choice;
  310.  
  311. start = NULL;
  312.  
  313. while (1)
  314. {
  315. cout << "\n-------------------------------" << endl;
  316. cout << "Операции над двунаправленным циклическим списком:" << endl;
  317. cout << "\n-------------------------------" << endl;
  318. cout << "1.Внести данные" << endl;
  319. cout << "2.Удалить элемент из списка" << endl;
  320. cout << "5.Обновить узел" << endl;
  321. cout << "6.Найти элемент" << endl;
  322. cout << "7.Сортировка" << endl;
  323. cout << "8.Вывесим данные" << endl;
  324. cout << "9.Выход" << endl;
  325. cout << "Введите Ваш выбор: ";
  326. cin >> choice;
  327. switch (choice) {
  328. case 1: inputBird(); break;
  329. case 6: outputBirdsFromFile(); break;
  330. case 7: exit(1); break;
  331. default: cout << "Неправильнный выбор\n"; break;
  332. }
  333. }
  334. return 0;
  335. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement