Update DS/C/Lab/Week8/DLLUnionIntersec.c
This commit is contained in:
parent
d15b9d0359
commit
8e68c6a751
@ -1,5 +1,3 @@
|
|||||||
// Write a program to implement union and intersection of two doubly linked lists.
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
@ -14,7 +12,7 @@ typedef struct node *NODE;
|
|||||||
NODE getnode() {
|
NODE getnode() {
|
||||||
NODE x;
|
NODE x;
|
||||||
x = (NODE)malloc(sizeof(struct node));
|
x = (NODE)malloc(sizeof(struct node));
|
||||||
if(x == NULL) {
|
if (x == NULL) {
|
||||||
printf("Out of memory\n");
|
printf("Out of memory\n");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
@ -26,13 +24,13 @@ void insert_rear(NODE *head, int item) {
|
|||||||
temp = getnode();
|
temp = getnode();
|
||||||
temp->data = item;
|
temp->data = item;
|
||||||
temp->next = NULL;
|
temp->next = NULL;
|
||||||
if(*head == NULL) {
|
if (*head == NULL) {
|
||||||
temp->prev = NULL;
|
temp->prev = NULL;
|
||||||
*head = temp;
|
*head = temp;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
cur = *head;
|
cur = *head;
|
||||||
while(cur->next != NULL)
|
while (cur->next != NULL)
|
||||||
cur = cur->next;
|
cur = cur->next;
|
||||||
cur->next = temp;
|
cur->next = temp;
|
||||||
temp->prev = cur;
|
temp->prev = cur;
|
||||||
@ -41,22 +39,22 @@ void insert_rear(NODE *head, int item) {
|
|||||||
NODE union_lists(NODE head1, NODE head2) {
|
NODE union_lists(NODE head1, NODE head2) {
|
||||||
NODE result = NULL;
|
NODE result = NULL;
|
||||||
NODE temp = head1;
|
NODE temp = head1;
|
||||||
while(temp != NULL) {
|
while (temp != NULL) {
|
||||||
insert_rear(&result, temp->data);
|
insert_rear(&result, temp->data);
|
||||||
temp = temp->next;
|
temp = temp->next;
|
||||||
}
|
}
|
||||||
temp = head2;
|
temp = head2;
|
||||||
while(temp != NULL) {
|
while (temp != NULL) {
|
||||||
NODE cur = result;
|
NODE cur = result;
|
||||||
int found = 0;
|
int found = 0;
|
||||||
while(cur != NULL) {
|
while (cur != NULL) {
|
||||||
if(cur->data == temp->data) {
|
if (cur->data == temp->data) {
|
||||||
found = 1;
|
found = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
cur = cur->next;
|
cur = cur->next;
|
||||||
}
|
}
|
||||||
if(!found)
|
if (!found)
|
||||||
insert_rear(&result, temp->data);
|
insert_rear(&result, temp->data);
|
||||||
temp = temp->next;
|
temp = temp->next;
|
||||||
}
|
}
|
||||||
@ -66,10 +64,10 @@ NODE union_lists(NODE head1, NODE head2) {
|
|||||||
NODE intersection_lists(NODE head1, NODE head2) {
|
NODE intersection_lists(NODE head1, NODE head2) {
|
||||||
NODE result = NULL;
|
NODE result = NULL;
|
||||||
NODE temp = head1;
|
NODE temp = head1;
|
||||||
while(temp != NULL) {
|
while (temp != NULL) {
|
||||||
NODE cur = head2;
|
NODE cur = head2;
|
||||||
while(cur != NULL) {
|
while (cur != NULL) {
|
||||||
if(temp->data == cur->data) {
|
if (temp->data == cur->data) {
|
||||||
insert_rear(&result, temp->data);
|
insert_rear(&result, temp->data);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -82,9 +80,42 @@ NODE intersection_lists(NODE head1, NODE head2) {
|
|||||||
|
|
||||||
void display(NODE head) {
|
void display(NODE head) {
|
||||||
NODE temp = head;
|
NODE temp = head;
|
||||||
while(temp != NULL) {
|
while (temp != NULL) {
|
||||||
printf("%d ", temp->data);
|
printf("%d ", temp->data);
|
||||||
temp = temp->next;
|
temp = temp->next;
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
NODE list1 = NULL;
|
||||||
|
NODE list2 = NULL;
|
||||||
|
|
||||||
|
// Inserting elements into list1
|
||||||
|
insert_rear(&list1, 1);
|
||||||
|
insert_rear(&list1, 2);
|
||||||
|
insert_rear(&list1, 3);
|
||||||
|
|
||||||
|
// Inserting elements into list2
|
||||||
|
insert_rear(&list2, 3);
|
||||||
|
insert_rear(&list2, 4);
|
||||||
|
insert_rear(&list2, 5);
|
||||||
|
|
||||||
|
// Displaying the lists
|
||||||
|
printf("List 1: ");
|
||||||
|
display(list1);
|
||||||
|
printf("List 2: ");
|
||||||
|
display(list2);
|
||||||
|
|
||||||
|
// Union of the lists
|
||||||
|
NODE union_result = union_lists(list1, list2);
|
||||||
|
printf("Union: ");
|
||||||
|
display(union_result);
|
||||||
|
|
||||||
|
// Intersection of the lists
|
||||||
|
NODE intersection_result = intersection_lists(list1, list2);
|
||||||
|
printf("Intersection: ");
|
||||||
|
display(intersection_result);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user