Advertisement
ibanezzaro

leaf to list

Mar 16th, 2016
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. typedef struct tree
  6. {
  7.     int value;
  8.     struct tree * left;
  9.     struct tree * right;
  10. }t_tree;
  11.  
  12. typedef struct list
  13. {
  14.     int value;
  15.     struct list * next;
  16. }t_list;
  17.  
  18.  
  19. void visit_list(t_list **ptrptr);
  20. void list_insertion(t_list **, int);
  21. void visit_leaf(t_tree *, t_list**);
  22. t_tree* new_node();
  23.  
  24.  
  25.  
  26. int main()
  27. {
  28.  
  29.     t_tree * ptr;
  30.     t_list **ptrptr = NULL;
  31.     t_tree *root = new_node();
  32.     root->left = new_node();
  33.     root->right = new_node();
  34.     root->left->left = new_node();
  35.     root->left->right = new_node();
  36.  
  37.     visit_leaf(root, ptrptr);
  38.     visit_list(ptrptr);
  39.  
  40. }
  41.  
  42. //THOSE 2 FUNCTIONS ARE THE CORE OF LEAFTOLIST, NOW CONSTRUCT THE MAIN
  43.  
  44. void list_insertion(t_list **ptrptr, int value)
  45. {
  46.     if(*ptrptr != NULL)
  47.     {
  48.         while(*ptrptr != NULL)
  49.         {
  50.             ptrptr = &((*ptrptr)->next);
  51.         }
  52.         if(*ptrptr != NULL)                             //Fanno la stessa cosa in pratica
  53.         {
  54.             t_list* tmp_ptr;
  55.             tmp_ptr = *ptrptr;
  56.             *ptrptr = (t_list*)malloc(sizeof(t_list));
  57.             (*ptrptr)->value = value;
  58.             (*ptrptr)->next = tmp_ptr;
  59.         }
  60.         else
  61.         {
  62.             *ptrptr = (t_list*)malloc(sizeof(t_list));
  63.             (*ptrptr)->value = value;
  64.             (*ptrptr)->next = NULL;
  65.         }
  66.     }
  67.     else
  68.     {
  69.             printf("\nList is empty, will insert %d at beginning\n", value);
  70.             *ptrptr = (t_list*)malloc(sizeof(t_list));
  71.             (*ptrptr)->value = value;
  72.             (*ptrptr)->next = NULL;
  73.     }
  74. }
  75.  
  76. void visit_leaf(t_tree * ptr, t_list ** ptrptr)
  77. {
  78.     if(ptr->left == NULL && ptr->right == NULL)
  79.     {
  80.         printf("\nLeaf value = %d\nI'm going to put it in your list\n", ptr->value);
  81.         list_insertion(ptrptr, ptr->value);
  82.     }
  83.     else
  84.     {
  85.         if(ptr->left != NULL)
  86.         {
  87.             visit_leaf(ptr->left, ptrptr);
  88.         }
  89.         if(ptr->right != NULL)
  90.         {
  91.             visit_leaf(ptr->right, ptrptr);
  92.         }
  93.     }
  94. }
  95.  
  96. t_tree* new_node()
  97. {
  98.     int data = 0;
  99.     printf("Insert value\n");
  100.     scanf("%d", &data);
  101.  
  102.     t_tree * tree = (t_tree *)malloc(sizeof(t_tree));
  103.  
  104.     tree->value = data;
  105.     tree->left = NULL;
  106.     tree->right = NULL;
  107.  
  108.     return(tree);
  109. }
  110.  
  111. void visit_list(t_list **ptrptr)
  112. {
  113.     while((*ptrptr) != NULL)
  114.     {
  115.         printf(" %d", (*ptrptr)->value);
  116.         ptrptr = &((*ptrptr)->next);
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement