Advertisement
ErliPan

Untitled

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