-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpl_question.c
More file actions
264 lines (223 loc) · 5.12 KB
/
Copy pathpl_question.c
File metadata and controls
264 lines (223 loc) · 5.12 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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include<stdio.h>
#include<stdlib.h>
typedef struct ll {
int payload;
struct ll *next;
}sll;
static void push(sll **head_ref,int data) {
// create the new node,
// set its .next to point to the current head,
// and finally change the head to point to the new node.
sll *new_node = (sll *)malloc(sizeof(sll));
new_node->payload = data;
new_node->next = *head_ref;
*head_ref = new_node;
}
static void insert_payload(sll **head,int data) {
if(*head == NULL) {
*head = (sll *)malloc(sizeof(sll));
(*head)->payload = data;
(*head)->next = NULL;
}else {
insert_payload(&((*head)->next),data);
}
}
static void traverse_list(sll *head) {
printf("\n");
while(head) {
printf("%d \t",head->payload);
head = head->next;
}
}
static void just_traverse(sll **head_ref) {
sll *first = *head_ref;
sll *second = (*head_ref)->next;
if(second == NULL) {
return;
}
just_traverse(&(second));
*head_ref = second;
printf("%d \t",second->payload);
}
static void recursive_reverse(sll** head_ref)
{
sll* first;
sll* rest;
/* empty list */
if (*head_ref == NULL)
return;
/* suppose first = {1, 2, 3}, rest = {2, 3} */
first = *head_ref;
rest = first->next;
/* List has only one node */
if (rest == NULL)
return;
/* put the first element on the end of the list */
recursive_reverse(&rest);
first->next->next = first; //first->next->next is last node of reverse-rest list
/* tricky step -- see the diagram */
first->next = NULL;
/* fix the head pointer */
*head_ref = rest;
}
static void rec_reverse_sll (sll **first,sll **second) {
if(*first == NULL || *second == NULL) {
return;
}else {
rec_reverse_sll(&(*second),&((*second)->next));
(*second)->next = *first;
// (*first)->next = ;
}
}
static void reverse_sll (sll **head) {
//method of induction
if(*head == NULL || (*head)->next == NULL) {
return;
}else {
rec_reverse_sll(&(*head),&(*head)->next);
}
}
/**
* Move first element of second list to first element of first list
*/
static void move_node(sll **head_first,sll **head_second) {
// Always visualize the linked list diagram when you copy the *head_second you copy the reference not all elements
// the whole list in temp_node
//so whatever operation you perform on temp_node applicable to all list pointed by head_second
sll *temp_node = *head_second;
*head_second = (*head_second)->next;
temp_node->next = NULL;
temp_node->next = *head_first;
*head_first = temp_node;
}
static void front_back_split_fast(sll *source,sll **front_ref,sll **back_ref){
//instead of traversing the whole list and determine count do simple thing
//use 2 pointer fast=>move twice slow=>move only one position
sll *fast_ptr,*slow_ptr;
if(source == NULL || source->next == NULL) {
*front_ref = source;
*back_ref = NULL;
}else {
fast_ptr = source->next;
slow_ptr = source;
while(fast_ptr) {
fast_ptr = fast_ptr->next;
if(fast_ptr) {
slow_ptr = slow_ptr->next;
fast_ptr = fast_ptr->next;
}
}
*front_ref = source;
*back_ref = slow_ptr->next;
slow_ptr->next = NULL;
}
}
static void front_back_split(sll *source,sll **front_ref,sll **back_ref) {
int count = 0;
int count1,count2;
sll *temp = source;
//traverse list
while(temp){
count++;
temp = temp->next;
}
if(count % 2 == 0) {
count1 = count/2;
}else {
count1 = (count/2) + 1;
}
count2 = count -count1;
sll *temp2;
while(count1) {
count1--;
if(*front_ref == NULL) {
*front_ref = source;
temp2 = *front_ref;
}else {
temp2 = temp2->next;
}
source = source->next;
}
temp2->next = NULL;
while(count2){
count2--;
if(*back_ref == NULL) {
*back_ref = source;
}
}
temp2 = NULL;
}
static void detect_cycle(sll *head) {
int found_cycle = 0;
sll *hare = head; //Initially both run at same speed
sll *tortoise = head;
while(hare) {
hare = hare->next; //hare runs twice as tortoise
if(hare) {
hare = hare->next;
tortoise = tortoise->next;
if(hare == tortoise) {
found_cycle = 1;
break;
}
}
}
if(found_cycle == 1) {
printf("\nfound cycle \n");
}
else {
printf("\nnot found cycle \n");
}
}
static void insert_cycle(sll **head) {
sll *temp_head = *head;
while(temp_head->next) {
temp_head = temp_head->next;
}
temp_head->next = (*head)->next;
}
int main(int argc,char **argv){
sll *head = NULL;
int i = 0;
for(i=1;i<=4;i++) {
insert_payload(&head,i);
}
traverse_list(head);
// reverse_sll(&head);
recursive_reverse(&head);
// printf("\n");
traverse_list(head);
printf("\n");
// just_traverse(&head);
sll *head2 = NULL;
for(i=1;i<=3;i++) {
insert_payload(&head2,i);
}
push(&head2,34);
push(&(head2->next),45);
push(&(head2->next),48);
push(&(head2->next),52);
traverse_list(head2);
sll *front = NULL;
sll *back = NULL;
// front_back_split(head2,&front,&back);
front_back_split_fast(head2,&front,&back);
traverse_list(front);
traverse_list(back);
sll *a=NULL,*b=NULL;
for(i=1;i<=3;i++) {
insert_payload(&a,i);
insert_payload(&b,i);
}
traverse_list(a);
traverse_list(b);
move_node(&a,&b);
traverse_list(a);
traverse_list(b);
sll *cycle = NULL;
for(i=1;i<=6;i++) {
insert_payload(&cycle,i);
}
insert_cycle(&cycle);
detect_cycle(cycle);
}