-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSkipList.cpp
More file actions
126 lines (104 loc) · 3.73 KB
/
Copy pathSkipList.cpp
File metadata and controls
126 lines (104 loc) · 3.73 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
#include "SkipList.hpp"
#include <iostream>
// Define Movie class with necessary properties for demonstration purposes
// SkipListNode Constructor
SkipListNode::SkipListNode(Movie* movie, int level)
: movie(movie), forward(level + 1, nullptr) {}
// SkipList Constructor
SkipList::SkipList(int maxLevel, float probability)
: maxLevel(maxLevel), count(0), probability(probability), gen(std::random_device{}()), dist(0.0, 1.0) {
header = new SkipListNode(nullptr, maxLevel);
}
// SkipList Destructor
SkipList::~SkipList() {
SkipListNode* current = header;
while (current) {
SkipListNode* next = current->forward[0];
delete current;
current = next;
}
}
// Generates a random level for the new node
int SkipList::randomLevel() {
int level = 0;
while (dist(gen) < probability && level < maxLevel) {
level++;
}
return level;
}
// Inserts a movie into the skip list
void SkipList::insert(Movie* movie) {
std::vector<SkipListNode*> update(maxLevel + 1);
SkipListNode* current = header;
// Traverse the list to find the insertion point
for (int i = maxLevel; i >= 0; i--) {
while (current->forward[i] != nullptr && current->forward[i]->movie->getName() < movie->getName()) {
current = current->forward[i];
}
update[i] = current;
}
// Determine the random level for the new node
int level = randomLevel();
SkipListNode* newNode = new SkipListNode(movie, level);
// Insert the new node into the skip list
for (int i = 0; i <= level; i++) {
newNode->forward[i] = update[i]->forward[i];
update[i]->forward[i] = newNode;
}
count++; // Increase the node count
}
// Searches for a movie in the skip list by name
Movie* SkipList::search(const std::string& movieName) {
SkipListNode* current = header;
// Traverse the list to find the movie
for (int i = maxLevel; i >= 0; i--) {
while (current->forward[i] != nullptr && current->forward[i]->movie->getName() < movieName) {
current = current->forward[i];
}
}
// Move to the next node in the lowest level
current = current->forward[0];
// Check if the node contains the movie
if (current != nullptr && current->movie->getName() == movieName) {
return current->movie;
}
return nullptr; // Movie not found
}
// Removes a movie from the skip list by name
void SkipList::remove(const std::string& movieName) {
std::vector<SkipListNode*> update(maxLevel + 1);
SkipListNode* current = header;
// Traverse the list to find the node to remove
for (int i = maxLevel; i >= 0; i--) {
while (current->forward[i] != nullptr && current->forward[i]->movie->getName() < movieName) {
current = current->forward[i];
}
update[i] = current;
}
current = current->forward[0];
// If the node is found, remove it
if (current != nullptr && current->movie->getName() == movieName) {
for (int i = 0; i <= maxLevel; i++) {
if (update[i]->forward[i] != current) break;
update[i]->forward[i] = current->forward[i];
}
delete current;
count--; // Decrease the node count
}
}
int SkipList::getsize()
{
return count;
}
std::vector<std::string> SkipList::getAllNames() {
std::vector<std::string> names;
SkipListNode* current = header->forward[0]; // Start from the first node at level 0
// Traverse the entire level 0 to collect movie names
while (current != nullptr) {
if (current->movie != nullptr) { // Ensure there is a movie associated with the node
names.push_back(current->movie->getName());
}
current = current->forward[0];
}
return names;
}