Advertisement
Guest User

Untitled

a guest
Oct 15th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1.  
  2.  
  3. #include "pch.h"
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. struct t_List
  9. {
  10. double Data;
  11. t_List * Pred, *Next;
  12. };
  13.  
  14.  
  15. t_List * CreateList(int count)
  16. {
  17. t_List *Curr = 0, *Next = 0;
  18. for (unsigned i = 1; i <= count; ++i)
  19. {
  20. Curr = new t_List;
  21. Curr->Next = Next;
  22. if (Next)
  23. Next->Pred = Curr;
  24. Next = Curr;
  25. }
  26. return Curr;
  27. }
  28. void Print(t_List *Curr)
  29. {
  30. t_List *Dop;
  31. while (Curr)
  32. {
  33. cout << Curr->Data << "\t";
  34. Dop = Curr->Next; //Переход к следующему эл-ту
  35. Curr = Dop;
  36. }
  37.  
  38. cout << endl;
  39.  
  40. }
  41. void Print_1(t_List *Curr) {
  42.  
  43. if (Curr)
  44. {
  45. cout << Curr->Data << "\t";
  46. Print_1(Curr->Next); //переход к следующему элементу
  47. }
  48.  
  49. else cout << "\n";
  50.  
  51. }
  52. void DelList(t_List *&Beg) //удаление списка
  53. {
  54. t_List *Buf;
  55. while (Beg) //Beg & Buf - NANI?
  56. {
  57. Buf = Beg->Next;
  58. delete Beg;
  59. Beg = Buf;
  60. }
  61.  
  62. }
  63. t_List *LItem(t_List *Beg, int Ind) //доступ к любому эл-ту списка по его индексу
  64. {
  65. while (Beg && Ind--)
  66. Beg = Beg->Next;
  67. return Beg;
  68.  
  69. }
  70. t_List *Insert(t_List *Curr, int Ind, int DataItem) //вставка эл-та с заданным индексом
  71. {
  72. --Ind;
  73. t_List *NewItem = new (t_List);
  74. NewItem->Data = DataItem;
  75. NewItem->Next = 0;
  76. if (!Curr) //список пуст
  77. Curr = NewItem; //создаем первый элемент списка
  78. else
  79. {
  80. for (int i = 1; i < Ind && Curr->Next != NULL; ++i)
  81. Curr = Curr->Next;
  82. if (!Ind) //вставляем новый элемент на первое место
  83. {
  84. NewItem->Next = Curr;
  85. Curr = NewItem;
  86. }
  87. else {//вставляем новый элемент на непервое место
  88. if (Curr->Next != NULL)
  89. NewItem->Next = Curr->Next;
  90. Curr->Next = NewItem;
  91. }
  92. }
  93. return Curr;
  94.  
  95. }
  96. unsigned LengthList(t_List * Beg)
  97. {
  98. unsigned Length = 0;
  99. while (Beg)
  100. {
  101. ++Length;
  102. Beg = Beg->Next;
  103. }
  104. return Length;
  105. }
  106.  
  107. void DelItem(t_List * &Curr, unsigned Index)
  108. {
  109. if (Index >= LengthList(Curr))
  110. return;
  111. t_List * Item;
  112. if (!Index)
  113. {
  114. Item = Curr->Next;
  115. delete Curr;
  116. Curr = Item;
  117. Curr->Pred = 0;
  118. return;
  119. }
  120. Item = LItem(Curr, Index - 1);
  121. t_List * DItem = Item->Next;
  122. Item->Next = DItem->Next;
  123. Item->Next->Pred = Item;
  124. delete DItem;
  125. }
  126.  
  127. void FillArray (int* const arr, const int size)
  128. {
  129. for (int i = 0; i < size; i++)
  130. {
  131. arr[i] = rand() % 10;
  132. }
  133. }
  134.  
  135. void ShowArray(const int* const arr, const int size)
  136. {
  137. for (int i = 0; i < size; i++)
  138. {
  139. cout << arr[i] << "\t";
  140. }
  141. cout << endl;
  142. }
  143.  
  144. void push_back(int *&arr, int &size,int value)
  145. {
  146. int *newArray = new int[size + 1];
  147.  
  148. for (int i = 0; i < size; i++)
  149. {
  150. newArray[i] = arr[i];
  151. }
  152.  
  153. newArray[size] = value;
  154.  
  155. size++;
  156.  
  157. delete[] arr;
  158.  
  159. arr = newArray;
  160.  
  161. }
  162.  
  163. void pop_back(int *&arr, int &size)
  164. {
  165. size--;
  166. int *newArray = new int[size];
  167. for (int i = 0; i < size; i++)
  168. {
  169. newArray[i] = arr[i];
  170. }
  171.  
  172. delete[] arr;
  173.  
  174. arr = newArray;
  175.  
  176. }
  177.  
  178.  
  179. int main()
  180. {
  181. setlocale(0, "");
  182. int n, size, k;
  183. cout << "Введите размерность:" << endl;
  184. cin >> size;
  185. int *arr = new int[size];
  186. FillArray(arr, size);
  187. cout << "Ващ массив:" << endl;
  188. ShowArray(arr, size);
  189. t_List *List = CreateList(size);
  190. t_List * El = List;
  191. while (El)
  192. {
  193. El->Data = rand() % 100;
  194. El = El->Next;
  195. }
  196. El = List;
  197. cout << "Ващ список:" << endl;
  198. Print(List);
  199. cout << "добавить элемент:" << endl;
  200. cin >> n;
  201. push_back(arr, size, n);
  202. cout << "Ващ массив:" << endl;
  203. ShowArray(arr, size);
  204. Insert(List, 4, n);
  205. cout << "Ващ список:" << endl;
  206. Print_1(List);
  207. cout << "удалили элемент:" << endl;
  208. pop_back(arr, size);
  209. cout << "Ващ массив:" << endl;
  210. ShowArray(arr, size);
  211. DelItem(List, 4);
  212. cout << "Ващ список:" << endl;
  213. Print_1(List);
  214. cout << "значение какого элемента вы хотите получить?:" << endl;
  215. cin >> k;
  216. El = LItem(List, k);
  217. cout << El << endl;
  218. cout << arr[k-1]<<endl;
  219. delete[] arr;
  220. DelList(List);
  221. system("pause");
  222.  
  223. return 0;
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement