Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 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, u1, u2; 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 1: "; cin >> n;
  50. for (int i = 0; i < n; i++) {
  51. cin >> el;
  52. u.additem(el);
  53. }
  54. cout << "Enter size of list 2:"; cin >> n;
  55. for (int i = 0; i < n; i++) {
  56. cin >> el;
  57. u1.additem(el);
  58. }
  59. u2.connect(u);
  60. u2.connect(u1);
  61. u2.sort(u.newList, u1.newList);
  62. u2.Prints();
  63. u2.clear();
  64. break;
  65. case 2:
  66. cout << "Enter size of list 1: "; cin >> n;
  67. for (int i = 0; i < n; i++) {
  68. cin >> el;
  69. u.additem2(el, Current);
  70. }
  71. cout << "Enter size of list 2:"; cin >> n;
  72. for (int i = 0; i < n; i++) {
  73. cin >> el;
  74. u1.additem2(el, Current);
  75. }
  76. u2.connect(u);
  77. u2.connect(u1);
  78. u2.sort2(Head, Head);
  79. u2.Print_Double_List();
  80. u2.clear();
  81. break;
  82. case 3:
  83. cout << "Enter size of list 1: "; cin >> n;
  84. for (int i = 0; i < n; i++) {
  85. cin >> el;
  86. u.Make_Circle_Single_List(el, Loop);
  87. }
  88. cout << "Enter size of list 2:"; cin >> n;
  89. for (int i = 0; i < n; i++) {
  90. cin >> el;
  91. u1.Make_Circle_Single_List(el, Loop);
  92. }
  93. u2.connect(u);
  94. u2.connect(u1);
  95. u2.Sort_Circle_List(Head_circ, Head_circ);
  96. u2.Print_Circle_Single_List(Head_circ);
  97. break;
  98. default:cout << "\aError, invalid number\n"; break;
  99. }
  100. system("pause");
  101. return 0;
  102. }
  103. void _List::additem(int d) {
  104. List* createList = new List;
  105. createList->a = d;
  106. createList->next = NULL;
  107. List *x;//вспомогательный указатель
  108. if (newList != NULL) {
  109. x = newList;
  110. while ((x->next) != NULL) {
  111. x = x->next;
  112. }
  113. x->next = createList;
  114. }
  115. else {
  116. newList = createList;
  117. }
  118. }
  119.  
  120. void _List::Prints() {
  121. cout << "Spisok: =";
  122. while (newList) {
  123. cout << newList->a << " ";
  124. newList = newList->next;
  125. }
  126. }
  127. void _List::Print_Double_List() {
  128. cout << "Spisok: =";
  129. while (Head) {
  130. cout << Head->Data << " ";
  131. Head = Head->Next;
  132. }
  133. }
  134. void _List::sort(List *node, List *node2) {
  135. for (node = newList; node; node = node->next)
  136. for (node2 = newList; node2; node2 = node2->next)
  137. if (node->a < node2->a) { // если число из node меньше числа из node2 то переставляем их
  138. int i = node->a;
  139. node->a = node2->a;
  140. node2->a = i;
  141. }
  142. }
  143. void _List::sort2(Double_List *node, Double_List *node2) {
  144. for (node = Head; node; node = node->Next)
  145. for (node2 = Head; node2; node2 = node2->Next)
  146. if (node->Data < node2->Data) { // если число из node меньше числа из node2 то переставляем их
  147. int i = node->Data;
  148. node->Data = node2->Data;
  149. node2->Data = i;
  150. }
  151. }
  152. void _List::connect(_List &list) {
  153. List* x = list.newList;
  154. while (x != NULL) {
  155. additem(x->a);
  156. x = x->next;
  157. }
  158. }
  159. void _List::additem2(int d, Double_List * Prior) {
  160. Double_List* createList = new Double_List;
  161. createList->Data = d;
  162. createList->Prior = Prior;
  163. createList->Next = NULL;
  164. Double_List *x;//вспомогательный указатель
  165. if (Head != NULL) {
  166. x = Head;
  167. while ((x->Next) != NULL) {
  168. x = x->Next;
  169. }
  170. x->Next = createList;
  171. }
  172. else {
  173. Head = createList;
  174. }
  175. }
  176. //создание циклического однонаправленного списка
  177. void _List::Make_Circle_Single_List(int d, Circle_Single_List* Loop) {
  178. Circle_Single_List* createList = new Circle_Single_List;
  179. createList->Data = d;
  180. createList->Next = NULL;
  181. Circle_Single_List *x;//вспомогательный указатель
  182. if (Loop == NULL) Loop = createList;
  183. if (Head_circ != NULL) {
  184. x = Head_circ;
  185. while ((x->Next) != NULL) {
  186. x = x->Next;
  187. }
  188. x->Next = createList;
  189. }
  190. else {
  191. Head_circ = Loop;
  192. }
  193. }
  194. //печать циклического однонаправленного списка
  195. void _List::Print_Circle_Single_List(Circle_Single_List* Head) {
  196. Circle_Single_List* ptr = Head;
  197. //вспомогательный указатель
  198. do {
  199. cout << ptr->Data << "\t";
  200. ptr = ptr->Next;
  201.  
  202. } while (ptr);
  203. cout << "\n";
  204. }
  205.  
  206. void _List::Sort_Circle_List(Circle_Single_List* node, Circle_Single_List *node2) {
  207. for (node = Head_circ; node; node = node->Next)
  208. for (node2 = Head_circ; node2; node2 = node2->Next)
  209. if (node->Data < node2->Data) { // если число из node меньше числа из node2 то переставляем их
  210. int i = node->Data;
  211. node->Data = node2->Data;
  212. node2->Data = i;
  213. }
  214. }
  215. void _List::clear() {
  216. if (newList != NULL) {
  217. clear();
  218. delete newList;
  219. }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement