-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.7(intersection).cpp
More file actions
141 lines (114 loc) · 2.68 KB
/
2.7(intersection).cpp
File metadata and controls
141 lines (114 loc) · 2.68 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
#include<stdio.h>
#include<stdlib.h>
#include <iostream>
using namespace std;
/* Link list node */
struct node
{
int data;
struct node* next;
};
/* Function to get the counts of node in a linked list */
/* Takes head pointer of the linked list and
returns the count of nodes in the list */
/* function to get the intersection point of two linked
lists head1 and head2 where head1 has d more nodes than
head2 */
/* function to get the intersection point of two linked
lists head1 and head2 */
/* function to get the intersection point of two linked
lists head1 and head2 where head1 has d more nodes than
head2 */
void makeSameSize(struct node* &list, struct node* &list1){
struct node* temp;
temp = list;
int count = 0;
while(temp->next!=NULL){
count++;
temp=temp->next;
}
struct node* temp1;
temp1 = list1;
int cnt = 0;
while(temp1->next!=NULL){
cnt++;
temp1=temp1->next;
}
if(temp1!=temp){
throw invalid_argument( "The lists are not in intersection" );
}
int dif =0;
struct node* t;
if(count>cnt){
dif=count-cnt;
t=list;
for(int i=0; i<dif; i++){
list=list->next;
}
}
if(cnt>count){
dif = cnt-count;
for(int i=0; i<dif; i++){
list1=list1->next;
}
}
}
void display(struct node* list){
struct node* temp = list;
while(temp!=NULL){
cout<<temp->data<<" ";
temp= temp->next;
}
}
int getIntesectionNode(struct node* head1, struct node* head2)
{
makeSameSize(head1,head2);
struct node* h1= head1;
struct node* h2= head2;
struct node* intersection = new node();
while(h1!= NULL & h2!=NULL){
if(h1==h2){
intersection = h1;
return intersection->data;
}
h1=h1->next;
h2=h2->next;
}
throw invalid_argument( "The lists are not in intersection" );
return -1;
}
/* IGNORE THE BELOW LINES OF CODE. THESE LINES
ARE JUST TO QUICKLY TEST THE ABOVE FUNCTION */
int main()
{
/*
Create two linked lists
1st 3->6->9->15->30
2nd 10->15->30
15 is the intersection point
*/
struct node* newNode;
struct node* head1 =
(struct node*) malloc(sizeof(struct node));
head1->data = 10;
struct node* head2 =
(struct node*) malloc(sizeof(struct node));
head2->data = 3;
newNode = (struct node*) malloc (sizeof(struct node));
newNode->data = 6;
head2->next = newNode;
newNode = (struct node*) malloc (sizeof(struct node));
newNode->data = 9;
head2->next->next = newNode;
newNode = (struct node*) malloc (sizeof(struct node));
newNode->data = 15;
head1->next = newNode;
head2->next->next->next = newNode;
newNode = (struct node*) malloc (sizeof(struct node));
newNode->data = 30;
head1->next->next= newNode;
head1->next->next->next = NULL;
printf("\n The node of intersection is %d \n",
getIntesectionNode(head1, head2));
return 0;
}