This document contains C++ code for implementing a priority queue using a doubly linked skip list. It includes class definitions for a Node and SkipList that make up the skip list, and a PriorityQueue class that uses the skip list to store and retrieve elements based on priority. The main() function creates a PriorityQueue and SkipList, adds some sample elements, and calls the Process() method to iteratively remove and output elements by highest priority. However, the code is not fully implementing the Process() method and is not outputting the processed data as intended.
Interactive Powerpoint_How to Master effective communication
double linked list header file code#include iostream#include.pdf
1. double linked list header file code
#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
#include "doublySkiplist.h"
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);
7. 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 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
vector Process(SkipList& skipList) {
vector processedData;
// Process items from highest priority to lowest
for (int priority = MAXIMUM_ALLOWED_LEVELS; priority >= 0; priority--) {
Node* current = head->next[priority];
while (current->data != INT_MAX) {
int data = current->data;
// Process the item by removing it from the SkipList
skipList.Delete(data);
9. // Remove the item from the PriorityQueue
Node* temp = current;
current = current->next[priority];
for (int i = temp->priority; i >= 0; i--) {
temp->prev[i]->next[i] = temp->next[i];
temp->next[i]->prev[i] = temp->prev[i];
}
delete temp;
// Add the processed data to the result
processedData.push_back(data);
}
}
// Display the SkipList from highest level to lowest level
skipList.ShowBackwards();
return processedData;
}
};
#endif
main.cpp code file below
#include
#include
#include
#include "PriorityQ.h"
#include "doublyskiplist.h"
int main(){
srand(static_cast(time(nullptr)));
PriorityQueue queue(3);
SkipList q(3);
10. queue.Enqueue(5,0);
queue.Enqueue(2,1);
queue.Enqueue(3,2);
//queue.Dequeue();
//queue.ShowQueue();
queue.Process(q);
return 0;
}
l am trying to complete the below task
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
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
11. 4. Display the skip list from highest level to lowest level to check
your code
Can you tell me why code doesn't output anything