Skip to content

Commit 08fac3e

Browse files
committed
minor changes
1 parent 3827532 commit 08fac3e

File tree

1 file changed

+87
-89
lines changed

1 file changed

+87
-89
lines changed

linked-lists/question12.c

Lines changed: 87 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -48,93 +48,93 @@ Space complexity: O(1)
4848
*/
4949

5050
//METHOD1
51-
// #include <stdio.h>
52-
// #include <stdlib.h>
53-
54-
// struct node{
55-
// int data;
56-
// struct node *arb;
57-
// struct node *link;
58-
// };
59-
60-
// void makeList(struct node *t, int maxCounter, int mul){
61-
// int counter = 1;
62-
// struct node *temp = t;
63-
// while(counter <=maxCounter){
64-
// t->data = counter*mul;
65-
// if(counter == maxCounter){
66-
// t->link = NULL;
67-
// }else{
68-
// t->link = (struct node *)malloc(sizeof(struct node));
69-
// }
70-
// t = t->link;
71-
// counter++;
72-
// }
73-
// counter = 1;
74-
// t = temp;
75-
// while(counter <=maxCounter){
76-
// if(t && t->link && t->link->link){
77-
// t->arb = t->link->link;
78-
// }else if(t && t->link){
79-
// t->arb = t->link;
80-
// }
81-
// else if(temp->link){
82-
// t->arb = temp->link;
83-
// }
84-
// t=t->link;
85-
// counter++;
86-
// }
87-
// }
88-
89-
// void cloneList(struct node *head, struct node *clone){
90-
// for(;head; head=head->link, clone=clone->link){
91-
// clone->data = head->data;
92-
// if(head->link == NULL){
93-
// clone->link = NULL;
94-
// }else{
95-
// clone->link = (struct node *)malloc(sizeof(struct node));
96-
// }
97-
// }
98-
// }
99-
100-
// void printList(struct node *head){
101-
// if(head){
102-
// printf("(%d, %p) -->", head->data, head->arb);
103-
// printList(head->link);
104-
// }
105-
// printf("\n");
106-
// }
107-
108-
// struct node *findPtrInClone(int val, struct node *clone){
109-
// for(;clone; clone=clone->link){
110-
// if(clone->data == val){
111-
// return clone;
112-
// }
113-
// }
114-
// return NULL;
115-
// }
116-
117-
// void assignArb(struct node *head, struct node *clone){
118-
// int val, cloneVal;
119-
// struct node *clonehead = clone;
120-
// for(;head;head=head->link,clone=clone->link){
121-
// val = head->arb->data;
122-
// struct node *temp = findPtrInClone(val, clonehead);
123-
// clone->arb = temp;
124-
// }
125-
// }
126-
127-
// int main(){
128-
129-
// struct node *head = (struct node *)malloc(sizeof(struct node));
130-
// struct node *t = head;
131-
// makeList(t,8,100);
132-
// printList(head);
133-
// struct node *clone = (struct node *)malloc(sizeof(struct node));
134-
// cloneList(head, clone);
135-
// assignArb(head,clone);
136-
// printList(clone);
137-
// }
51+
#include <stdio.h>
52+
#include <stdlib.h>
53+
54+
struct node{
55+
int data;
56+
struct node *arb;
57+
struct node *link;
58+
};
59+
60+
void makeList(struct node *t, int maxCounter, int mul){
61+
int counter = 1;
62+
struct node *temp = t;
63+
while(counter <=maxCounter){
64+
t->data = counter*mul;
65+
if(counter == maxCounter){
66+
t->link = NULL;
67+
}else{
68+
t->link = (struct node *)malloc(sizeof(struct node));
69+
}
70+
t = t->link;
71+
counter++;
72+
}
73+
counter = 1;
74+
t = temp;
75+
while(counter <=maxCounter){
76+
if(t && t->link && t->link->link){
77+
t->arb = t->link->link;
78+
}else if(t && t->link){
79+
t->arb = t->link;
80+
}
81+
else if(temp->link){
82+
t->arb = temp->link;
83+
}
84+
t=t->link;
85+
counter++;
86+
}
87+
}
88+
89+
void cloneList(struct node *head, struct node *clone){
90+
for(;head; head=head->link, clone=clone->link){
91+
clone->data = head->data;
92+
if(head->link == NULL){
93+
clone->link = NULL;
94+
}else{
95+
clone->link = (struct node *)malloc(sizeof(struct node));
96+
}
97+
}
98+
}
99+
100+
void printList(struct node *head){
101+
if(head){
102+
printf("(%d, %p) -->", head->data, head->arb);
103+
printList(head->link);
104+
}
105+
printf("\n");
106+
}
107+
108+
struct node *findPtrInClone(int val, struct node *clone){
109+
for(;clone; clone=clone->link){
110+
if(clone->data == val){
111+
return clone;
112+
}
113+
}
114+
return NULL;
115+
}
116+
117+
void assignArb(struct node *head, struct node *clone){
118+
int val, cloneVal;
119+
struct node *clonehead = clone;
120+
for(;head;head=head->link,clone=clone->link){
121+
val = head->arb->data;
122+
struct node *temp = findPtrInClone(val, clonehead);
123+
clone->arb = temp;
124+
}
125+
}
126+
127+
int main(){
128+
129+
struct node *head = (struct node *)malloc(sizeof(struct node));
130+
struct node *t = head;
131+
makeList(t,8,100);
132+
printList(head);
133+
struct node *clone = (struct node *)malloc(sizeof(struct node));
134+
cloneList(head, clone);
135+
assignArb(head,clone);
136+
printList(clone);
137+
}
138138

139139
//================================================================================================
140140
//METHOD2: hash table to be done later
@@ -222,9 +222,7 @@ int main(){
222222
struct node *head = (struct node *)malloc(sizeof(struct node));
223223
struct node *t = head;
224224
makeList(t,8,100);
225-
// printList(head);
226225
cloneList(head);
227-
// printList(head);
228226
setRandomNode(head);
229227
printList(head);
230228

0 commit comments

Comments
 (0)