|
| 1 | +// C/C++ program to find union |
| 2 | +// and intersection of two unsorted |
| 3 | +// linked lists |
| 4 | +#include <stdbool.h> |
| 5 | +#include <stdio.h> |
| 6 | +#include <stdlib.h> |
| 7 | +/* Link list node */ |
| 8 | +struct Node { |
| 9 | + int data; |
| 10 | + struct Node* next; |
| 11 | +}; |
| 12 | + |
| 13 | +/* A utility function to insert a |
| 14 | +node at the beginning ofa linked list*/ |
| 15 | +void push(struct Node** head_ref, int new_data); |
| 16 | + |
| 17 | +/* A utility function to check if |
| 18 | +given data is present in a list */ |
| 19 | +bool isPresent(struct Node* head, int data); |
| 20 | + |
| 21 | +/* Function to get union of two |
| 22 | +linked lists head1 and head2 */ |
| 23 | +struct Node* getUnion( |
| 24 | + struct Node* head1, |
| 25 | + struct Node* head2) |
| 26 | +{ |
| 27 | + struct Node* result = NULL; |
| 28 | + struct Node *t1 = head1, *t2 = head2; |
| 29 | + |
| 30 | + // Insert all elements of |
| 31 | + // list1 to the result list |
| 32 | + while (t1 != NULL) { |
| 33 | + push(&result, t1->data); |
| 34 | + t1 = t1->next; |
| 35 | + } |
| 36 | + |
| 37 | + // Insert those elements of list2 |
| 38 | + // which are not present in result list |
| 39 | + while (t2 != NULL) { |
| 40 | + if (!isPresent(result, t2->data)) |
| 41 | + push(&result, t2->data); |
| 42 | + t2 = t2->next; |
| 43 | + } |
| 44 | + |
| 45 | + return result; |
| 46 | +} |
| 47 | + |
| 48 | +/* Function to get intersection of |
| 49 | +two linked lists head1 and head2 */ |
| 50 | +struct Node* getIntersection(struct Node* head1, |
| 51 | + struct Node* head2) |
| 52 | +{ |
| 53 | + struct Node* result = NULL; |
| 54 | + struct Node* t1 = head1; |
| 55 | + |
| 56 | + // Traverse list1 and search each element of it in |
| 57 | + // list2. If the element is present in list 2, then |
| 58 | + // insert the element to result |
| 59 | + while (t1 != NULL) { |
| 60 | + if (isPresent(head2, t1->data)) |
| 61 | + push(&result, t1->data); |
| 62 | + t1 = t1->next; |
| 63 | + } |
| 64 | + |
| 65 | + return result; |
| 66 | +} |
| 67 | + |
| 68 | +/* A utility function to insert a |
| 69 | +node at the beginning of a linked list*/ |
| 70 | +void push(struct Node** head_ref, int new_data) |
| 71 | +{ |
| 72 | + /* allocate node */ |
| 73 | + struct Node* new_node |
| 74 | + = (struct Node*)malloc( |
| 75 | + sizeof(struct Node)); |
| 76 | + |
| 77 | + /* put in the data */ |
| 78 | + new_node->data = new_data; |
| 79 | + |
| 80 | + /* link the old list off the new node */ |
| 81 | + new_node->next = (*head_ref); |
| 82 | + |
| 83 | + /* move the head to point to the new node */ |
| 84 | + (*head_ref) = new_node; |
| 85 | +} |
| 86 | + |
| 87 | +/* A utility function to print a linked list*/ |
| 88 | +void printList(struct Node* node) |
| 89 | +{ |
| 90 | + while (node != NULL) { |
| 91 | + printf("%d ", node->data); |
| 92 | + node = node->next; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +/* A utility function that returns true if data is |
| 97 | +present in linked list else return false */ |
| 98 | +bool isPresent(struct Node* head, int data) |
| 99 | +{ |
| 100 | + struct Node* t = head; |
| 101 | + while (t != NULL) { |
| 102 | + if (t->data == data) |
| 103 | + return 1; |
| 104 | + t = t->next; |
| 105 | + } |
| 106 | + return 0; |
| 107 | +} |
| 108 | + |
| 109 | +/* Driver program to test above function*/ |
| 110 | +int main() |
| 111 | +{ |
| 112 | + /* Start with the empty list */ |
| 113 | + struct Node* head1 = NULL; |
| 114 | + struct Node* head2 = NULL; |
| 115 | + struct Node* intersecn = NULL; |
| 116 | + struct Node* unin = NULL; |
| 117 | + |
| 118 | + /*create a linked lits 10->15->5->20 */ |
| 119 | + push(&head1, 20); |
| 120 | + push(&head1, 4); |
| 121 | + push(&head1, 15); |
| 122 | + push(&head1, 10); |
| 123 | + |
| 124 | + /*create a linked lits 8->4->2->10 */ |
| 125 | + push(&head2, 10); |
| 126 | + push(&head2, 2); |
| 127 | + push(&head2, 4); |
| 128 | + push(&head2, 8); |
| 129 | + |
| 130 | + intersecn = getIntersection(head1, head2); |
| 131 | + unin = getUnion(head1, head2); |
| 132 | + |
| 133 | + printf("\n First list is \n"); |
| 134 | + printList(head1); |
| 135 | + |
| 136 | + printf("\n Second list is \n"); |
| 137 | + printList(head2); |
| 138 | + |
| 139 | + printf("\n Intersection list is \n"); |
| 140 | + printList(intersecn); |
| 141 | + |
| 142 | + printf("\n Union list is \n"); |
| 143 | + printList(unin); |
| 144 | + |
| 145 | + return 0; |
| 146 | +} |
0 commit comments