forked from garyexplains/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoublelinkedlist_with_skip.c
132 lines (113 loc) · 2.52 KB
/
doublelinkedlist_with_skip.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct element {
int value;
struct element *next;
struct element *prev;
struct element *express;
} element;
element *insert_ordered(element **head, element **tail, int v) {
element *h = *head;
if((h == NULL) || (h->value > v)){
// Empty list or need to insert at start
element *ne = malloc(sizeof(element));
ne->value = v;
ne->next = h;
ne->prev = NULL;
ne->express = h;
if(ne->next!=NULL)
ne->next->prev = ne;
if(h == NULL)
*tail=ne;
*head=ne;
return;
}
element *p = *head;
while( (p->next != NULL) && (p->next->value <= v) ) {
p = p->next;
}
// Avoid duplicates, you might want duplicates, so delete
// this if necessary
if(p->value==v) return;
// Now p point to place to insert new element
// Insert
element *ne = malloc(sizeof(element));
ne->value = v;
ne->next = p->next;
p->next = ne;
ne->prev = p;
if(((rand() % 2) == 0) || (ne->next==NULL)){
// Randomly (except when last node) update the skip list
ne->express = p->express;
p->express = ne;
}
if(ne->next==NULL)
*tail = ne;
else
ne->next->prev = ne;
return;
}
void print_list(element *head) {
printf("List: ");
element *p = head;
while(p != NULL) {
printf("%d ", p->value);
p = p->next;
}
printf("\n");
}
void print_rev_list(element *tail) {
printf("Reversed list: ");
element *p = tail;
while(p != NULL) {
printf("%d ", p->value);
p = p->prev;
}
printf("\n");
}
void print_express(element *head) {
printf("Express: ");
element *p = head;
while(p != NULL) {
printf("%d ", p->value);
p = p->express;
}
printf("\n");
}
int main() {
int i;
/* Intializes random number generator */
srand((unsigned) time(NULL));
element *head = NULL;
element *tail = NULL;
insert_ordered(&head, &tail, 7);
print_list(head);
print_rev_list(tail);
insert_ordered(&head, &tail, 10);
print_list(head);
print_rev_list(tail);
insert_ordered(&head, &tail, 20);
print_list(head);
print_rev_list(tail);
insert_ordered(&head, &tail, 15);
print_list(head);
print_rev_list(tail);
insert_ordered(&head, &tail, 4);
print_list(head);
print_rev_list(tail);
insert_ordered(&head, &tail, 30);
print_list(head);
print_rev_list(tail);
insert_ordered(&head, &tail, 29);
print_list(head);
print_rev_list(tail);
insert_ordered(&head, &tail, 5);
print_list(head);
print_rev_list(tail);
for(i=0;i<100;i++)
insert_ordered(&head, &tail, rand() % 0xFF);
print_list(head);
print_rev_list(tail);
print_express(head);
}