double Skiplist code below
#include
#include
#include
#include
#include
#include
#include "Node.h"
#ifndef SKIPLIST_H
#define SKIPLIST_H
using namespace std;
class SkipList{
public:
// Maximum number of levels allowed for this skip list
int MAXIMUM_ALLOWED_LEVELS;
// current maximum level amongst the inserted nodes
int level;
// the head node's next links are connected to the first node at every level
Node *head;
SkipList(int maxLevels){
MAXIMUM_ALLOWED_LEVELS = maxLevels;
// initially we have the bottom-most level only
level = 0;
// create the header node, value is irrelevant (as long as it doesn't match an inserted value -
NO REPEATS), number of next links is important (this node is connected to the first node at
every level)
head = new Node(INT_MIN, MAXIMUM_ALLOWED_LEVELS);
}
int RandomLevel(){
float probablity = (float)rand()/RAND_MAX; // flip a coin
int lvl = 0;
/*Olog(n) */while (probablity < 0.5 && lvl < MAXIMUM_ALLOWED_LEVELS){
lvl++; // landed heads so increase level by 1
probablity = (float)rand()/RAND_MAX; // flip a coin again
}
return lvl;
}
Node* CreateNode(int value, int level){
// create a new node with next links for every level that this node will be part of
// it will use these links to connect to the next node at each level
return new Node(value, level);
}
void InsertElement(int value)/*Olog(n) becuase all the functions in it are Olog(n) */{
/**
* THIS CODE HAS A BUG,
*
* ***********FIND THE BUG,
* *************EXPLAIN THE WHY THE BUG EXIST
* AND
* **********************SUGGEST A FIX *
*
*/
//w
Node *current = head; // start at head node at the toppest level
/*Olog(n)*/vector update(MAXIMUM_ALLOWED_LEVELS+1, nullptr); // this will hold
the nodes that need updating at every level after the insert takes place
/*BIG Olog(n)*/for (int i = level; i >= 0; i--){ //level is the current maximum level
// for level i, if value is to be inserted here then find out where (i.e. after which node)
/*O(1) constant */ while (current->next[i] != nullptr && current->next[i]->value <
value){ //the while loop is saying keep moving right until null or the current.value > value
current = current->next[i];
}
// found the node after which the value is to be placed at level i
update[i] = current;
}
//if
current = current->next[0]; // current moves bcoz we need to check if the value exists
if (current == nullptr || current->value != value){
/*Olog(n) */int ranLevel = RandomLevel(); // choose a random level upto where the new
node will be placed
if (ranLevel > level){
// if random level for current node is higher than the current maximum level for the
existing nodes then set updates for each level over current maximum to head (since there are no
nodes at these levels yet).
/*Olog(n)*/for (int i=level+1;i<=ranLevel;i++){
update[i] = head;
}
// also change the current maximum level for the existing nodes
level = ranLevel;
}
// create new node with next links for every level from ranLevel to zero
/*Olog(n) */Node* n = CreateNode(value, ranLevel);
// placing new node in the correct place at every level
/*Olog(n)*/for (int i=0;i<=ranLevel;i++){
n->next[i] = update[i]->next[i];
update[i]->next[i] = n;
}
}
}
void Delete(int value) {
if (!Search(value)) {
cout << value;
cout<<" does not exist"< update(level, nullptr);
for (int i = level; i >= 0; i--) {
aftercurr = current->next[i];
while (aftercurr != nullptr && aftercurr->value < value) {
current = aftercurr;
aftercurr = current->next[i];
}
update[i] = current;
}
for (int i = level; i >= 0; i--) {
aftercurr = update[i]->next[i];
if (aftercurr != nullptr && aftercurr->value == value) {
update[i]->next[i] = aftercurr->next[i];
delete aftercurr;
}
}
}
bool Search(int value){
// TO BE COMPLETED
// search for value in skip list and return true if it exists; false otherwise
//cout<<"inside"<= 0; i--){ //level is the current maximum level
//cout << i <next[i] != nullptr){ //the while loop is saying keep moving right until null or
the current.value > value
//cout<next[i]->value<next[i]->value == value){
//cout<<"Found it thus returning ";
return true;
}
else if(current->next[i]->value > value || current->next[i]->next[i] == nullptr){
//cout<<"break"<next[i];
//cout<next[i]->value<next[i];
cout << "Level " << i << ": ";
while (node != nullptr && node->next[i] != nullptr){
cout << node->value << " -> ";
node = node->next[i];
}
cout << node->value << " .n";
}
}
};
#endif
Node header file code
#ifndef NODE_H
#define NODE_H
#include
using namespace std;
// the node
class Node{
public:
int value;
// Array of pointers to the next nodes (which are located at various levels)
vector next;
Node(int val, int level){
value = val;
next = vector(level+1, nullptr); // initialize array of pointers to nulls
};
/*int value; // holds data DO NOT ALTER
vector next; // next nodes at various levels DO NOT ALTER
vector prev; // previous nodes at various levels DO NOT ALTER
Node *down; // node at the level below MAY ALTER/REMOVE/ADD MORE LINKS
Olog(n) Node(int val, int level){
value = val;
next = vector(level+1, nullptr);
prev = vector(level+1, nullptr);
down = nullptr;
};*/
};
#endif
Priority Queue code header file
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);
tail->prev.resize(MAXIMUM_ALLOWED_LEVELS + 1, head);
}
~PriorityQueue() {
Node* current = head;
while (current) {
Node* nextNode = current->next[0];
delete current;
current = nextNode;
}
}
void Enqueue(int data, int priority) {
cout << "inside e" << endl;
Node* node = new Node(data, priority);
Node* current = head;
for (int i = MAXIMUM_ALLOWED_LEVELS; i >= 0; i--) {
while (current->next[i]->level < priority) {
current = current->next[i];
}
}
cout << "middle e" << endl;
for (int i = 0; i <= priority; i++) {
node->next[i] = current->next[i];
node->prev[i] = current;
current->next[i]->prev[i] = node;
current->next[i] = node;
}
cout << "done e" << endl;
}
int Dequeue() {
if (IsEmpty()) {
cout << "PriorityQueue is empty." << endl;
return INT_MIN; // or any other sentinel value
}
// Find and remove the highest priority item
Node* current = head->next[MAXIMUM_ALLOWED_LEVELS];
int data = current->value;
for (int i = current->level; i >= 0; i--) {
current->prev[i]->next[i] = current->next[i];
current->next[i]->prev[i] = current->prev[i];
delete current;
}
return data;
}
bool IsEmpty() const {
return head->next[0] == tail;
}
// Add this member function to the PriorityQueue class
void Process(SkipList& skipList) {
cout<<"inside"<= 0; priority--) {
Node* current = head->next[priority];
while (current->value != INT_MAX) {
int data = current->value;
// Process the item by removing it from the SkipList
skipList.Delete(data);
// Remove the item from the PriorityQueue
Node* temp = current;
current = current->next[priority];
for (int i = temp->level; i >= 0; i--) {
temp->prev[i]->next[i] = temp->next[i];
temp->next[i]->prev[i] = temp->prev[i];
}
delete temp;
// You can perform any other actions you need here
// Optionally, print the processed data
cout << "Processed data: " << data << endl;
}
}
// Display the SkipList from highest level to lowest level
for (int level = MAXIMUM_ALLOWED_LEVELS; level >= 0; level--) {
cout << "Level " << level << ": ";
Node* current = head->next[level];
while (current->value != INT_MAX) {
cout << current->value << " ";
current = current->next[level];
}
cout << endl;
}
}
};
#endif
main.cpp file code below
#include
#include
#include
#include "PriorityQ.h"
#include "doublyskiplist.h"
int main(){
srand(static_cast(time(nullptr)));
PriorityQueue queue(3); // Create a priority queue with 3 maximum levels
SkipList skipList(3); // Create a skip list with 3 maximum levels
queue.Enqueue(5,0);
queue.Enqueue(2,1);
queue.Enqueue(10,2);
queue.Process(skipList);
return 0;
}
l am trying to complete the task below and l believe l did but NOTHING outputs
The priority queue holds data in a skip list where each node is doubly
linked at every level
1. Each node in the skip list contains an integer data, a non-
negative integer priority (this will also be the level index of the
node in the skiplist once inserted, default 0 because every
node must be part of level 0), a vector of next links and a
vector of previous links where the number of links in each
vector determines how many levels the node is part of.
1. Maximum value for priority is the maximum allowed
level for the skip list, minimum value is 0
Add methods Enqueue, Dequeue, Process to the Priority Queue class
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)
Priority of a node determines its level in the skiplist (no coin tosses)
When a priority queue is processed, all higher priority items are
processed before lower priority items
1. Once an item is processed it is removed from the SkipList
2. The Process method must output the data values in the order
they were processed
3. Display the skip list from highest level to lowest level to check
your code
Can you please fix my code to output becuase even my test outputs don't do anything

double Skiplist code below#include iostream#include vector.pdf

  • 1.
    double Skiplist codebelow #include #include #include #include #include #include #include "Node.h" #ifndef SKIPLIST_H #define SKIPLIST_H using namespace std; class SkipList{ public: // Maximum number of levels allowed for this skip list int MAXIMUM_ALLOWED_LEVELS; // current maximum level amongst the inserted nodes int level; // the head node's next links are connected to the first node at every level Node *head; SkipList(int maxLevels){ MAXIMUM_ALLOWED_LEVELS = maxLevels;
  • 2.
    // initially wehave the bottom-most level only level = 0; // create the header node, value is irrelevant (as long as it doesn't match an inserted value - NO REPEATS), number of next links is important (this node is connected to the first node at every level) head = new Node(INT_MIN, MAXIMUM_ALLOWED_LEVELS); } int RandomLevel(){ float probablity = (float)rand()/RAND_MAX; // flip a coin int lvl = 0; /*Olog(n) */while (probablity < 0.5 && lvl < MAXIMUM_ALLOWED_LEVELS){ lvl++; // landed heads so increase level by 1 probablity = (float)rand()/RAND_MAX; // flip a coin again } return lvl; } Node* CreateNode(int value, int level){ // create a new node with next links for every level that this node will be part of // it will use these links to connect to the next node at each level return new Node(value, level); } void InsertElement(int value)/*Olog(n) becuase all the functions in it are Olog(n) */{ /** * THIS CODE HAS A BUG, * * ***********FIND THE BUG, * *************EXPLAIN THE WHY THE BUG EXIST
  • 3.
    * AND * **********************SUGGESTA FIX * * */ //w Node *current = head; // start at head node at the toppest level /*Olog(n)*/vector update(MAXIMUM_ALLOWED_LEVELS+1, nullptr); // this will hold the nodes that need updating at every level after the insert takes place /*BIG Olog(n)*/for (int i = level; i >= 0; i--){ //level is the current maximum level // for level i, if value is to be inserted here then find out where (i.e. after which node) /*O(1) constant */ while (current->next[i] != nullptr && current->next[i]->value < value){ //the while loop is saying keep moving right until null or the current.value > value current = current->next[i]; } // found the node after which the value is to be placed at level i update[i] = current; } //if current = current->next[0]; // current moves bcoz we need to check if the value exists if (current == nullptr || current->value != value){ /*Olog(n) */int ranLevel = RandomLevel(); // choose a random level upto where the new node will be placed if (ranLevel > level){ // if random level for current node is higher than the current maximum level for the existing nodes then set updates for each level over current maximum to head (since there are no nodes at these levels yet). /*Olog(n)*/for (int i=level+1;i<=ranLevel;i++){ update[i] = head; } // also change the current maximum level for the existing nodes level = ranLevel; }
  • 4.
    // create newnode with next links for every level from ranLevel to zero /*Olog(n) */Node* n = CreateNode(value, ranLevel); // placing new node in the correct place at every level /*Olog(n)*/for (int i=0;i<=ranLevel;i++){ n->next[i] = update[i]->next[i]; update[i]->next[i] = n; } } } void Delete(int value) { if (!Search(value)) { cout << value; cout<<" does not exist"< update(level, nullptr); for (int i = level; i >= 0; i--) { aftercurr = current->next[i]; while (aftercurr != nullptr && aftercurr->value < value) { current = aftercurr; aftercurr = current->next[i]; } update[i] = current; } for (int i = level; i >= 0; i--) { aftercurr = update[i]->next[i]; if (aftercurr != nullptr && aftercurr->value == value) { update[i]->next[i] = aftercurr->next[i]; delete aftercurr; } } } bool Search(int value){ // TO BE COMPLETED
  • 5.
    // search forvalue in skip list and return true if it exists; false otherwise //cout<<"inside"<= 0; i--){ //level is the current maximum level //cout << i <next[i] != nullptr){ //the while loop is saying keep moving right until null or the current.value > value //cout<next[i]->value<next[i]->value == value){ //cout<<"Found it thus returning "; return true; } else if(current->next[i]->value > value || current->next[i]->next[i] == nullptr){ //cout<<"break"<next[i]; //cout<next[i]->value<next[i]; cout << "Level " << i << ": "; while (node != nullptr && node->next[i] != nullptr){ cout << node->value << " -> "; node = node->next[i]; } cout << node->value << " .n"; } } }; #endif Node header file code #ifndef NODE_H #define NODE_H #include using namespace std; // the node class Node{ public:
  • 6.
    int value; // Arrayof pointers to the next nodes (which are located at various levels) vector next; Node(int val, int level){ value = val; next = vector(level+1, nullptr); // initialize array of pointers to nulls }; /*int value; // holds data DO NOT ALTER vector next; // next nodes at various levels DO NOT ALTER vector prev; // previous nodes at various levels DO NOT ALTER Node *down; // node at the level below MAY ALTER/REMOVE/ADD MORE LINKS Olog(n) Node(int val, int level){ value = val; next = vector(level+1, nullptr); prev = vector(level+1, nullptr); down = nullptr; };*/ }; #endif Priority Queue code header file 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); tail->prev.resize(MAXIMUM_ALLOWED_LEVELS + 1, head);
  • 7.
    } ~PriorityQueue() { Node* current= head; while (current) { Node* nextNode = current->next[0]; delete current; current = nextNode; } } void Enqueue(int data, int priority) { cout << "inside e" << endl; Node* node = new Node(data, priority); Node* current = head; for (int i = MAXIMUM_ALLOWED_LEVELS; i >= 0; i--) { while (current->next[i]->level < priority) { current = current->next[i]; } } cout << "middle e" << endl; for (int i = 0; i <= priority; i++) { node->next[i] = current->next[i]; node->prev[i] = current; current->next[i]->prev[i] = node; current->next[i] = node; } cout << "done e" << endl; } int Dequeue() { if (IsEmpty()) { cout << "PriorityQueue is empty." << endl; return INT_MIN; // or any other sentinel value }
  • 8.
    // Find andremove the highest priority item Node* current = head->next[MAXIMUM_ALLOWED_LEVELS]; int data = current->value; for (int i = current->level; i >= 0; i--) { current->prev[i]->next[i] = current->next[i]; current->next[i]->prev[i] = current->prev[i]; delete current; } return data; } bool IsEmpty() const { return head->next[0] == tail; } // Add this member function to the PriorityQueue class void Process(SkipList& skipList) { cout<<"inside"<= 0; priority--) { Node* current = head->next[priority]; while (current->value != INT_MAX) { int data = current->value; // Process the item by removing it from the SkipList skipList.Delete(data); // Remove the item from the PriorityQueue Node* temp = current;
  • 9.
    current = current->next[priority]; for(int i = temp->level; i >= 0; i--) { temp->prev[i]->next[i] = temp->next[i]; temp->next[i]->prev[i] = temp->prev[i]; } delete temp; // You can perform any other actions you need here // Optionally, print the processed data cout << "Processed data: " << data << endl; } } // Display the SkipList from highest level to lowest level for (int level = MAXIMUM_ALLOWED_LEVELS; level >= 0; level--) { cout << "Level " << level << ": "; Node* current = head->next[level]; while (current->value != INT_MAX) { cout << current->value << " "; current = current->next[level]; } cout << endl; } } }; #endif main.cpp file code below #include #include #include #include "PriorityQ.h"
  • 10.
    #include "doublyskiplist.h" int main(){ srand(static_cast(time(nullptr))); PriorityQueuequeue(3); // Create a priority queue with 3 maximum levels SkipList skipList(3); // Create a skip list with 3 maximum levels queue.Enqueue(5,0); queue.Enqueue(2,1); queue.Enqueue(10,2); queue.Process(skipList); return 0; } l am trying to complete the task below and l believe l did but NOTHING outputs The priority queue holds data in a skip list where each node is doubly linked at every level 1. Each node in the skip list contains an integer data, a non- negative integer priority (this will also be the level index of the node in the skiplist once inserted, default 0 because every node must be part of level 0), a vector of next links and a vector of previous links where the number of links in each vector determines how many levels the node is part of. 1. Maximum value for priority is the maximum allowed level for the skip list, minimum value is 0 Add methods Enqueue, Dequeue, Process to the Priority Queue class 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)
  • 11.
    Priority of anode determines its level in the skiplist (no coin tosses) When a priority queue is processed, all higher priority items are processed before lower priority items 1. Once an item is processed it is removed from the SkipList 2. The Process method must output the data values in the order they were processed 3. Display the skip list from highest level to lowest level to check your code Can you please fix my code to output becuase even my test outputs don't do anything