-
Notifications
You must be signed in to change notification settings - Fork 0
/
skiplists.c
318 lines (258 loc) · 5.81 KB
/
skiplists.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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct node {
int val;
struct node * next;
struct node * nextk1;
struct node * nextk2;
};
typedef struct node * nodeaddress;
int * generateArray(int size);
void quicksortarray(int * array, int start, int stop);
int partition(int * array, int start, int stop);
void swap(int * a, int * b);
int rand_val_of_k(void);
nodeaddress linkedListFromArray(int * a, int n);
nodeaddress create_skip_list(int * arr, int n);
void printskiplist(nodeaddress head);
int main(void)
{
srand(time(0));
int n;
printf("Enter length of skip list: ");
scanf("%d", &n);
int * array = generateArray(n);
quicksortarray(array, 0, n-1);
nodeaddress skiplist = create_skip_list(array, n);
printskiplist(skiplist); // prints address of node, value, address of next, nextk1 and nextk2
}
int rand_val_of_k(void) // generates random value of 1 < k <= 5
{
int result = (rand()%5) + 2;
return result;
}
int * makearray(int size)
{
int * array = malloc( size * sizeof(int) );
if(array)
{
for (int i = 0; i < size; i++)
{
array[0] = 1;
array[1] = 8;
array[2] = 10;
array[3] = 12;
array[4] = 14;
array[5] = 16;
array[6] = 18;
array[7] = 20;
array[8] = 21;
array[9] = 22;
}
}
return array;
}
int * generateArray(int size)
{
int * array = malloc(size * sizeof(int));
for (int i = 0; i < size; i++)
{
array[i] = rand()%1000;
}
return array;
}
void quicksortarray(int * array, int start, int stop)
{
if (start < stop)
{
int partindex = partition(array, start, stop);
quicksortarray(array, start, partindex-1);
quicksortarray(array, partindex+1, stop);
}
}
int partition(int * array, int start, int stop)
{
int pivot = array[start];
int i = start+1;
int j = stop;
while(i <= j)
{
if (array[i] > pivot)
{
swap(&array[i], &array[j]);
j = j - 1;
}
else
{
i = i + 1;
}
}
swap(&array[start], &array[j]);
return j;
}
void swap(int * a, int * b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
nodeaddress linkedListFromArray(int * a, int n)
{
int i;
nodeaddress head = NULL;
nodeaddress temp = NULL;
// special case for head
if(n>0)
{
head = malloc( sizeof(struct node) );
head->val = a[0];
head->next = NULL;
temp = head;
}
for(i=1; i<n; i++)
{
temp->next = malloc( sizeof(struct node) );
temp = temp->next;
temp->val = a[i];
temp->next = NULL;
}
return head;
}
nodeaddress create_skip_list(int * arr, int n)
{
nodeaddress head = linkedListFromArray(arr, n);
nodeaddress temp = head;
nodeaddress temp2nd = temp;
int firstjumpval = rand_val_of_k();
int secondjumpval = 2*firstjumpval;
printf(" k1 : %i\n ", firstjumpval);
printf(" k2 : %i\n ", secondjumpval);
for (int count = 0; count < firstjumpval; count++)
{
if (temp2nd == NULL)
{
break;
}
else
{
temp2nd = temp2nd->next;
}
}
if (temp2nd == NULL)
{
head->nextk1 = NULL;
}
else
{
head->nextk1 = temp2nd;
}
// for assigning the 4 skip part
temp2nd = temp;
for (int count = 0; count < secondjumpval; count++)
{
if (temp2nd == NULL)
{
break;
}
else
{
temp2nd = temp2nd->next;
}
}
if (temp2nd == NULL)
{
head->nextk2 = NULL;
}
else
{
head->nextk2 = temp2nd;
}
// rest of the list
for (int i = 1; i < n; i++)
{
temp = temp->next;
temp2nd = temp;
if ( i % firstjumpval == 0)
{
for (int count = 0; count < firstjumpval; count++)
{
if (temp2nd == NULL)
{
break;
}
else
{
temp2nd = temp2nd->next;
}
}
if (temp2nd == NULL)
{
temp->nextk1 = NULL;
}
else
{
temp->nextk1 = temp2nd;
}
}
temp2nd = temp;
if (i % secondjumpval == 0)
{
for (int count = 0; count < secondjumpval; count++)
{
if (temp2nd == NULL)
{
break;
}
else
{
temp2nd = temp2nd->next;
}
}
if (temp2nd == NULL)
{
temp->nextk2 = NULL;
}
else
{
temp->nextk2 = temp2nd;
}
}
}
return head;
}
void printskiplist(nodeaddress head)
{
nodeaddress temp;
int count = 0;
printf("Skip List = ");
for(temp = head; temp != NULL; temp = temp->next)
{
if (temp == head)
{
printf("\n");
printf(" %i) ", count);
printf("Addr %d ", temp);
printf("Val %d ", temp->val);
printf("Next %d ", temp->next);
printf("nextk1 %d ", temp->nextk1);
printf("nextk2 %d ", temp->nextk2);
printf("\n");
count++;
}
else
{
printf("\n");
printf(" %i) ", count );
printf("Addr %d ", temp);
printf("Val %d ", temp->val);
printf("Next %d ", temp->next);
printf("nextk1 %d ", temp->nextk1);
printf("nextk2 %d ", temp->nextk2);
printf("\n");
count++;
}
}
printf("\n");
}