Here are a few things to check in your enqueue implementation:
- Make sure you are properly initializing the next and prev vectors in the Node constructor based on the priority.
- When finding the insertion point, you need to traverse all levels from MAXIMUM_ALLOWED_LEVELS down to 0, not just level 0.
- When linking in the new node, you need to link it in at all levels from 0 to the randomPriority level inclusive.
- Double check that your next and prev pointers are set correctly when linking the new node in (node->next[i] points to current->next[i], node->prev[i] points to current, etc.)
- Print out the values as
Home assignment II on Spectroscopy 2024 Answers.pdf
the header file code for double link list#include iostream#.pdf
1. the header file code for double link list:
#include
#include
#include
#include
#ifndef DOUBLYSKIPLIST_H
#define DOUBLYSKIPLIST_H
using namespace std;
class Node {
public:
int value;
int level;
vector next;
vector prev;
Node(int val, int lvl) : value(val), level(lvl) {
next.resize(lvl + 1, nullptr);
prev.resize(lvl + 1, nullptr);
}
};
class SkipList {
public:
int level;
Node* head;
Node* tail;
int MAXIMUM_ALLOWED_LEVELS;
2. SkipList(int maxLevels) : MAXIMUM_ALLOWED_LEVELS(maxLevels), level(0) {
head = new Node(INT_MIN, MAXIMUM_ALLOWED_LEVELS);
tail = new Node(INT_MAX, MAXIMUM_ALLOWED_LEVELS);
for (int i = 0; i <= MAXIMUM_ALLOWED_LEVELS; i++) {
head->next[i] = tail;
tail->prev[i] = head;
}
}
int RandomLevel() {
float probability = (float)rand() / RAND_MAX;
int lvl = 0;
while (probability < 0.5 && lvl < MAXIMUM_ALLOWED_LEVELS) {
lvl++;
probability = (float)rand() / RAND_MAX;
}
return lvl;
}
Node* CreateNode(int value, int level) {
return new Node(value, level);
}
void InsertElement(int value) {
Node* current = head;
vector update(MAXIMUM_ALLOWED_LEVELS + 1, nullptr);
for (int i = level; i >= 0; i--) {
while (current->next[i]->value < value) {
3. current = current->next[i];
}
update[i] = current;
}
current = current->next[0];
if (current->value != value) {
int ranLevel = RandomLevel();
if (ranLevel > level) {
for (int i = level + 1; i <= ranLevel; i++) {
update[i] = head;
}
level = ranLevel;
}
Node* n = CreateNode(value, ranLevel);
for (int i = 0; i <= ranLevel; i++) {
n->next[i] = update[i]->next[i];
n->prev[i] = update[i];
update[i]->next[i]->prev[i] = n;
update[i]->next[i] = n;
}
}
}
void Delete(int value) {
if (!Search(value)) {
cout << value << " does not exist" << endl;
return;
}
Node* current = head;
vector update(MAXIMUM_ALLOWED_LEVELS + 1, nullptr);
4. for (int i = level; i >= 0; i--) {
while (current->next[i]->value < value) {
current = current->next[i];
}
update[i] = current;
}
current = current->next[0];
for (int i = 0; i <= level; i++) {
if (update[i]->next[i] == current) {
update[i]->next[i] = current->next[i];
current->next[i]->prev[i] = update[i];
}
}
delete current;
}
bool Search(int value) {
Node* current = head;
for (int i = level; i >= 0; i--) {
while (current->next[i]->value < value) {
current = current->next[i];
}
}
current = current->next[0];
6. #define PRIORITYQ_H
#include
#include
#include // for INT_MAX
#include // for rand()
#include
using namespace std;
class Node {
public:
int data;
int priority; // Priority is also the level in the skip list
std::vector next;
std::vector prev;
Node(int val, int prio) : data(val), priority(prio) {
// Initialize vectors based on priority
next.resize(prio + 1, nullptr);
prev.resize(prio + 1, nullptr);
}
};
class PriorityQueue {
public:
int MAXIMUM_ALLOWED_LEVELS; // Maximum priority/level
Node* head;
Node* tail;
PriorityQueue(int maxLevels) : MAXIMUM_ALLOWED_LEVELS(maxLevels) {
head = new Node(INT_MIN, 0);
tail = new Node(INT_MAX, 0);
head->next.resize(MAXIMUM_ALLOWED_LEVELS + 1, tail);
7. tail->prev.resize(MAXIMUM_ALLOWED_LEVELS + 1, head);
}
void Enqueue(int data, int priority){
int randomPriority = rand() % (MAXIMUM_ALLOWED_LEVELS + 1);
Node* node = new Node(data, randomPriority);
Node* current = head;
for (int i = MAXIMUM_ALLOWED_LEVELS; i >= 0; i--) {
while (current->next[i]->priority < randomPriority) {
current = current->next[i];
}
}
for (int i = 0; i <= randomPriority; i++) {
node->next[i] = current->next[i];
node->prev[i] = current;
current->next[i]->prev[i] = node;
current->next[i] = node;
}
}
l am trying to complete the task below and my enqueue is not working for some reason, please
help in c++
1. When new item is being enqueued set its data and priority to
random integers (priority is a random integer between 0 and
MAXIMUM_ALLOWED_LEVEL_INDEX which is a property of the
SkipList class)