Double linked list header file below for FYI #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;
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) {
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);
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];
return current->value == value; } void Show() { for (int i = 0; i <= level; i++) { Node* node = head->next[i]; cout << "Level " << i << ": "; while (node != tail) { cout << node->value << " -> "; node = node->next[i]; } cout << "Tail" << endl; } } void ShowBackwards() { Node* currTail = tail; for (int i = 0; i <= level; i++) { Node* node = currTail->prev[i]; cout << "Level " << i << ": "; while (node != head) { cout << node->value << " <- "; node = node->prev[i]; } cout << "Head" << endl; } } }; #endif PriorityQueue header file code below
#ifndef PRIORITYQ_H #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); 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) { Node* node = new Node(data, priority); Node* current = head; for (int i = MAXIMUM_ALLOWED_LEVELS; i >= 0; i--) { while (current->next[i]->priority < priority) { current = current->next[i]; } } 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; } } 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->data; for (int i = current->priority; 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 }; #endif So far what l have done is 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) but l am having trouble create a process method that does the following, please asist in c++ 1.When a priority queue is processed, all higher priority items are processed before lower priority items 2 Once an item is processed it is removed from the SkipList 3. The Process method must output the data values in the order they were processed 4. Display the skip list from highest level to lowest level to check your code

Double linked list header file below for FYI#include iostream.pdf

  • 1.
    Double linked listheader file below for FYI #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];
  • 5.
    return current->value ==value; } void Show() { for (int i = 0; i <= level; i++) { Node* node = head->next[i]; cout << "Level " << i << ": "; while (node != tail) { cout << node->value << " -> "; node = node->next[i]; } cout << "Tail" << endl; } } void ShowBackwards() { Node* currTail = tail; for (int i = 0; i <= level; i++) { Node* node = currTail->prev[i]; cout << "Level " << i << ": "; while (node != head) { cout << node->value << " <- "; node = node->prev[i]; } cout << "Head" << endl; } } }; #endif PriorityQueue header file code below
  • 6.
    #ifndef PRIORITYQ_H #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) {
  • 7.
    head = newNode(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) { Node* node = new Node(data, priority); Node* current = head; for (int i = MAXIMUM_ALLOWED_LEVELS; i >= 0; i--) { while (current->next[i]->priority < priority) { current = current->next[i]; } } 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; } } 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->data; for (int i = current->priority; 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 }; #endif So far what l have done is 1. When new item is being enqueued set its data and priority to random integers (priority is a random integer between 0 and
  • 9.
    MAXIMUM_ALLOWED_LEVEL_INDEX which isa property of the SkipList class) but l am having trouble create a process method that does the following, please asist in c++ 1.When a priority queue is processed, all higher priority items are processed before lower priority items 2 Once an item is processed it is removed from the SkipList 3. The Process method must output the data values in the order they were processed 4. Display the skip list from highest level to lowest level to check your code