SHARE
TWEET

list.c

a guest Oct 23rd, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct node {
  5.     int value;
  6.     struct node * next;   /* pointer to the same structure */
  7. } node;
  8.  
  9. typedef node* list;
  10.  
  11. /*
  12.  * Assume that the list is in increasing order and insert the element
  13.  * preserving the increasing order
  14.  */
  15. list list_insert_ordered(list p, int val);
  16.  
  17. /*
  18.  * Concatenate two lists
  19.  */
  20. list list_cat(list before, list after);
  21.  
  22. /*
  23.  * Insert elements at the head of the list
  24.  */
  25. list list_insert_head(list p, int val);
  26.  
  27. /*
  28.  * Insert elements at the tail of the list
  29.  */
  30. list list_insert_tail(list p, int val);
  31.  
  32. /*
  33.  * Print the list content
  34.  */
  35. void list_print(list p);
  36.  
  37. /*
  38.  * Free the list
  39.  */
  40.  
  41. void list_free(list p);
  42.  
  43. int main()
  44. {
  45.     list head = NULL;
  46.  
  47.     /* insert some numbers in the list... */
  48.     head = list_insert_head(head, 10);
  49.     head = list_insert_head(head, 2);
  50.     head = list_insert_head(head, 15);
  51.     /* ... print them... */
  52.     list_print(head);
  53.     /* ... and clean everything up  */
  54.     list_free(head);
  55.  
  56.  
  57.     /*...inserisci una lista di numeri ordinata in ordine crescente...*/
  58.     head = list_insert_head(head, 10);
  59.     //head = list_insert_head(head, 5);
  60.     //head = list_insert_head(head, 2);
  61.     /*inserisci un numero nella lista nella posizione giusta*/
  62.     //head=list_insert_ordered(head,7);
  63.     /*...stampala...*/
  64.     list_print(head);
  65.     /*...e cancellala*/
  66.     //list_free(head);
  67.  
  68.     return 0;
  69. }
  70.  
  71. list list_insert_head(list p, int val)
  72. {
  73.     list new_el;
  74.  
  75.     /* Allocate the new node */
  76.     new_el = malloc(sizeof(*new_el));
  77.     /* Setting the data */
  78.     new_el->value = val;
  79.     /* head insertion: old head becomes .next field of new head */
  80.     new_el->next = p;
  81.     /* new head is the pointer to the new node */
  82.     return new_el;
  83. }
  84.  
  85. void list_print(list p)
  86. {
  87.     /* Looping all nodes until p == NULL */
  88.     if (p == NULL) {
  89.         printf("Empty list\n");
  90.         return;
  91.     }
  92.     printf("[%i]", p->value);
  93.     for(; p->next!=NULL; p = p->next) {
  94.         printf(" -> [%i]", p->next->value);
  95.     }
  96.     printf("\n");
  97. }
  98.  
  99. void list_free(list p)
  100. {
  101.     /* If p ==  NULL, nothing to deallocate */
  102.     if (p == NULL) {
  103.         return;
  104.     }
  105.     /* First deallocate (recursively) the next nodes... */
  106.     list_free(p->next);
  107.     /* ... then deallocate the node itself */
  108.     free(p);
  109. }
  110.  
  111. list list_insert_ordered(list p, int val){
  112.  
  113.     /*list new_el;
  114.     new_el = malloc(sizeof(*new_el));
  115.     new_el->value = val;*/
  116.  
  117.     if(p==NULL){
  118.         /*new_el->next = p;
  119.         return new_el;*/
  120.         p->next=p;
  121.         p->value=val;
  122.         return p;
  123.     }
  124.     else if((p->value)>val){
  125.         p->next->value=p->value;
  126.         p->value=val;
  127.         return p;
  128.     }
  129.     else{
  130.         return list_insert_ordered(p->next,val);
  131.     }
  132.  
  133. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top