Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct List {
  6. int a;
  7. List *next;
  8. };
  9. struct Double_List {//структура данных
  10. int Data; //информационное поле
  11. Double_List *Next, //адресное поле
  12. *Prior; //адресное поле
  13. };
  14. Double_List *Head; //указатель на первый элемент списка
  15. Double_List *Current;
  16. //указатель на текущий элемент списка (при необходимости)
  17. struct Circle_Single_List {
  18. int Data;
  19. Circle_Single_List* Next;
  20. };
  21. Circle_Single_List *Head_circ;
  22. Circle_Single_List *Loop;
  23. class _List {
  24. public:
  25. List* newList;
  26. _List() {
  27. Head = NULL;
  28. newList = NULL;
  29. Head_circ = NULL;
  30. }
  31. void additem(int d);
  32. void additem2(int, Double_List*);
  33. void connect(_List &list);
  34. void sort(List *, List*);
  35. void sort2(Double_List *, Double_List*);
  36. void Prints();
  37. void clear();
  38. void Print_Double_List();
  39. void Make_Circle_Single_List(int d, Circle_Single_List* Loop);
  40. void Print_Circle_Single_List(Circle_Single_List* Head);
  41. void Sort_Circle_List(Circle_Single_List*, Circle_Single_List*);
  42. };
  43. int main() {
  44. _List u; Double_List* List2;
  45. int n(0), el(0), answ(0);//размер первого списка
  46. cout << "Menu: \n1.odnonapravlenii\n2.dvunapravleni\n3.ciklicheski\n"; cin >> answ;
  47. switch (answ) {
  48. case 1:
  49. cout << "Enter size of list: "; cin >> n;
  50. for (int i = 0; i < n; i++) {
  51. cin >> el;
  52. u.additem(el);
  53. }
  54. u.sort(u.newList, u.newList);
  55. u.Prints();
  56. u.clear();
  57. break;
  58. case 2:
  59. cout << "Enter size of list: "; cin >> n;
  60. for (int i = 0; i < n; i++) {
  61. cin >> el;
  62. u.additem2(el, Current);
  63. }
  64. u.sort2(Head, Head);
  65. u.Print_Double_List();
  66. u.clear();
  67. break;
  68. case 3:
  69. cout << "Enter size of list: "; cin >> n;
  70. for (int i = 0; i < n; i++) {
  71. cin >> el;
  72. u.Make_Circle_Single_List(el, Loop);
  73. }
  74. u.Sort_Circle_List(Head_circ, Head_circ);
  75. u.Print_Circle_Single_List(Head_circ);
  76. break;
  77. default:cout << "\aError, invalid number\n"; break;
  78. }
  79. system("pause");
  80. return 0;
  81. }
  82. void _List::additem(int d) {
  83. List* createList = new List;
  84. createList->a = d;
  85. createList->next = NULL;
  86. List *x;//вспомогательный указатель
  87. if (newList != NULL) {
  88. x = newList;
  89. while ((x->next) != NULL) {
  90. x = x->next;
  91. }
  92. x->next = createList;
  93. }
  94. else {
  95. newList = createList;
  96. }
  97. }
  98.  
  99. void _List::Prints() {
  100. cout << "Spisok: =";
  101. while (newList) {
  102. cout << newList->a << " ";
  103. newList = newList->next;
  104. }
  105. }
  106. void _List::Print_Double_List() {
  107. cout << "Spisok: =";
  108. while (Head) {
  109. cout << Head->Data << " ";
  110. Head = Head->Next;
  111. }
  112. }
  113. void _List::sort(List *node, List *node2) {
  114. for (node = newList; node; node = node->next)
  115. for (node2 = newList; node2; node2 = node2->next)
  116. if (node->a > node2->a) { // если число из node меньше числа из node2 то переставляем их
  117. int i = node->a;
  118. node->a = node2->a;
  119. node2->a = i;
  120. }
  121. }
  122. void _List::sort2(Double_List *node, Double_List *node2) {
  123. for (node = Head; node; node = node->Next)
  124. for (node2 = Head; node2; node2 = node2->Next)
  125. if (node->Data > node2->Data) { // если число из node меньше числа из node2 то переставляем их
  126. int i = node->Data;
  127. node->Data = node2->Data;
  128. node2->Data = i;
  129. }
  130. }
  131. void _List::connect(_List &list) {
  132. List* x = list.newList;
  133. while (x != NULL) {
  134. additem(x->a);
  135. x = x->next;
  136. }
  137. }
  138. void _List::additem2(int d, Double_List * Prior) {
  139. Double_List* createList = new Double_List;
  140. createList->Data = d;
  141. createList->Prior = Prior;
  142. createList->Next = NULL;
  143. Double_List *x;//вспомогательный указатель
  144. if (Head != NULL) {
  145. x = Head;
  146. while ((x->Next) != NULL) {
  147. x = x->Next;
  148. }
  149. x->Next = createList;
  150. }
  151. else {
  152. Head = createList;
  153. }
  154. }
  155. //создание циклического однонаправленного списка
  156. void _List::Make_Circle_Single_List(int d, Circle_Single_List* Loop) {
  157. Circle_Single_List* createList = new Circle_Single_List;
  158. createList->Data = d;
  159. createList->Next = NULL;
  160. Circle_Single_List *x;//вспомогательный указатель
  161. if (Loop == NULL) Loop = createList;
  162. if (Head_circ != NULL) {
  163. x = Head_circ;
  164. while ((x->Next) != NULL) {
  165. x = x->Next;
  166. }
  167. x->Next = createList;
  168. }
  169. else {
  170. Head_circ = Loop;
  171. }
  172. }
  173. //печать циклического однонаправленного списка
  174. void _List::Print_Circle_Single_List(Circle_Single_List* Head) {
  175. Circle_Single_List* ptr = Head;
  176. //вспомогательный указатель
  177. do {
  178. cout << ptr->Data << "\t";
  179. ptr = ptr->Next;
  180.  
  181. } while (ptr);
  182. cout << "\n";
  183. }
  184.  
  185. void _List::Sort_Circle_List(Circle_Single_List* node, Circle_Single_List *node2) {
  186. for (node = Head_circ; node; node = node->Next)
  187. for (node2 = Head_circ; node2; node2 = node2->Next)
  188. if (node->Data > node2->Data) { // если число из node меньше числа из node2 то переставляем их
  189. int i = node->Data;
  190. node->Data = node2->Data;
  191. node2->Data = i;
  192. }
  193. }
  194. void _List::clear() {
  195. if (newList != NULL) {
  196. clear();
  197. delete newList;
  198. }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement