SlideShare a Scribd company logo
1 of 11
Download to read offline
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;
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
Priority Queue header file code
#ifndef PRIORITYQ_H
#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);
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
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);
// 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);
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
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

More Related Content

Similar to double linked list header file code#include iostream#include.pdf

#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
 #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
angelsfashion1
 
#includeiostream #includecstdio #includecstdlib using na.pdf
#includeiostream #includecstdio #includecstdlib using na.pdf#includeiostream #includecstdio #includecstdlib using na.pdf
#includeiostream #includecstdio #includecstdlib using na.pdf
harihelectronicspune
 
Lecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsLecture 4 data structures and algorithms
Lecture 4 data structures and algorithms
Aakash deep Singhal
 
In the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdfIn the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdf
arjunstores123
 
Please refer this solution. This is working file for IntegersHeade.pdf
Please refer this solution. This is working file for IntegersHeade.pdfPlease refer this solution. This is working file for IntegersHeade.pdf
Please refer this solution. This is working file for IntegersHeade.pdf
sooryasalini
 
Revisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queueRevisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queue
ssuser7319f8
 
Given the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdfGiven the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdf
illyasraja7
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
ritu1806
 
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdfWrite a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
rozakashif85
 
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfHere is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
arrowmobile
 
C++ Program to Implement Singly Linked List #includei.pdf
  C++ Program to Implement Singly Linked List  #includei.pdf  C++ Program to Implement Singly Linked List  #includei.pdf
C++ Program to Implement Singly Linked List #includei.pdf
anupambedcovers
 
take the following code and give details of what each line of code i.pdf
take the following code and give details of what each line of code i.pdftake the following code and give details of what each line of code i.pdf
take the following code and give details of what each line of code i.pdf
fastechsrv
 

Similar to double linked list header file code#include iostream#include.pdf (20)

Linkedlist
LinkedlistLinkedlist
Linkedlist
 
Ds 2 cycle
Ds 2 cycleDs 2 cycle
Ds 2 cycle
 
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
 #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
 
#includeiostream #includecstdio #includecstdlib using na.pdf
#includeiostream #includecstdio #includecstdlib using na.pdf#includeiostream #includecstdio #includecstdlib using na.pdf
#includeiostream #includecstdio #includecstdlib using na.pdf
 
Lecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsLecture 4 data structures and algorithms
Lecture 4 data structures and algorithms
 
In the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdfIn the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdf
 
ISCP internal.pdf
ISCP internal.pdfISCP internal.pdf
ISCP internal.pdf
 
Please refer this solution. This is working file for IntegersHeade.pdf
Please refer this solution. This is working file for IntegersHeade.pdfPlease refer this solution. This is working file for IntegersHeade.pdf
Please refer this solution. This is working file for IntegersHeade.pdf
 
Revisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queueRevisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queue
 
Given the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdfGiven the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdf
 
17 linkedlist (1)
17 linkedlist (1)17 linkedlist (1)
17 linkedlist (1)
 
#include stdafx.h #include iostream using namespace std;vo.docx
#include stdafx.h #include iostream using namespace std;vo.docx#include stdafx.h #include iostream using namespace std;vo.docx
#include stdafx.h #include iostream using namespace std;vo.docx
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
 
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdfWrite a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
 
Implement of c &amp; its coding programming by sarmad baloch
Implement of c &amp; its coding  programming by sarmad balochImplement of c &amp; its coding  programming by sarmad baloch
Implement of c &amp; its coding programming by sarmad baloch
 
3.linked list
3.linked list3.linked list
3.linked list
 
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfHere is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
 
C++ Program to Implement Singly Linked List #includei.pdf
  C++ Program to Implement Singly Linked List  #includei.pdf  C++ Program to Implement Singly Linked List  #includei.pdf
C++ Program to Implement Singly Linked List #includei.pdf
 
take the following code and give details of what each line of code i.pdf
take the following code and give details of what each line of code i.pdftake the following code and give details of what each line of code i.pdf
take the following code and give details of what each line of code i.pdf
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
 

More from facevenky

Flag question Question 9Question 91 ptsMarty is a creative proble.pdf
Flag question Question 9Question 91 ptsMarty is a creative proble.pdfFlag question Question 9Question 91 ptsMarty is a creative proble.pdf
Flag question Question 9Question 91 ptsMarty is a creative proble.pdf
facevenky
 
Exercise 3 You are to code some simple music player application .pdf
Exercise 3  You are to code some simple music player application .pdfExercise 3  You are to code some simple music player application .pdf
Exercise 3 You are to code some simple music player application .pdf
facevenky
 
Exercise2You are to develop a simple queue system for a customer .pdf
Exercise2You are to develop a simple queue system for a customer .pdfExercise2You are to develop a simple queue system for a customer .pdf
Exercise2You are to develop a simple queue system for a customer .pdf
facevenky
 
Electronic Distribution has a defined benefit pension plan. Characte.pdf
Electronic Distribution has a defined benefit pension plan. Characte.pdfElectronic Distribution has a defined benefit pension plan. Characte.pdf
Electronic Distribution has a defined benefit pension plan. Characte.pdf
facevenky
 
Draw a complete ERD that depicts the data model to record the requir.pdf
Draw a complete ERD that depicts the data model to record the requir.pdfDraw a complete ERD that depicts the data model to record the requir.pdf
Draw a complete ERD that depicts the data model to record the requir.pdf
facevenky
 
