Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct node {
  5. int val;
  6. node * next = NULL;
  7. };
  8.  
  9. struct list{
  10. node * head = NULL;
  11. int size = 0;
  12. };
  13.  
  14. void add_node_head(list * & l1 , node * & element){
  15. if (l1 -> head == NULL){
  16. l1 -> head = element;
  17. }
  18. else {
  19. element->next = l1 ->head;
  20. l1 -> head = element;
  21. }
  22. l1 -> size ++;
  23. }
  24.  
  25. int return_val(list * & list, int pos){ //pos - position; Head = 1
  26.  
  27. if (list->head != NULL){
  28.  
  29. node * tmp = list->head;
  30.  
  31. for (int i = 1; i < list->size; i++){
  32. if (i == pos){
  33. return tmp->val;
  34. }
  35. else{
  36. if (tmp->next != NULL){
  37. tmp = tmp->next;
  38. }
  39. else return false;
  40. }
  41. }
  42. return tmp ->val;
  43. }
  44. else return false;
  45. }
  46.  
  47. void add_val_list(list *& list, int nval){
  48. node * tmp = new node;
  49. tmp->val = nval;
  50.  
  51. if (list->head == NULL){
  52. list->head = tmp;
  53. }
  54. else {
  55. tmp->next = list->head;
  56. list->head = tmp;
  57. }
  58. list->size++;
  59. }
  60. void show_list(list *list){
  61. node * tmp = list->head;
  62. while (tmp){
  63. cout << tmp->val << " -> ";
  64. tmp = tmp->next;
  65. }
  66. cout << "NULL";
  67. }
  68.  
  69. void show_list2(list * list){
  70. node * tmp = list->head;
  71. for (int i = 1; i <= list -> size; i++){
  72. cout << return_val(list, i) << " -> ";
  73. }
  74. cout << "NULL";
  75. }
  76.  
  77. bool is_Empty(list * list){ //funkcja zwraca prawde jak jest pusta. tak dla picu funkcja zeby nie pisac pare razy pozniej
  78. if (list->head) return false;
  79. return true;
  80. }
  81.  
  82. bool add_2_list(list * & l1, list * l2){
  83. if (l1 -> size != l2 -> size)return false; //spr czy sa tego samego rozmiaru. Jak nie to nara. #lenistwo
  84.  
  85. node * tmp1 = l1->head;
  86. node * tmp2 = l2->head;
  87. if (is_Empty(l1)) return false; //wczesniej sprawdzone czy tego samego rozmiaru wiec styknie sprawdzic tylko 1 #optymalizacja
  88. for (int i = 1; i <= l1->size; i++){
  89.  
  90. tmp1 ->val += tmp2->val;
  91. tmp1 = tmp1->next;
  92. tmp2 = tmp2->next;
  93. }
  94.  
  95. return true;
  96. }
  97.  
  98.  
  99. bool del_head(list * & list){
  100. if (!is_Empty(list)){
  101. node * tmp = list->head;
  102. tmp = tmp->next;
  103. list->head = tmp->next;
  104. list->size--;
  105. return true;
  106. }
  107. return false;
  108. }
  109.  
  110.  
  111. bool del_last(list * & list){
  112. if (!is_Empty(list)){
  113. node *tmp = list->head;
  114. node *tmp2 = list->head;
  115. while (tmp->next != NULL){
  116. tmp2 = tmp;
  117. tmp = tmp->next;
  118.  
  119. }
  120.  
  121. tmp2->next = NULL;
  122.  
  123. list->size--;
  124. return true;
  125. }
  126. return false;
  127. }
  128.  
  129.  
  130. bool copy_list(list * & newlist, list * & oldlist){ //kopiuje stara liste na koniec nowej. jak pusta to od poczatku poprostu
  131. // newlist: 1 -> 2 -> 4 -> NULL oldlist: 5 -> 6 -> 7 ===>> newlist: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> NULL
  132. node * tmp = newlist->head;
  133. node * tmp2 = oldlist->head;
  134. node * nnode = new node;
  135. if (!is_Empty(newlist) && !is_Empty(oldlist)){
  136. while (tmp->next != NULL){
  137. tmp = tmp->next;
  138. }
  139.  
  140. for (int i = 1; i <= oldlist->size; i++){
  141. node * nnode = new node;
  142. tmp->next = nnode;
  143. nnode->val = tmp2->val;
  144. tmp = nnode;
  145. tmp2 = tmp2->next;
  146. newlist->size++;
  147. }
  148. return true;
  149. }
  150. else{
  151. if (!is_Empty(oldlist)){
  152. newlist->head = nnode;
  153. nnode->val = tmp2->val;
  154. tmp2 = tmp2->next;
  155. tmp = nnode;
  156. newlist->size++;
  157.  
  158. for (int i = 2; i <= oldlist->size; i++){
  159. node * nnode = new node;
  160. tmp->next = nnode;
  161. nnode->val = tmp2->val;
  162. tmp = nnode;
  163. tmp2 = tmp2->next;
  164. newlist->size++;
  165. }
  166. return true;
  167.  
  168. }
  169. return true;
  170. }
  171.  
  172.  
  173.  
  174. return 0;
  175. }
  176. // 1 - 2- 3- 4- 5- 6- 7- 8 -> NULL
  177. void zamienheadzdrugirm(list * & list){
  178. if (list->size > 1){
  179. node * first = list->head;
  180. node * tmp = first -> next;
  181. first->next = tmp->next;
  182. tmp->next = first;
  183.  
  184. list->head = tmp; // 2 -1 -3 -4 - 5 -6 - 7 -8 -> NULL
  185.  
  186. }
  187. }
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196. /*SZTUCZNE FUNKCJE DO ZADAŃ*/
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208. int main(){
  209.  
  210. list * list1 = new list; //tworzenie nowej pustej listy (domyslnie size =0 i head = NULL)
  211. list * list2 = new list;
  212. list * list3 = new list;
  213. node * nowy = new node; //tworzenie nowego node
  214. nowy->val = 2; //nadanie stworzonemu node wartosci 2 (next ma domyslnie NULL)
  215.  
  216.  
  217. add_val_list(list1, 1); //dodaje do poczatku listy node z wartoscia 1.
  218. add_node_head(list1,nowy); //dodaje do poczatku listy podanego node
  219. add_val_list(list1,3); //dodaje do poczatku listy node z wartoscia 3.
  220. add_val_list(list1, 4); //dodaje do poczatku listy node z wartoscia 4.
  221. add_val_list(list1, 5); //dodaje do poczatku listy node z wartoscia 5.
  222. cout << endl;
  223. show_list(list1); //Wyswietla cala liste. Wyswietla az wskaznik != NULL
  224. // cout << return_val(list1,1);
  225. // cout << " " << return_val(list1, 6);
  226. // add_val_list(list1, 6);
  227. // cout << endl;
  228. // show_list2(list1); //Wyswietla cala liste. Wyswietla az i <= size
  229.  
  230. add_val_list(list2, 9); //tworze druga liste
  231. add_val_list(list2, 8);
  232. add_val_list(list2, 7);
  233. add_val_list(list2, 6);
  234. add_val_list(list2, 5);
  235. cout << endl;
  236. show_list2(list2);
  237.  
  238.  
  239. cout << endl;
  240. cout << "lista przed zamiana: ";
  241. show_list2(list1);
  242. zamienheadzdrugirm(list1); //zamienia head z drugim elementem
  243.  
  244.  
  245. cout << endl << "lista po zamianie: ";
  246. show_list2(list1);
  247.  
  248. add_2_list(list1, list2); //dodaje do pierwszej listy druga
  249. cout << endl;
  250. show_list2(list1);
  251.  
  252.  
  253. // if (is_Empty(list2))cout << "pusta"; //testowanie funkcji is_Empty
  254. // if (!is_Empty(list2))cout << "Nie pusta";
  255.  
  256.  
  257.  
  258. add_val_list(list3, 1);
  259. add_val_list(list3, 2);
  260. add_val_list(list3, 3);
  261. add_val_list(list3, 4);
  262. add_val_list(list3, 5);
  263. cout << endl;
  264. show_list2(list3);
  265. del_head(list3);
  266. cout << endl;
  267. show_list2(list3);
  268. del_last(list3);
  269. cout << endl;
  270. show_list2(list3);
  271.  
  272. list * list4 = new list;
  273. copy_list(list4, list1);
  274. copy_list(list4, list1);
  275. show_list(list4);
  276.  
  277.  
  278. cout << endl;
  279. system("pause");
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement