-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunionintersectionLL.cpp
129 lines (111 loc) · 2.93 KB
/
unionintersectionLL.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// C/C++ program to find union and intersection of two unsorted
// linked lists
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
/* Link list node */
struct Node
{
int data;
struct Node* next;
};
/* A utility function to insert a node at the beginning of
a linked list*/
void push(struct Node** head_ref, int new_data);
/* A utility function to check if given data is present in a list */
bool ispresent(struct Node *head, int data)
{
struct Node *temp = head;
while(temp != NULL)
{
if(temp->data == data)
return true;
temp = temp->next;
}
return false;
}
/* Function to get union of two linked lists head1 and head2 */
struct Node *getUnion(struct Node *head1, struct Node *head2)
{
struct Node *uniref = NULL;
struct Node *temp1 = head1;
struct Node *temp2 = head2;
while(temp1 != NULL)
{
push(&uniref,temp1->data);
temp1 = temp1->next;
}
while(temp2 != NULL)
{
if(!(ispresent(uniref,temp2->data)))
push(&uniref,temp2->data);
temp2 = temp2->next;
}
return uniref;
}
/* Function to get intersection of two linked lists
head1 and head2 */
struct Node *getIntersection(struct Node *head1,
struct Node *head2)
{
struct Node *intref = NULL;
while(head2 != NULL)
{
if(ispresent(head1,head2->data))
push(&intref,head2->data);
head2 = head2->next;
}
return intref;
}
/* A utility function to insert a node at the begining of a linked list*/
void push (struct Node** head_ref, int new_data)
{
/* allocate node */
struct Node* new_node =
(struct Node*) malloc(sizeof(struct Node));
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
/* A utility function to print a linked list*/
void printList (struct Node *node)
{
while (node != NULL)
{
printf ("%d ", node->data);
node = node->next;
}
}
/* Drier program to test above function*/
int main()
{
/* Start with the empty list */
struct Node* head1 = NULL;
struct Node* head2 = NULL;
struct Node* intersecn = NULL;
struct Node* unin = NULL;
/*create a linked lits 10->15->5->20 */
push (&head1, 20);
push (&head1, 4);
push (&head1, 15);
push (&head1, 10);
/*create a linked lits 8->4->2->10 */
push (&head2, 10);
push (&head2, 2);
push (&head2, 4);
push (&head2, 8);
intersecn = getIntersection (head1, head2);
unin = getUnion (head1, head2);
printf ("\n First list is \n");
printList (head1);
printf ("\n Second list is \n");
printList (head2);
printf ("\n Intersection list is \n");
printList (intersecn);
printf ("\n Union list is \n");
printList (unin);
return 0;
}