-
Notifications
You must be signed in to change notification settings - Fork 2
/
question12.c
233 lines (205 loc) · 6.05 KB
/
question12.c
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
A linked list has three elements- data, link to next node and
one random pointer pointing to a random node. Clone the linked list.
//space required to represent output is not included since its demand of the que.
We only take into account the extra space that we use
Applications for cloning the list with random pointers:
1) Snakes and ladders game. The next pointer will point to the next number in the sequence but the random
pointer will point to greater random number or smaller depending on ladder or snake.
METHOD1:
We can copy the entire list first and then copy random ptrs because then the nodes pointed by the
random ptrs will exist. We will tell whether they exist or node basis the data in the node.
We will have to search for that particular node in the newly created node for every random node in
the linked list.
Disadvantages of using this method:
This method will fail if we have duplicate datas
Since comparision is to be made for data, if the data is some large thing, it will even be difficult to compare
it.
Time complexity: O(n^2)
Space complexity: O(1)
METHOD2:
We can store the mapping of addresses in the first list to the ones that we are creating respectively as
key value in a hash table and create the list. Then to make random nodes we can see what node points to which
one in the first list and corresponding mapping in the second list and build random nodes.
Time complexity: O(n)
Space complexity: O(n)
METHOD3:
Make each node corresponding to the first list by traversing. When a node is created, make the pointer of
the old list point to the corresponding node of the new list. Before that store the next node of the old list
in a temp variable (this variable will be overrided as we iterate). Also make the random node of new list point to the node of the old list.
Now keep repeating it. In the end you will have a route to each and every random node in the list and
you can assign your random nodes right path.
Disadvantages: You will loose the old list
Time complexity: O(n)
Space complexity: O(1)
METHOD4:
Traverse the first list and add all the new nodes just next to the node of the old list (in the middle)
everytime. Once that is done now you will have a reference to the older nodes random links. Traverse again
and update the random nodes of the newly created nodes that are in between the old nodes.
Now you can apply method of alternate split to separate the two linked lists.
Time complexity: O(n)
Space complexity: O(1)
*/
//METHOD1
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *arb;
struct node *link;
};
void makeList(struct node *t, int maxCounter, int mul){
int counter = 1;
struct node *temp = t;
while(counter <=maxCounter){
t->data = counter*mul;
if(counter == maxCounter){
t->link = NULL;
}else{
t->link = (struct node *)malloc(sizeof(struct node));
}
t = t->link;
counter++;
}
counter = 1;
t = temp;
while(counter <=maxCounter){
if(t && t->link && t->link->link){
t->arb = t->link->link;
}else if(t && t->link){
t->arb = t->link;
}
else if(temp->link){
t->arb = temp->link;
}
t=t->link;
counter++;
}
}
void cloneList(struct node *head, struct node *clone){
for(;head; head=head->link, clone=clone->link){
clone->data = head->data;
if(head->link == NULL){
clone->link = NULL;
}else{
clone->link = (struct node *)malloc(sizeof(struct node));
}
}
}
void printList(struct node *head){
if(head){
printf("(%d, %p) -->", head->data, head->arb);
printList(head->link);
}
printf("\n");
}
struct node *findPtrInClone(int val, struct node *clone){
for(;clone; clone=clone->link){
if(clone->data == val){
return clone;
}
}
return NULL;
}
void assignArb(struct node *head, struct node *clone){
int val, cloneVal;
struct node *clonehead = clone;
for(;head;head=head->link,clone=clone->link){
val = head->arb->data;
struct node *temp = findPtrInClone(val, clonehead);
clone->arb = temp;
}
}
int main(){
struct node *head = (struct node *)malloc(sizeof(struct node));
struct node *t = head;
makeList(t,8,100);
printList(head);
struct node *clone = (struct node *)malloc(sizeof(struct node));
cloneList(head, clone);
assignArb(head,clone);
printList(clone);
}
//================================================================================================
//METHOD2: hash table to be done later
//METHOD3: coming soon
//METHOD4
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *arb;
struct node *link;
};
void makeList(struct node *t, int maxCounter, int mul){
int counter = 1;
struct node *temp = t;
while(counter <=maxCounter){
t->data = counter*mul;
if(counter == maxCounter){
t->link = NULL;
}else{
t->link = (struct node *)malloc(sizeof(struct node));
}
t = t->link;
counter++;
}
counter = 1;
t = temp;
while(counter <=maxCounter){
if(t && t->link && t->link->link){
t->arb = t->link->link;
}else if(t && t->link){
t->arb = t->link;
}
else if(temp->link){
t->arb = temp->link;
}
t=t->link;
counter++;
}
}
void cloneList(struct node *head){
struct node *temp = head;
while(temp){
struct node *clone = (struct node *)malloc(sizeof(struct node));
clone->data = temp->data;
clone->link = temp->link;
temp->link = clone;
temp = clone->link;
}
}
void setRandomNode(struct node *t1){
while(t1){
t1->link->arb = t1->arb->link;
t1=t1->link->link;
}
}
void printList(struct node *head){
if(head){
printf("(%d,%p) -->", head->data, head->arb);
printList(head->link);
}
printf("\n");
}
struct node *separateClonedList(struct node *head1, struct node *head2){
struct node *temp = head2;
for(;head1 && head2 && head1->link && head2->link;
head1=head1->link, head2=head2->link){
head1->link = head2->link;
head2->link = head2->link->link;
}
head1->link = head2->link = NULL;
return temp;
}
int main(){
struct node *head = (struct node *)malloc(sizeof(struct node));
struct node *t = head;
makeList(t,8,100);
cloneList(head);
setRandomNode(head);
printList(head);
struct node *clone = separateClonedList(head, head->link);
printList(head);
printList(clone);
}