Do the following well, read all directions, I asked this question be.pdf
Do the following well, read all directions, I asked this question be.pdfDo the following well, read all directions, I asked this question be.pdf
Do the following well, read all directions, I asked this question be.pdf
facevenky
 
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdf
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdfCopy your completed LinkedList class from Lab 3 into the LinkedList..pdf
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdf
facevenky
 

More from facevenky (20)

Flag question Question 9Question 91 ptsMarty is a creative proble.pdf
Flag question Question 9Question 91 ptsMarty is a creative proble.pdfFlag question Question 9Question 91 ptsMarty is a creative proble.pdf
Flag question Question 9Question 91 ptsMarty is a creative proble.pdf
 
Exercise 3 You are to code some simple music player application .pdf
Exercise 3  You are to code some simple music player application .pdfExercise 3  You are to code some simple music player application .pdf
Exercise 3 You are to code some simple music player application .pdf
 
Fiancial Statements. South Sea Baubles has the following (incomplete.pdf
Fiancial Statements. South Sea Baubles has the following (incomplete.pdfFiancial Statements. South Sea Baubles has the following (incomplete.pdf
Fiancial Statements. South Sea Baubles has the following (incomplete.pdf
 
Explore how the shift to digital platforms and online interactions has.pdf
Explore how the shift to digital platforms and online interactions has.pdfExplore how the shift to digital platforms and online interactions has.pdf
Explore how the shift to digital platforms and online interactions has.pdf
 
Exercise2You are to develop a simple queue system for a customer .pdf
Exercise2You are to develop a simple queue system for a customer .pdfExercise2You are to develop a simple queue system for a customer .pdf
Exercise2You are to develop a simple queue system for a customer .pdf
 
Electronic Distribution has a defined benefit pension plan. Characte.pdf
Electronic Distribution has a defined benefit pension plan. Characte.pdfElectronic Distribution has a defined benefit pension plan. Characte.pdf
Electronic Distribution has a defined benefit pension plan. Characte.pdf
 
Execution Steps of Building the Inverted Index is shown as below Exec.pdf
Execution Steps of Building the Inverted Index is shown as below Exec.pdfExecution Steps of Building the Inverted Index is shown as below Exec.pdf
Execution Steps of Building the Inverted Index is shown as below Exec.pdf
 
Elicitation TechniquesSeeking help for Elicitation plan for projec.pdf
Elicitation TechniquesSeeking help for Elicitation plan for projec.pdfElicitation TechniquesSeeking help for Elicitation plan for projec.pdf
Elicitation TechniquesSeeking help for Elicitation plan for projec.pdf
 
Draw a complete ERD that depicts the data model to record the requir.pdf
Draw a complete ERD that depicts the data model to record the requir.pdfDraw a complete ERD that depicts the data model to record the requir.pdf
Draw a complete ERD that depicts the data model to record the requir.pdf
 
Do the following well, read all directions, I asked this question be.pdf
Do the following well, read all directions, I asked this question be.pdfDo the following well, read all directions, I asked this question be.pdf
Do the following well, read all directions, I asked this question be.pdf
 
Digital Communication SystemsObjectives 1 To be able to demons.pdf
Digital Communication SystemsObjectives 1 To be able to demons.pdfDigital Communication SystemsObjectives 1 To be able to demons.pdf
Digital Communication SystemsObjectives 1 To be able to demons.pdf
 
Diagonal communicationa) is the traditional flow of communication .pdf
Diagonal communicationa) is the traditional flow of communication .pdfDiagonal communicationa) is the traditional flow of communication .pdf
Diagonal communicationa) is the traditional flow of communication .pdf
 
Depicted below is footnote #5 for Boeing, Co. for FYE 2021 and 2020,.pdf
Depicted below is footnote #5 for Boeing, Co. for FYE 2021 and 2020,.pdfDepicted below is footnote #5 for Boeing, Co. for FYE 2021 and 2020,.pdf
Depicted below is footnote #5 for Boeing, Co. for FYE 2021 and 2020,.pdf
 
Create a diagram for the following scenario The music streaming serv.pdf
Create a diagram for the following scenario The music streaming serv.pdfCreate a diagram for the following scenario The music streaming serv.pdf
Create a diagram for the following scenario The music streaming serv.pdf
 
Create a function that will read the saved projects from localStorag.pdf
Create a function that will read the saved projects from localStorag.pdfCreate a function that will read the saved projects from localStorag.pdf
Create a function that will read the saved projects from localStorag.pdf
 
Dawn Company general ledger shows the following account balances amo.pdf
Dawn Company general ledger shows the following account balances amo.pdfDawn Company general ledger shows the following account balances amo.pdf
Dawn Company general ledger shows the following account balances amo.pdf
 
Create a class Person with two instance variables of type String cal.pdf
Create a class Person with two instance variables of type String cal.pdfCreate a class Person with two instance variables of type String cal.pdf
Create a class Person with two instance variables of type String cal.pdf
 
Dave and his friend Stewart each owns 50 percent of KBS. During the .pdf
Dave and his friend Stewart each owns 50 percent of KBS. During the .pdfDave and his friend Stewart each owns 50 percent of KBS. During the .pdf
Dave and his friend Stewart each owns 50 percent of KBS. During the .pdf
 
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdf
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdfCopy your completed LinkedList class from Lab 3 into the LinkedList..pdf
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdf
 
Contreras article is about different reforms that have been introdu.pdf
Contreras article is about different reforms that have been introdu.pdfContreras article is about different reforms that have been introdu.pdf
Contreras article is about different reforms that have been introdu.pdf
 

Recently uploaded

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
fonyou31
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 

Recently uploaded (20)

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
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];
  • 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 Priority Queue header file code #ifndef PRIORITYQ_H
  • 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