Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*The program creates two linked lists:
- one of even numbers and one of odd numbers
- The program calculates sum, min, and max for each list*/
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <malloc.h>
- typedef struct element
- {
- int data;
- struct element *ptr_next;
- } element;
- /* 3 alternative solutions for even odd lists */
- int even_odd(element **, element **);
- int even_odd1(element **, element **);
- int even_odd2(element **, element **);
- int sum_min_max(element *lst, int *min, int *max);
- void print_list (element *lst);
- void free_list (element *lst);
- void insert_new_element_to_list(element **list, int x);
- void main ()
- {
- element *even_num_list, *odd_num_list;
- int min_even, max_even, sum_even, min_odd, max_odd, sum_odd,
- num = even_odd (&even_num_list, &odd_num_list);
- sum_even = sum_min_max (even_num_list, &min_even, &max_even);
- sum_odd = sum_min_max (odd_num_list, &min_odd, &max_odd);
- printf ("%d numbers:\n", num);
- printf ("even_numbers: ");
- print_list (even_num_list);
- if (even_num_list)
- printf ("\n(sum=%d min=%d max=%d)\n", sum_even, min_even, max_even);
- else
- printf ("\n(sum=%d)\n", sum_even);
- printf ("odd_numbers: ");
- print_list (odd_num_list);
- if (odd_num_list)
- printf ("\n(sum=%d min=%d max=%d)\n", sum_odd, min_odd, max_odd);
- else
- printf ("\n(sum=%d)\n", sum_odd);
- free_list (even_num_list);
- free_list (odd_num_list);
- /* Alternative solution for the even odd lists without a dummy element */
- num = even_odd1(&even_num_list, &odd_num_list);
- printf("even_numbers: ");
- print_list(even_num_list);
- printf("\nodd_numbers: ");
- print_list(odd_num_list);
- free_list(even_num_list);
- free_list(odd_num_list);
- /* Alternative solution for the even odd lists without a dummy element
- but the order of the elements of the list will be opposite than entered */
- num = even_odd2(&even_num_list, &odd_num_list);
- printf("even_numbers: ");
- print_list(even_num_list);
- printf("\nodd_numbers: ");
- print_list(odd_num_list);
- free_list(even_num_list);
- free_list(odd_num_list);
- getchar();
- }
- /* adding the new number to the END of the relevant list odd/even */
- int even_odd(element **even_list, element **odd_list)
- {
- element *even_ptr, *odd_ptr; /* will point to the last element that was added to each list */
- int count = 0, x;
- /* creating a dummy first element to avoid a special treatment for the first item in each list */
- *even_list = (element *)malloc(sizeof(element));
- *odd_list = (element *)malloc(sizeof(element));
- even_ptr = *even_list;
- odd_ptr = *odd_list;
- printf("Enter numbers: ");
- scanf("%d", &x);
- while (x != -1)
- {
- if (x % 2 == 0)
- {
- even_ptr->ptr_next = (element *)malloc(sizeof(element));
- even_ptr = even_ptr->ptr_next;
- even_ptr->data = x;
- }
- else
- {
- odd_ptr->ptr_next = (element *)malloc(sizeof(element));
- odd_ptr = odd_ptr->ptr_next;
- odd_ptr->data = x;
- }
- count++;
- scanf("%d", &x);
- }
- even_ptr->ptr_next = odd_ptr->ptr_next = NULL; /* adding NULL at the end of each list */
- /* removing dummy element in each list and free it,
- each list will point to the first real element in the list */
- even_ptr = *even_list;
- *even_list = (*even_list)->ptr_next;
- free(even_ptr);
- odd_ptr = *odd_list;
- *odd_list = (*odd_list)->ptr_next;
- free(odd_ptr);
- return count;
- }
- /* adding the new number to the END of the relevant list odd/even */
- int even_odd1(element **even_list, element **odd_list)
- {
- element *even_ptr, *odd_ptr; /* will point to the last element that was added
- a special treatment for the first element that we add is required */
- int count = 0, x;
- *even_list = *odd_list = NULL; /* at the beginning both lists are empty */
- even_ptr = *even_list;
- odd_ptr = *odd_list;
- printf("\nEnter numbers: ");
- scanf("%d", &x);
- while (x != -1)
- {
- if (x % 2 == 0)
- {
- if (*even_list)
- {
- /* not first element in the list */
- even_ptr->ptr_next = (element *)malloc(sizeof(element));
- even_ptr = even_ptr->ptr_next;
- even_ptr->data = x;
- }
- else
- {
- /* first element in the list */
- even_ptr = (element *)malloc(sizeof(element));
- even_ptr->data = x;
- *even_list = even_ptr; /* the even list will point on the first element */
- }
- }
- else
- {
- if (*odd_list)
- {
- /* not first element in the list */
- odd_ptr->ptr_next = (element *)malloc(sizeof(element));
- odd_ptr = odd_ptr->ptr_next;
- odd_ptr->data = x;
- }
- else
- {
- /* first element in the list */
- odd_ptr = (element *)malloc(sizeof(element));
- odd_ptr->data = x;
- *odd_list = odd_ptr; /* the odd list will point on the first element */
- }
- }
- count++;
- scanf("%d", &x);
- }
- /* pointing the end of the lists to NULL*/
- if (even_ptr)
- even_ptr->ptr_next = NULL;
- if (odd_ptr)
- odd_ptr->ptr_next = NULL;
- getchar();
- return count;
- }
- void insert_new_element_to_list(element **list, int x)
- {
- element *tmp_ptr;
- if (*list)
- {
- /* not first element in the list */
- tmp_ptr = *list; /* will save a pinter to the first element */
- *list = (element *)malloc(sizeof(element)); /* the head of the list will point to the new element */
- (*list)->ptr_next = tmp_ptr; /* the new first element will point on the old first element */
- (*list)->data = x;
- }
- else
- {
- /* first element in the list */
- *list = (element *)malloc(sizeof(element));
- (*list)->data = x;
- (*list)->ptr_next = NULL;
- }
- return;
- }
- /* adding the new number to the BEGINNING of the relevant list odd/even */
- int even_odd2 (element **even_list, element **odd_list)
- {
- int count = 0, x;
- *even_list = *odd_list = NULL; /* at the beginning both lists are empty */
- printf ("\nEnter numbers: ");
- scanf ("%d", &x);
- while (x != -1)
- {
- if (x % 2 == 0)
- {
- insert_new_element_to_list(even_list, x);
- }
- else
- {
- insert_new_element_to_list(odd_list, x);
- }
- count++;
- scanf ("%d", &x);
- }
- getchar();
- return count;
- }
- int sum_min_max (element *lst, int *min, int *max)
- {
- int sum = 0;
- if (lst)
- *min = *max = lst->data;
- while (lst)
- {
- if (lst->data < *min)
- *min = lst->data;
- else
- if (lst->data > *max)
- *max = lst->data;
- sum += lst->data;
- lst = lst->ptr_next;
- }
- return sum;
- }
- void print_list (element *lst)
- {
- while (lst)
- {
- printf ("%d ", lst->data);
- lst = lst->ptr_next;
- }
- }
- void free_list (element *lst)
- {
- element *temp;
- while (lst)
- {
- temp = lst;
- lst = lst->ptr_next;
- free (temp);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement