-
Notifications
You must be signed in to change notification settings - Fork 4
/
dsl.c
175 lines (146 loc) · 4.63 KB
/
dsl.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
#include "dsl.h"
#include <stdlib.h>
#include "fatal.h"
/* START: fig12_23.txt */
struct SkipNode
{
ElementType Element;
SkipList Right;
SkipList Down;
};
static Position Bottom = NULL; /* Needs initialization */
static Position Tail = NULL; /* Needs initialization */
/* Initialization procedure */
SkipList
Initialize( void )
{
SkipList L;
if( Bottom == NULL )
{
Bottom = malloc( sizeof( struct SkipNode ) );
if( Bottom == NULL )
FatalError( "Out of space!!!" );
Bottom->Right = Bottom->Down = Bottom;
Tail = malloc( sizeof( struct SkipNode ) );
if( Tail == NULL )
FatalError( "Out of space!!!" );
Tail->Element = Infinity;
Tail->Right = Tail;
}
/* Create the header node */
L = malloc( sizeof( struct SkipNode ) );
if( L == NULL )
FatalError( "Out of space!!!" );
L->Element = Infinity;
L->Right = Tail;
L->Down = Bottom;
return L;
}
/* END */
void
Output( ElementType Element )
{
printf( "%d\n", Element );
}
/* Memory reclamation is left as an exercise */
/* Hint: Delete from top level to bottom level */
SkipList
MakeEmpty( SkipList L )
{
L->Right = Tail;
L->Down = Bottom;
return L;
}
/* START: fig12_24.txt */
/* Return Position of node containing Item, */
/* or Bottom if not found */
Position
Find( ElementType Item, SkipList L )
{
Position Current = L;
Bottom->Element = Item;
while( Item != Current->Element )
if( Item < Current->Element )
Current = Current->Down;
else
Current = Current->Right;
return Current;
}
/* END */
Position
FindMin( SkipList L )
{
Position Current = L;
while( Current->Down != Bottom )
Current = Current->Down;
return Current;
}
Position
FindMax( SkipList L )
{
Position Current = L;
while( Current->Right->Right != Tail ||
Current->Down != Bottom )
{
if( Current->Right->Right != Tail )
Current = Current->Right;
else
Current = Current->Down;
}
return Current;
}
/* START: fig12_25.txt */
SkipList
Insert( ElementType Item, SkipList L )
{
Position Current = L;
Position NewNode;
Bottom->Element = Item;
while( Current != Bottom )
{
while( Item > Current->Element )
Current = Current->Right;
/* If gap size is 3 or at bottom level */
/* and must insert, then promote middle element */
if( Current->Element >
Current->Down->Right->Right->Element )
{
NewNode = malloc( sizeof( struct SkipNode ) );
if( NewNode == NULL )
FatalError( "Out of space!!!" );
NewNode->Right = Current->Right;
NewNode->Down = Current->Down->Right->Right;
Current->Right = NewNode;
NewNode->Element = Current->Element;
Current->Element = Current->Down->Right->Element;
}
else
Current = Current->Down;
}
/* Raise height of DSL if necessary */
if( L->Right != Tail )
{
NewNode = malloc( sizeof( struct SkipNode ) );
if( NewNode == NULL )
FatalError( "Out of space!!!" );
NewNode->Down = L;
NewNode->Right = Tail;
NewNode->Element = Infinity;
L = NewNode;
}
return L;
}
/* END */
SkipList
Remove( ElementType Item, SkipList L )
{
printf( "Remove is unimplemented\n" );
if( Item )
return L;
return L;
}
ElementType
Retrieve( Position P )
{
return P->Element;
}