Skip to content

Commit 3827532

Browse files
committed
new node added
1 parent bf33db0 commit 3827532

File tree

2 files changed

+130
-31
lines changed

2 files changed

+130
-31
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ TODO:
6666
- program to add, mult, subtract two numbers stored in two different linked list and store the result in another linked list
6767
- storing addresses of a linked list in hash table
6868
- storing addresses of a linked list in stack and doing operations
69+
- hash table implementation of question12 to be done
6970

7071

7172
### General Questions

linked-lists/question12.c

Lines changed: 129 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,100 @@ Time complexity: O(n)
4747
Space complexity: O(1)
4848
*/
4949

50+
//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+
// }
138+
139+
//================================================================================================
140+
//METHOD2: hash table to be done later
141+
//METHOD3: coming soon
142+
143+
//METHOD4
50144
#include <stdio.h>
51145
#include <stdlib.h>
52146

@@ -85,53 +179,57 @@ void makeList(struct node *t, int maxCounter, int mul){
85179
}
86180
}
87181

88-
void cloneList(struct node *head, struct node *clone){
89-
for(;head; head=head->link, clone=clone->link){
90-
clone->data = head->data;
91-
if(head->link == NULL){
92-
clone->link = NULL;
93-
}else{
94-
clone->link = (struct node *)malloc(sizeof(struct node));
95-
}
182+
void cloneList(struct node *head){
183+
struct node *temp = head;
184+
while(temp){
185+
struct node *clone = (struct node *)malloc(sizeof(struct node));
186+
clone->data = temp->data;
187+
clone->link = temp->link;
188+
temp->link = clone;
189+
temp = clone->link;
190+
}
191+
}
192+
193+
void setRandomNode(struct node *t1){
194+
while(t1){
195+
t1->link->arb = t1->arb->link;
196+
t1=t1->link->link;
96197
}
198+
97199
}
98200

99201
void printList(struct node *head){
100202
if(head){
101-
printf("(%d, %p) -->", head->data, head->arb);
203+
printf("(%d,%p) -->", head->data, head->arb);
102204
printList(head->link);
103205
}
104206
printf("\n");
105207
}
106208

107-
struct node *findPtrInClone(int val, struct node *clone){
108-
for(;clone; clone=clone->link){
109-
if(clone->data == val){
110-
return clone;
111-
}
112-
}
113-
return NULL;
114-
}
115-
116-
void assignArb(struct node *head, struct node *clone){
117-
int val, cloneVal;
118-
struct node *clonehead = clone;
119-
for(;head;head=head->link,clone=clone->link){
120-
val = head->arb->data;
121-
struct node *temp = findPtrInClone(val, clonehead);
122-
clone->arb = temp;
209+
struct node *separateClonedList(struct node *head1, struct node *head2){
210+
struct node *temp = head2;
211+
for(;head1 && head2 && head1->link && head2->link;
212+
head1=head1->link, head2=head2->link){
213+
214+
head1->link = head2->link;
215+
head2->link = head2->link->link;
123216
}
217+
head1->link = head2->link = NULL;
218+
return temp;
124219
}
125220

126221
int main(){
127-
128222
struct node *head = (struct node *)malloc(sizeof(struct node));
129223
struct node *t = head;
130224
makeList(t,8,100);
225+
// printList(head);
226+
cloneList(head);
227+
// printList(head);
228+
setRandomNode(head);
131229
printList(head);
132-
struct node *clone = (struct node *)malloc(sizeof(struct node));
133-
cloneList(head, clone);
134-
assignArb(head,clone);
135-
printList(clone);
136-
}
137230

231+
struct node *clone = separateClonedList(head, head->link);
232+
233+
printList(head);
234+
printList(clone);
235+
}

0 commit comments

Comments
 (0)