Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. using namespace std;
  4. typedef struct node* link;
  5. struct node
  6. {
  7. int data;
  8. link next;
  9. };
  10. int sz=0;
  11. static link head;
  12. int lulz = 5;
  13.  
  14.  
  15. link NEW(int num, link next);
  16. void shiftRight(node *a, int size, int pos);
  17. void rtLeft(int *a, int size, int pos);
  18. void add(int num); // добавление в начало
  19. void add(int num, int *array);
  20. void add_rand(int num, int pos); //добавление элемента на место после pos
  21. void add_rand(int num, int pos, int *array);
  22. void searchByArg(int arg);
  23. void searchByArg(int arg, int *array);
  24. void replaceArg(int arg, int replaceFor);
  25. void replaceArg(int arg, int replaceFor, int *array);
  26. void searchByIndex(int index);
  27. void searchByIndex(int index, int *array);
  28. void replaceIndex(int index, int replaceFor);
  29. void replaceIndex(int index, int replaceFor, int *array);
  30. void deleteEverything(int var);
  31. void init();
  32. void del(int pos);
  33. void del(int pos, int *array);
  34. int empty();
  35. void Obmen(int position, int position1);
  36.  
  37.  
  38.  
  39. void main()
  40. {
  41. setlocale(LC_ALL, "russian");
  42. init();
  43. int elem,position,position1;
  44. int choice = 0;
  45. cout << "Способ представления списка в памяти:\n1. Указатели\n2. Статический массив\n3. Динамический массив" << endl;
  46. cin >> choice;
  47. int n;
  48. if (choice == 3)
  49. {
  50. cout << "Введите размер списка" << endl;
  51. cin >> lulz;
  52. }
  53. int *array = new int[lulz];
  54. int pos, value;
  55. while (1<2)
  56. {
  57. cout << "\n1.Вывести список на экран\n2. Добавить элемент в начало\n3. Добавить элемент в произвольную позицию\n4. Удалить элемент\n5. Поиск элемента\n 6. Получение элемента по индексу\n 7. Замена старого значения элемента на заданное новое\n 8. Установка значения элемента по заданному индексу\n 9. Удалить все\n10. Структура пуста?\n11.Генерация структуры данных с заданным числом элементов\n12.Передвинуть указатель структуры на n элементов вперед, назад(только для списков)\n13.Выход" << endl;
  58. int x = 0;
  59. cin >> x;
  60. if (x == 13)
  61. break;
  62. switch (x)
  63. {
  64. case 1:
  65. if (choice == 1)
  66. {
  67. link temp = new node;
  68. temp = head;
  69. while (temp)
  70. {
  71. cout << temp->data << " ";
  72. temp = temp->next;
  73. }
  74. }
  75. else
  76. {
  77. for (int i = 0; i < sz; i++)
  78. cout << array[i] << " ";
  79. }
  80. break;
  81.  
  82. case 2:
  83. cout << "Введите элемент" << endl;
  84. cin >> value;
  85. if (choice == 1)
  86. add(value);
  87. else
  88. {
  89. add(value, array);
  90.  
  91. }
  92. break;
  93.  
  94. case 3:
  95. cout << "Введите позицию" << endl;
  96. cin >> pos;
  97. cout << "Введите элемент" << endl;
  98. cin >> value;
  99. if (choice == 1)
  100. add_rand(value, pos);
  101. else
  102. add_rand(value, pos, array);
  103. break;
  104.  
  105. case 4:
  106. cout << "Введите позицию" << endl;
  107. cin >> pos;
  108. if (choice == 1)
  109. del(pos);
  110. else
  111. del(pos, array);
  112. break;
  113.  
  114. case 5:
  115. cout << "Введите аргумент для поиска" << endl;
  116. cin >> value;
  117. if (choice == 1)
  118. searchByArg(value);
  119. else
  120. searchByArg(value, array);
  121. break;
  122. case 6:
  123. cout << "Введите индекс" << endl;
  124. cin >> pos;
  125. if (choice == 1)
  126. searchByIndex(pos);
  127. else
  128. searchByIndex(pos, array);
  129. break;
  130. case 7:
  131. cout << "Введите аргументы, которые нужно заменить" << endl;
  132. cin >> value;
  133. cout << "Введите аргумент для замены" << endl;
  134. cin >> pos;
  135. if (choice == 1)
  136. replaceArg(value, pos);
  137. else
  138. replaceArg(value, pos, array);
  139. break;
  140. case 8:
  141. cout << "Введите индекс элемента для замены" << endl;
  142. cin >> pos;
  143. cout << "Введите аргумент для замены" << endl;
  144. cin >> value;
  145. if (choice == 1)
  146. replaceIndex(pos, value);
  147. else
  148. replaceIndex(pos, value, array);
  149. break;
  150. case 9:
  151. deleteEverything(choice);
  152. break;
  153. case 10:
  154. if (choice = 1)
  155. {if (head==NULL)
  156. cout << "Структура пуста"<<endl;
  157. else
  158. {
  159. cout << "Структура не пуста"<<endl;
  160. }
  161. }
  162. else
  163. {
  164. if (array[0]=NULL)
  165. cout << "Структура пуста"<<endl;
  166. else
  167. {
  168. cout << "Структура не пуста"<<endl;
  169. }
  170. case 11:
  171. cout << "Введите количество элементов в списке" << endl;
  172. cin >> elem;
  173. cout << "Введите элемент" << endl;
  174. cin >> value;
  175. if (choice == 1)
  176. for (int i = 0; i < elem; i++)
  177. {
  178. add(value);
  179. }
  180.  
  181. else if(elem<lulz)
  182. {for (int i = 0; i < elem; i++)
  183. {
  184. add(value,array);
  185. }
  186. break;
  187. }
  188. else
  189. {
  190. cout << "Вы собираетесь добавить слишком много элементов";
  191. }
  192. case 12:
  193. {if (choice == 1){ cout << "Начальная позиция > "; cin >> position; cout << "Конечная позиция > "; cin >> position1; Obmen(position, position1); } break; }
  194. }
  195.  
  196. }
  197. }
  198.  
  199. }
  200.  
  201.  
  202. link NEW(int num, link next)
  203. {
  204. link newnode = new node;
  205. newnode->data = num;
  206. newnode->next = next;
  207. return newnode;
  208. }
  209. void shiftRight(int *a, int size, int pos)
  210. {
  211. int i = size;
  212. while (i > pos)
  213. {
  214. a[i] = a[i - 1];
  215. i--;
  216. }
  217. }
  218. void shiftLeft(int *a, int size, int pos)
  219. {
  220. int i = pos;
  221. while (i < size - 1)
  222. {
  223. a[i] = a[i + 1];
  224. i++;
  225. }
  226. }
  227.  
  228. void add(int num)
  229. {
  230. if (empty())
  231. {
  232. head = NEW(num, NULL);
  233. }
  234. else
  235. {
  236. link newnode = NEW(num, head);
  237. head = newnode;
  238. }
  239.  
  240. }
  241. void add(int num, int *array)
  242. {
  243.  
  244. shiftRight(array, sz, 0);
  245. array[0] = num;
  246. if (sz<lulz)
  247. sz++;
  248.  
  249. }
  250. int empty()
  251. {
  252. return head == NULL;
  253. }
  254. void init()
  255. {
  256. head = NULL;
  257. }
  258. void add_rand(int num, int pos)
  259. {
  260. if (empty())
  261. return;
  262.  
  263. link temp = head;
  264. for (int i = pos; i > 1; i--)
  265. {
  266. temp = temp->next;
  267. if (temp == NULL)
  268. return;
  269. }
  270. link newnode = NEW(num, temp->next);
  271. temp->next = newnode;
  272.  
  273.  
  274. }
  275. void add_rand(int num, int pos, int *array)
  276. {
  277. shiftRight(array, sz, pos);
  278. array[pos] = num;
  279. if (sz<lulz)
  280. sz++;
  281. }
  282. void del(int pos)
  283. {
  284.  
  285. if (empty())
  286. return;
  287. if (pos == 1)
  288. {
  289. link temp = head;
  290. head = head->next;
  291. delete temp;
  292. }
  293. else
  294. {
  295. link temp = head;
  296. for (int i = pos; i > 2; i--)
  297. {
  298. temp = temp->next;
  299. if (temp == NULL)
  300. return;
  301. }
  302. link temp2 = temp->next;
  303. temp->next = temp2->next;
  304. delete temp2;
  305. }
  306. }
  307. void del(int pos, int *array)
  308. {
  309. shiftLeft(array, sz, pos - 1);
  310. sz--;
  311. }
  312. void searchByArg(int arg)
  313. {
  314. link temp = head;
  315. while (temp)
  316. {
  317. if (temp->data == arg)
  318. cout << "\n Found it: " << temp->data << endl;
  319. temp = temp->next;
  320. }
  321. }
  322. void searchByArg(int arg, int *array)
  323. {
  324. for (int i = 0; i < sz; i++)
  325. if (array[i] == arg)
  326. cout << "\n Found it: " << array[i] << endl;
  327. }
  328. void replaceArg(int arg, int replaceFor)
  329. {
  330. link temp = head;
  331. while (temp)
  332. {
  333. if (temp->data == arg)
  334. temp->data = replaceFor;
  335. temp = temp->next;
  336. }
  337. }
  338. void replaceArg(int arg, int replaceFor, int *array)
  339. {
  340. for (int i = 0; i < sz; i++)
  341. {
  342. if (array[i] == arg)
  343. array[i] = replaceFor;
  344. }
  345. }
  346. void searchByIndex(int index)
  347. {
  348. link temp = head;
  349. for (int i = index; i > 1; i--)
  350. {
  351. if (temp == NULL)
  352. return;
  353. temp = temp->next;
  354. }
  355. cout << temp->data << endl;
  356. }
  357. void searchByIndex(int index, int *array)
  358. {
  359. if (index < sz && index >= 1)
  360. cout << "Here it is: " << array[index - 1] << endl;
  361. else
  362. cout << "Nope" << endl;
  363. }
  364. void replaceIndex(int index, int replaceFor)
  365. {
  366. link temp = head;
  367. for (int i = index; i > 1; i--)
  368. {
  369. if (temp == NULL)
  370. return;
  371. temp = temp->next;
  372. }
  373. temp->data = replaceFor;
  374. }
  375. void replaceIndex(int index, int replaceFor, int *array)
  376. {
  377. if (index < sz && index >= 1)
  378. array[index - 1] = replaceFor;
  379. else
  380. cout << "Nope" << endl;
  381. }
  382. void deleteEverything(int var)
  383. {
  384. if (var == 1)
  385. {
  386. link temp = head;
  387. while (head)
  388. {
  389. temp = head->next;
  390. delete head;
  391. head = temp;
  392. }
  393. }
  394. else
  395. sz = 0;
  396. }
  397. void Obmen(int position, int position1)
  398. {
  399. int s = 0; float temp;
  400. if (head == NULL) cout << "\nСписок пуст\n\n";
  401. else
  402. {
  403. node *a = head;
  404. do
  405. {
  406. if (a->data != NULL){ s++; }
  407. if (s == position){ break; }
  408. a = a->next;
  409. } while (a != head);
  410. s = 0;
  411. node *a1 = head;
  412. do
  413. {
  414. if (a1->data != NULL){ s++; }
  415. if (s == position1){ break; }
  416. a1 = a1->next;
  417. } while (a1 != head);
  418. temp = a->data; a->data = a1->data; a1->data = temp;
  419. }
  420. }
  421. //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement