Advertisement
ErliPan

Untitled

Dec 23rd, 2020
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. typedef unsigned short int boolean;
  5. #define TRUE 1
  6. #define FALSE 0
  7.  
  8. struct list {
  9. int value;
  10. struct list * next_ptr;
  11. };
  12.  
  13. void init(struct list ** ptr);
  14. boolean pre_insert(struct list **ptrptr, int value);
  15. boolean suf_insert(struct list **ptrptr, int value);
  16. boolean ord_insert(struct list **ptrptr, int value);
  17. boolean pre_remove(struct list **ptrptr, int * value);
  18. boolean suf_remove(struct list **ptrptr, int * value);
  19. boolean ord_remove(struct list **ptrptr, int target);
  20. void visit(struct list *ptr);
  21. void visit_back(struct list * ptr);
  22. boolean search(struct list * ptr, int target);
  23. boolean clone(struct list * src_ptr, struct list ** dst_ptr);
  24. boolean clone2(struct list * src_ptr, struct list ** dst_ptr);
  25.  
  26. void function(struct list ** L, int *V, int N, int M){
  27. int i;
  28. *L= NULL;
  29. struct list ** restart = L;
  30. struct list ** start;
  31. struct list * app2;
  32. for(i=0;i<M;i++){
  33. struct list * new=(struct list *)malloc(sizeof(struct list));
  34. new->value=V[i];
  35. new->next_ptr=NULL;
  36. *L = new;
  37. //(*L)=(*L)->next_ptr;
  38. L = &((*L)->next_ptr);
  39. }
  40. L = restart;
  41. for(i=M;i<N;i++){
  42. struct list * new=(struct list *)malloc(sizeof(struct list));
  43. new->value=V[i];
  44. new->next_ptr=(*L);
  45. if(app2==NULL)
  46. app2->next_ptr=new;
  47. else
  48. start=&new;
  49. app2=(*L);
  50. (*L)=(*L)->next_ptr;
  51. }
  52. L=start;
  53. }
  54.  
  55. void eserizio3(struct list ** ptr, int *V, int N, int M) {
  56.  
  57. for (int i = 0; i < M - 1;i++) {
  58. suf_insert(ptr, V[M + i]);
  59. suf_insert(ptr, V[i]);
  60. }
  61. for (int i = M - 1 + M; i < N;i++) {
  62. suf_insert(ptr, V[i]);
  63. }
  64. }
  65.  
  66.  
  67.  
  68. int main() {
  69. struct list * ptr;
  70. int value;
  71.  
  72. // init the list
  73. init(&ptr);
  74.  
  75. int array[] = {5,1,3,4,6,2,7,9};
  76.  
  77. function(&ptr, array, 8, 3);
  78.  
  79. visit(ptr);
  80.  
  81. //system("pause");
  82. return 0;
  83. }
  84.  
  85. void init(struct list ** ptr)
  86. {
  87. *ptr = NULL;
  88. }
  89.  
  90. boolean pre_insert(struct list **ptrptr, int value)
  91. {
  92. struct list * tmp_ptr;
  93. tmp_ptr = (struct list *)malloc(sizeof(struct list));
  94. if ( tmp_ptr != NULL ) {
  95. tmp_ptr->value = value;
  96. tmp_ptr->next_ptr = *ptrptr;
  97. *ptrptr = tmp_ptr;
  98. return TRUE;
  99. }
  100. else
  101. return FALSE;
  102.  
  103. }
  104.  
  105. boolean suf_insert(struct list **ptrptr, int value)
  106. {
  107. while( *ptrptr != NULL )
  108. ptrptr = &((*ptrptr)->next_ptr);
  109. if ( pre_insert(ptrptr,value) )
  110. return TRUE;
  111. else
  112. return FALSE;
  113. }
  114.  
  115. boolean ord_insert(struct list **ptrptr, int value)
  116. {
  117. while( *ptrptr != NULL && (*ptrptr)->value < value )
  118. ptrptr = &((*ptrptr)->next_ptr);
  119. if ( pre_insert(ptrptr,value) )
  120. return TRUE;
  121. else
  122. return FALSE;
  123. }
  124.  
  125. boolean pre_remove(struct list **ptrptr, int * value)
  126. {
  127. struct list * tmp_ptr;
  128.  
  129. if ( *ptrptr != NULL ) {
  130. tmp_ptr = *ptrptr;
  131. *value = tmp_ptr->value;
  132. *ptrptr = (*ptrptr)->next_ptr;
  133. free(tmp_ptr);
  134. return TRUE;
  135. }
  136. else
  137. return FALSE;
  138. }
  139.  
  140. boolean suf_remove(struct list ** ptrptr, int * value)
  141. {
  142. if ( *ptrptr != NULL ) {
  143. while( (*ptrptr)->next_ptr != NULL ) {
  144. ptrptr = &((*ptrptr)->next_ptr);
  145. }
  146. pre_remove(ptrptr,value);
  147. return TRUE;
  148. }
  149. else
  150. return FALSE;
  151. }
  152.  
  153. boolean ord_remove(struct list ** ptrptr, int target)
  154. {
  155. int value;
  156. boolean found = FALSE;
  157.  
  158. if ( *ptrptr != NULL ) {
  159. while( *ptrptr != NULL && found == FALSE ) {
  160. if ((*ptrptr)->value == target) {
  161. pre_remove(ptrptr, &value);
  162. printf("\nValue %d has been removed from the list",value);
  163. found = TRUE;
  164. }
  165. else
  166. ptrptr = &((*ptrptr)->next_ptr);
  167. }
  168. }
  169. return found;
  170. }
  171.  
  172. void visit(struct list * ptr)
  173. {
  174. printf("\nList: ");
  175. while ( ptr != NULL ) {
  176. printf("%d ", ptr->value);
  177. ptr = ptr->next_ptr;
  178. }
  179. }
  180.  
  181. // backward visit of the list. Elements are printed in reverse order
  182. void visit_back(struct list * ptr)
  183. {
  184. struct list * tmp_ptr;
  185.  
  186. init(&tmp_ptr);
  187. while ( ptr != NULL ) {
  188. pre_insert(&tmp_ptr,ptr->value);
  189. ptr = ptr->next_ptr;
  190. }
  191. visit(tmp_ptr);
  192. }
  193.  
  194. boolean search(struct list * ptr, int target)
  195. {
  196. boolean found = FALSE;
  197.  
  198. while( ptr != NULL && found == FALSE ) {
  199. if ( ptr->value == target )
  200. found = TRUE;
  201. else
  202. ptr = ptr->next_ptr;
  203. }
  204. return found;
  205. }
  206.  
  207. boolean clone(struct list * src_ptr, struct list ** dst_list)
  208. {
  209. boolean is_correct = TRUE;
  210. init(dst_list);
  211. // insert all the elements of the source list in the destination list
  212. while ( src_ptr != NULL && is_correct == TRUE ) {
  213. is_correct = suf_insert(dst_list,src_ptr->value);
  214. src_ptr = src_ptr->next_ptr;
  215. }
  216. return is_correct;
  217. }
  218.  
  219. // clone with linear cost
  220. boolean clone2(struct list * src_ptr, struct list ** dst_ptr)
  221. {
  222. boolean is_correct = TRUE;
  223. init(dst_ptr);
  224. // insert all the elements of the source list in the destination list
  225. while ( src_ptr != NULL && is_correct == TRUE ) {
  226. is_correct = suf_insert(dst_ptr,src_ptr->value);
  227. // advance the pointer to the next_ptr filed of the last element of the list
  228. dst_ptr = &((*dst_ptr)->next_ptr);
  229. src_ptr = src_ptr->next_ptr;
  230. }
  231. return is_correct;
  232. }
  233.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement