Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.49 KB | None | 0 0
  1. /*The program creates two linked lists:
  2. one of even numbers and one of odd numbers
  3. The program calculates sum, min, and max for each list*/
  4. #define _CRT_SECURE_NO_WARNINGS
  5. #include <stdio.h>
  6. #include <malloc.h>
  7.  
  8. typedef struct element
  9. {
  10.     int data;
  11.     struct element *ptr_next;
  12. } element;
  13.  
  14. /* 3 alternative solutions for even odd lists */
  15. int even_odd(element **, element **);
  16. int even_odd1(element **, element **);
  17. int even_odd2(element **, element **);
  18. int sum_min_max(element *lst, int *min, int *max);
  19. void print_list (element *lst);
  20. void free_list (element *lst);
  21. void insert_new_element_to_list(element **list, int x);
  22.  
  23. void main ()
  24. {
  25.     element *even_num_list, *odd_num_list;
  26.     int min_even, max_even, sum_even, min_odd, max_odd, sum_odd,
  27.        
  28.     num = even_odd (&even_num_list, &odd_num_list);
  29.     sum_even = sum_min_max (even_num_list, &min_even, &max_even);
  30.     sum_odd = sum_min_max (odd_num_list, &min_odd, &max_odd);
  31.     printf ("%d numbers:\n", num);
  32.     printf ("even_numbers: ");
  33.     print_list (even_num_list);
  34.     if (even_num_list)
  35.         printf ("\n(sum=%d min=%d max=%d)\n", sum_even, min_even, max_even);
  36.     else
  37.         printf ("\n(sum=%d)\n", sum_even);
  38.     printf ("odd_numbers: ");
  39.     print_list (odd_num_list);
  40.     if (odd_num_list)
  41.         printf ("\n(sum=%d min=%d max=%d)\n", sum_odd, min_odd, max_odd);
  42.     else
  43.         printf ("\n(sum=%d)\n", sum_odd);
  44.     free_list (even_num_list);
  45.     free_list (odd_num_list);
  46.  
  47.     /* Alternative solution for the even odd lists without a dummy element */
  48.     num = even_odd1(&even_num_list, &odd_num_list);
  49.     printf("even_numbers: ");
  50.     print_list(even_num_list);
  51.     printf("\nodd_numbers: ");
  52.     print_list(odd_num_list);
  53.     free_list(even_num_list);
  54.     free_list(odd_num_list);
  55.  
  56.     /* Alternative solution for the even odd lists without a dummy element
  57.        but the order of the elements of the list will be opposite than entered */
  58.     num = even_odd2(&even_num_list, &odd_num_list);
  59.     printf("even_numbers: ");
  60.     print_list(even_num_list);
  61.     printf("\nodd_numbers: ");
  62.     print_list(odd_num_list);
  63.     free_list(even_num_list);
  64.     free_list(odd_num_list);
  65.  
  66.     getchar();
  67. }
  68.  
  69. /* adding the new number to the END of the relevant list odd/even */
  70. int even_odd(element **even_list, element **odd_list)
  71. {
  72.     element *even_ptr, *odd_ptr; /* will point to the last element that was added to each list */
  73.     int count = 0, x;
  74.  
  75.     /* creating a dummy first element to avoid a special treatment for the first item in each list */
  76.     *even_list = (element *)malloc(sizeof(element));
  77.     *odd_list = (element *)malloc(sizeof(element));
  78.     even_ptr = *even_list;
  79.     odd_ptr = *odd_list;
  80.     printf("Enter numbers: ");
  81.     scanf("%d", &x);
  82.     while (x != -1)
  83.     {
  84.         if (x % 2 == 0)
  85.         {
  86.             even_ptr->ptr_next = (element *)malloc(sizeof(element));
  87.             even_ptr = even_ptr->ptr_next;
  88.             even_ptr->data = x;
  89.         }
  90.         else
  91.         {
  92.             odd_ptr->ptr_next = (element *)malloc(sizeof(element));
  93.             odd_ptr = odd_ptr->ptr_next;
  94.             odd_ptr->data = x;
  95.         }
  96.         count++;
  97.         scanf("%d", &x);
  98.     }
  99.     even_ptr->ptr_next = odd_ptr->ptr_next = NULL; /* adding NULL at the end of each list */
  100.  
  101.     /* removing dummy element in each list and free it,
  102.     each list will point to the first real element in the list */
  103.     even_ptr = *even_list;
  104.     *even_list = (*even_list)->ptr_next;
  105.     free(even_ptr);
  106.     odd_ptr = *odd_list;
  107.     *odd_list = (*odd_list)->ptr_next;
  108.     free(odd_ptr);
  109.     return count;
  110. }
  111.  
  112. /* adding the new number to the END of the relevant list odd/even */
  113. int even_odd1(element **even_list, element **odd_list)
  114. {
  115.     element *even_ptr, *odd_ptr; /* will point to the last element that was added
  116.                                  a special treatment for the first element that we add is required */
  117.     int count = 0, x;
  118.  
  119.     *even_list = *odd_list = NULL; /* at the beginning both lists are empty */
  120.     even_ptr = *even_list;
  121.     odd_ptr = *odd_list;
  122.     printf("\nEnter numbers: ");
  123.     scanf("%d", &x);
  124.     while (x != -1)
  125.     {
  126.         if (x % 2 == 0)
  127.         {
  128.             if (*even_list)
  129.             {
  130.                 /* not first element in the list */
  131.                 even_ptr->ptr_next = (element *)malloc(sizeof(element));
  132.                 even_ptr = even_ptr->ptr_next;
  133.                 even_ptr->data = x;
  134.             }
  135.             else
  136.             {
  137.                 /* first element in the list */
  138.                 even_ptr = (element *)malloc(sizeof(element));
  139.                 even_ptr->data = x;
  140.                 *even_list = even_ptr; /* the even list will point on the first element */
  141.             }
  142.         }
  143.         else
  144.         {
  145.             if (*odd_list)
  146.             {
  147.                 /* not first element in the list */
  148.                 odd_ptr->ptr_next = (element *)malloc(sizeof(element));
  149.                 odd_ptr = odd_ptr->ptr_next;
  150.                 odd_ptr->data = x;
  151.             }
  152.             else
  153.             {
  154.                 /* first element in the list */
  155.                 odd_ptr = (element *)malloc(sizeof(element));
  156.                 odd_ptr->data = x;
  157.                 *odd_list = odd_ptr; /* the odd list will point on the first element */
  158.             }
  159.         }
  160.         count++;
  161.         scanf("%d", &x);
  162.     }
  163.  
  164.     /* pointing the end of the lists to NULL*/
  165.     if (even_ptr)
  166.         even_ptr->ptr_next = NULL;
  167.     if (odd_ptr)
  168.         odd_ptr->ptr_next = NULL;
  169.  
  170.     getchar();
  171.     return count;
  172. }
  173.  
  174.  
  175. void insert_new_element_to_list(element **list, int x)
  176. {
  177.     element *tmp_ptr;
  178.     if (*list)
  179.     {
  180.         /* not first element in the list */
  181.         tmp_ptr = *list; /* will save a pinter to the first element */
  182.         *list = (element *)malloc(sizeof(element)); /* the head of the list will point to the new element */
  183.         (*list)->ptr_next = tmp_ptr; /* the new first element will point on the old first element */
  184.         (*list)->data = x;
  185.     }
  186.     else
  187.     {
  188.         /* first element in the list */
  189.         *list = (element *)malloc(sizeof(element));
  190.         (*list)->data = x;
  191.         (*list)->ptr_next = NULL;
  192.     }
  193.     return;
  194. }
  195.  
  196.  
  197. /* adding the new number to the BEGINNING of the relevant list odd/even */
  198. int even_odd2 (element **even_list, element **odd_list)
  199. {
  200.     int count = 0, x;
  201.  
  202.     *even_list = *odd_list = NULL; /* at the beginning both lists are empty */
  203.  
  204.     printf ("\nEnter numbers: ");
  205.     scanf ("%d", &x);
  206.     while (x != -1)
  207.     {
  208.         if (x % 2 == 0)
  209.         {
  210.             insert_new_element_to_list(even_list, x);
  211.         }
  212.         else
  213.         {
  214.             insert_new_element_to_list(odd_list, x);
  215.         }
  216.         count++;
  217.         scanf ("%d", &x);
  218.     }
  219.  
  220.     getchar();
  221.     return count;
  222. }
  223.  
  224. int sum_min_max (element *lst, int *min, int *max)
  225. {
  226.     int sum = 0;
  227.     if (lst)
  228.         *min = *max = lst->data;
  229.     while (lst)
  230.     {
  231.         if (lst->data < *min)
  232.             *min = lst->data;
  233.         else
  234.             if (lst->data > *max)
  235.                 *max = lst->data;
  236.         sum += lst->data;
  237.         lst = lst->ptr_next;
  238.     }
  239.     return sum;
  240. }
  241.    
  242.  
  243. void print_list (element *lst)
  244. {
  245.     while (lst)
  246.     {
  247.         printf ("%d ", lst->data);
  248.         lst = lst->ptr_next;
  249.     }
  250. }
  251.  
  252. void free_list (element *lst)
  253. {
  254.     element *temp;
  255.  
  256.     while (lst)
  257.     {
  258.         temp = lst;
  259.         lst = lst->ptr_next;
  260.         free (temp);
  261.     }
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement