From 047eaad687a0456905b1e1d21c07b4768cd363e8 Mon Sep 17 00:00:00 2001 From: Aarohi Verma <55794065+Aarohi99@users.noreply.github.com> Date: Wed, 7 Oct 2020 23:12:58 +0530 Subject: [PATCH] Merging of Linked Lists Merging of Linked Lists --- Merging of two linked lists | 96 +++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 Merging of two linked lists diff --git a/Merging of two linked lists b/Merging of two linked lists new file mode 100644 index 0000000..c515a9f --- /dev/null +++ b/Merging of two linked lists @@ -0,0 +1,96 @@ +#include +#include + +struct node { + int data; + struct node *next; +}; + +struct node *even = NULL; +struct node *odd = NULL; +struct node *list = NULL; + +//Create Linked List +void insert(int data) { + // Allocate memory for new node; + struct node *link = (struct node*) malloc(sizeof(struct node)); + struct node *current; + + link->data = data; + link->next = NULL; + + if(data%2 == 0) { + if(even == NULL) { + even = link; + return; + } else { + current = even; + + while(current->next != NULL) + current = current->next; + + // Insert link at the end of the list + current->next = link; + } + + } else { + if(odd == NULL) { + odd = link; + return; + } else { + current = odd; + + while(current->next!=NULL) + current = current->next; + + // Insert link at the end of the list + current->next = link; + } + } +} + +void display(struct node *head) { + struct node *ptr = head; + + printf("[head] =>"); + + while(ptr != NULL) { + printf(" %d =>",ptr->data); + ptr = ptr->next; + } + + printf(" [null]\n"); +} + +void combine() { + struct node *link; + + list = even; + link = list; + + while(link->next!= NULL) { + link = link->next; + } + + link->next = odd; +} + +int main() { + int i; + + for(i = 1; i <= 10; i++) + insert(i); + + printf("Even : "); + display(even); + + printf("Odd : "); + display(odd); + + combine(); + + printf("Combined List :\n"); + display(list); + + return 0; +}