-
Notifications
You must be signed in to change notification settings - Fork 0
/
skiplist.cpp
150 lines (132 loc) · 3.28 KB
/
skiplist.cpp
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
#include <iostream>
#include <cstdlib>
#include <limits.h>
using namespace std;
struct node{
int val;
struct node** next;
int nps; // number of pointers in next
};
struct node* createNode(int num)
{
struct node* n;
n = malloc(sizeof(struct node));
n->val = num;
n->next = calloc(1, sizeof(struct node*));
n->nps = 1;
return n;
}
int h = 0;
struct node *ninf = createNode(INT_MIN); // - infinity
struct node *pinf = createNode(INT_MAX); // + infinity
void createSkipList()
{
ninf->next[0] = pinf;
}
void insertElement(int num, int lvl, struct node* m)
{
struct node* p = ninf;
if(lvl > 0)
{
double y = ((1.0)*rand())/RAND_MAX;
if(y <= 0.5) return;
}
if(lvl > h)
{
h++;
ninf->next = realloc(ninf->next, ++ninf->nps);
ninf->next[lvl] = pinf;
}
while(p->next[lvl]->val <= num){
p = p->next[lvl];
}
if(!lvl) m = createNode(num);
else
{
m->next = realloc(m->next, ++m->nps);
}
m->next[lvl] = p->next[lvl];
p->next[lvl] = m;
insertElement(num, lvl+1, m);
}
struct node* searchElement(int num)
{
struct node *p = ninf;
int k = p->nps - 1;
while(k >= 0){
while(p->next[k]->val <= num){
p = p->next[k];
}
k--;
}
return p;
}
void deleteElement(int num)
{
struct node* deleteNode = searchElement(num);
if(deleteNode->val != num){
cout << "Number entered doesn't exist in the skip list\n";
return;
}
for(int lvl = 0; lvl <= h; lvl++){
struct node* temp = ninf;
while(temp->next[lvl] != deleteNode && temp->next[lvl] != pinf){
temp = temp->next[lvl];
}
if(temp->next[lvl] == pinf) break;
temp->next[lvl] = deleteNode->next[lvl];
if(temp == ninf && temp->next[lvl] == pinf){
if(lvl > 0){
ninf->next = realloc(ninf->next, lvl-1);
h = lvl-1;
}
else{
ninf->next = realloc(ninf->next, lvl);
h = lvl;
}
}
}
free(deleteNode);
}
void display()
{
cout << "Skip List:\n";
for(int i = 0; i <= h; i++){
struct node* n;
n = ninf->next[i];
cout << "Level " << i << ": ";
while(n->next[i] != NULL){
cout << n->val << " ";
n = n->next[i];
}
cout << endl;
}
}
int main()
{
const initial = 0;
createSkipList();
insertElement(3, initial, NULL);
insertElement(2, initial, NULL);
insertElement(0, initial, NULL);
insertElement(-9, initial, NULL);
insertElement(6, initial, NULL);
insertElement(7, initial, NULL);
insertElement(3, initial, NULL);
insertElement(4, initial, NULL);
insertElement(100, initial, NULL);
insertElement(-59, initial, NULL);
display();
cout << endl;
int x;
cout << "Search for: ";
cin >> x;
if(searchElement(x)->val == x) cout << "Exists in the skip list\n\n";
else cout << "Not in the skip list\n\n";
display();
cout << "\nDelete: ";
cin >> x;
deleteElement(x);
display();
return 0;
}