Advertisement
Guest User

5

a guest
Dec 10th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. //Лабораторная работа №5.
  2. /*L-кольцевой двунаправленный список с загл. звеном
  3. Написать функцию, которая выводит равные соседние элементы
  4. 2 курс, 62 группа, Петрова Анастасия.
  5. */
  6.  
  7. #include "pch.h"
  8. #include <iostream>
  9. #include <fstream>
  10. using namespace std;
  11. struct TList
  12. {
  13. int value;
  14. TList* next;
  15. TList* prev;
  16. };
  17.  
  18. bool isEmpty(TList* lst)
  19. {
  20. return lst == NULL;
  21. }
  22.  
  23. int addElem(TList*& lst, int newValue)
  24. {
  25. if (!isEmpty(lst))
  26. {
  27. TList* temp = new TList;
  28. temp->value = newValue;
  29. TList* lastElem = lst->prev;
  30. lastElem->next = temp;
  31. temp->prev = lastElem;
  32. lst->prev = temp;
  33. temp->next = lst;
  34. return 0;
  35. }
  36. else
  37. {
  38. TList* temp = new TList;
  39. temp->value = newValue;
  40. temp->next = temp;
  41. temp->prev = temp;
  42. lst = temp;
  43. return 0;
  44. }
  45. }
  46.  
  47. void printList(TList* lst)
  48. {
  49. TList* tmp = lst;
  50. do {
  51. cout << tmp->value << " ";
  52. tmp = tmp->next;
  53. } while (tmp != lst);
  54. }
  55.  
  56. bool Task(TList* lst)
  57. {
  58. TList* temp = lst;
  59. do
  60. {
  61. if (temp->value == temp->next->value)
  62. return true;
  63. temp = temp->next;
  64.  
  65. }
  66. while (temp != lst);
  67. return 0;
  68. }
  69.  
  70. int clearList(TList*& lst)
  71. {
  72. TList* tmp = lst->next;
  73. TList* deletedItem;
  74. do {
  75. deletedItem = tmp;
  76. tmp = tmp->next;
  77. delete deletedItem;
  78. } while (tmp != lst);
  79. delete lst;
  80. lst = NULL;
  81. return 0;
  82. }
  83.  
  84. int main()
  85. {
  86. setlocale(0, "Russian");
  87. TList* List = NULL;
  88. int t;
  89. ifstream inputFile("input.txt");
  90. if (inputFile)
  91. {
  92. while (!inputFile.eof())
  93. {
  94. inputFile >> t;
  95. addElem(List, t);
  96. }
  97. printList(List);
  98. cout << endl;
  99. if (Task(List))
  100. cout << "Равные соседние элементы в списке есть";
  101. else cout << "Равных соседних элементов в списке нет";
  102. clearList(List);
  103. return 0;
  104. }
  105. else
  106. {
  107. cout << "Не удалось открыть файл для чтения" << endl;
  108. return -1;
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement