Advertisement
Guest User

Untitled

a guest
May 28th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. List *new_item()
  2. {
  3. List *result=NULL;
  4. result = (List *)malloc(sizeof(List));
  5. printf("New Item -> Val = ");
  6. scanf("%d",&result->val);
  7. result->next = NULL;
  8. return result;
  9. }
  10. /*Създаване на опашъчен списък*/
  11. List *create_queue_list(int size)
  12. {
  13. List *res_root = NULL;
  14. List *end_item = NULL;
  15. int i;
  16. for(i=0; i<size; i++)
  17. {
  18. if(res_root == NULL)
  19. {
  20. res_root = new_item();
  21. end_item = res_root;
  22. }
  23. else
  24. {
  25. end_item->next = new_item();
  26. end_item = end_item->next;
  27. }
  28. }
  29. return res_root;
  30. }
  31. /*Създаване на стеков списък*/
  32. List *create_stack_list(int size)
  33. {
  34. List *res_root = NULL;
  35. List *curr_item = NULL;
  36. int i;
  37.  
  38. Упражнение 9- Едносвързани списъци
  39.  
  40. 7
  41.  
  42. for(i=0; i<size; i++)
  43. {
  44. curr_item = new_item();
  45. curr_item->next = res_root;
  46. res_root=curr_item;
  47. }
  48. return res_root;
  49. }
  50. /*Изчистване на списък от паметта*/
  51. void free_list(List* root)
  52. {
  53. List* curr_item = root;
  54. while(root != NULL)
  55. {
  56. root = root->next;
  57. free(curr_item);
  58. curr_item= root;
  59. }
  60. }
  61. /*Изтриване на всички елемети на списък, които отговарят на зададен критерии*/
  62. List *delete_item(List *root, int criteria(List *item))
  63. {
  64. List* prev_item=root;
  65. List* curr_item=root;
  66. int f=0;
  67. while(curr_item!=NULL)
  68. {
  69. if(criteria(curr_item))
  70. {
  71. if(curr_item == root)
  72. {
  73. root = root->next;
  74. prev_item = root;
  75. f=1;
  76. }
  77. else
  78. prev_item->next = curr_item->next;
  79. free(curr_item);
  80. curr_item = prev_item;
  81. }
  82. prev_item = curr_item;
  83. if(f == 0)
  84. curr_item=curr_item->next;
  85. f=0;
  86. }
  87. return root;
  88. }
  89. /*Добавя елемент след даден елемент*/
  90. void insert_item_after(List *item,List *item2)
  91. {
  92. item2->next=item->next;
  93. item->next = item2;
  94. }
  95. /*Разпечатва на екрана всички елементи от списък*/
  96. void print(List* root)
  97. {
  98. List* curr_item = root;
  99. while(curr_item != NULL)
  100. {
  101. printf("Item has value %d\n",curr_item->val);
  102. curr_item=curr_item->next;
  103. }
  104.  
  105. Упражнение 9- Едносвързани списъци
  106.  
  107. 8
  108.  
  109. }
  110. /*Връща указател към първият елемент от списък, който отговаря на зададен критерии*/
  111. List* find_first_item(List* root, int criteria(List *item))
  112. {
  113. List* curr_item = root;
  114. while(curr_item != NULL)
  115. {
  116. if(criteria(curr_item))
  117. return curr_item;
  118. curr_item = curr_item->next;
  119. }
  120. return NULL;
  121. }
  122. /*Разменя два елемента от един списък*/
  123. List* swap_items(List* root, List* A, List* B)
  124. {
  125. List *prev_A=NULL;
  126. List *prev_B=NULL;
  127. List *curr_item = root;
  128. int f=0;
  129. if(root == A) f=1;
  130. else if(root == B) f=2;
  131. while(curr_item != NULL)
  132. {
  133. if(curr_item->next == A)
  134. prev_A = curr_item;
  135. if(curr_item->next == B)
  136. prev_B = curr_item;
  137. curr_item = curr_item->next;
  138. }
  139. if((prev_A != NULL||f==1) && (prev_B != NULL||f==2))
  140. {
  141. if(f!=1)
  142. prev_A->next = B;
  143. else
  144. root = B;
  145. if(f!=2)
  146. prev_B->next = A;
  147. else
  148. root = A;
  149. curr_item = A->next;
  150. A->next = B->next;
  151. B->next = curr_item;
  152. }
  153. return root;
  154. }
  155. /*Импементация на сортиране по метода на мехурчето върху списък*/
  156. List* bubble_sort(List* root,int compare(List* A, List* B))
  157. {
  158. List* curr_item = root;
  159. int change;
  160. do
  161. for(curr_item = root,change = 0;curr_item-
  162. >next!=NULL&&curr_item!=NULL;curr_item=curr_item->next)
  163. if(compare(curr_item,curr_item->next))
  164. {
  165. root = swap_items(root,curr_item,curr_item->next);
  166. change = 1;
  167. break;
  168. }
  169.  
  170. Упражнение 9- Едносвързани списъци
  171.  
  172. 9
  173.  
  174. while(change);
  175. return root;
  176. }
  177. /*Функции за определяне на критерии и сравнение*/
  178. int cmp(List* A, List* B)
  179. {
  180. if(A->val > B->val)
  181. return 1;
  182. else
  183. return 0;
  184.  
  185. }
  186. int val_more_than_5_and_less_than_8(List *item)
  187. {
  188. if(item->val > 5 && item->val < 8)
  189. return 1;
  190. else
  191. return 0;
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement