@@ -48,93 +48,93 @@ Space complexity: O(1)
48
48
*/
49
49
50
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
- // }
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
138
139
139
//================================================================================================
140
140
//METHOD2: hash table to be done later
@@ -222,9 +222,7 @@ int main(){
222
222
struct node * head = (struct node * )malloc (sizeof (struct node ));
223
223
struct node * t = head ;
224
224
makeList (t ,8 ,100 );
225
- // printList(head);
226
225
cloneList (head );
227
- // printList(head);
228
226
setRandomNode (head );
229
227
printList (head );
230
228
0 commit comments