Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@linked list be @edensheiko
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <malloc.h>
- #include <assert.h>
- typedef struct List // linked list def
- {
- int data;
- struct item *next;
- }list;
- int list_analyzer(list **even, list **odd);
- void print_list(list *head);
- void free_list(list *head);
- void main()
- {
- list *zogi;
- list *ezogi;
- int item = list_analyzer(&zogi,&ezogi);
- printf("the amount of elemetns is %d \n", item);
- printf("first list even: \n");
- print_list(zogi);
- printf("secand list odd:\n");
- print_list(ezogi);
- free_list(zogi);
- free_list(ezogi);
- system("pause");
- }
- int list_analyzer(list **even, list **odd)
- {
- int temp;
- int cout = 0; // used for countting elements
- list *head_even = NULL;
- list *curr_even = NULL;
- list *head_odd = NULL;
- list *curr_odd = NULL;
- while (1)
- {
- printf("please enter value\n");
- scanf("%d", &temp);
- if (temp == -1)
- {
- break;
- }
- cout++;
- if (temp % 2 == 0)
- {
- if (head_even == NULL)
- {
- head_even = (list*)malloc(sizeof(list)); //utlizing the first elemnt
- assert(head_even); // Memory leak cheak
- curr_even = head_even; // curr gets head place
- head_even->data = temp; // util of temp to the head of list
- }
- else // if head_even has value so we just moving the data by one
- {
- curr_even->next = (list*)malloc(sizeof(list)); //utlizing the sec elemnt
- assert(curr_even);// Memory leak cheak
- curr_even = curr_even->next;
- curr_even->data = temp;
- }
- }
- else
- {
- if (head_odd == NULL) // same as up just for the odd number
- {
- head_odd = (list*)malloc(sizeof(list));
- assert(head_odd);
- curr_odd = head_odd;
- curr_odd->data = temp;
- }
- else
- {
- curr_odd->next = (list*)malloc(sizeof(list));
- assert(curr_odd);
- curr_odd = curr_odd->next;
- curr_odd->data = temp;
- }
- }
- //// null opttimzuer
- //if (curr_odd)
- //{
- // curr_odd->next = NULL;
- //}
- //if (curr_even)
- //{
- // curr_even->next = NULL;
- //}
- /**even = head_even;
- *odd = head_odd;
- return cout;*/
- }
- // null opttimzuer
- if (curr_odd)
- {
- curr_odd->next = NULL;
- }
- if (curr_even)
- {
- curr_even->next = NULL;
- }
- //by ref even and odd
- *even = head_even;
- *odd = head_odd;
- return cout;
- }
- void print_list(list *head)
- {
- list *pos = head;
- while (pos)
- {
- printf(" %d -> ", pos->data);
- pos = pos->next;
- }
- }
- void free_list(list *head)
- {
- list *t_free = head;
- while (t_free!=NULL)
- {
- head = head->next;
- free(t_free);
- t_free = head;
- }
- }
Add Comment
Please, Sign In to add comment