SlideShare a Scribd company logo
1 of 11
Download to read offline
Node file code below
#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 may be located at various levels)
// next[i] is the next link for level i
// the size of this vector determines the number of levels that the current node is part of
vector next;
vector prev; // previous links for each level the node is part of
Node(int val, int level){
value = val;
next = vector(level+1, nullptr); // initialize array of pointers to nulls
prev = vector(level+1, nullptr); // initialize array of pointers to nulls
};
};
#endif
SKIPLIST.H FILE CODE BELOW
#include
#include
#include
#include
#include
#include
#include "Node.h"
#ifndef SKIPLIST_H
#define SKIPLIST_H
using namespace std;
class SkipList{
public:
// Maximum allowed level index
int MAXIMUM_ALLOWED_LEVEL_INDEX;
// current maximum level amongst the inserted nodes
int currentHighestLevelIndex;
// the head node's next links are connected to the first node at every level
Node *head;
Node* tail; // last node at every level
SkipList(int maxLevels){
MAXIMUM_ALLOWED_LEVEL_INDEX = maxLevels;
// initially we have the bottom-most level only
currentHighestLevelIndex = 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 (initially this node is the first node at every
level)
head = new Node(INT_MIN, MAXIMUM_ALLOWED_LEVEL_INDEX);
tail = new Node(INT_MAX, MAXIMUM_ALLOWED_LEVEL_INDEX); // last nodes at
each level
// connect head to tail at every level
for(int i = 0; i <= MAXIMUM_ALLOWED_LEVEL_INDEX; i++){
head->next[i] = tail; // head's prev is null
tail->prev[i] = head; // tail's next is null
}
}
int RandomLevel(){
float probablity = (float)rand()/RAND_MAX; // flip a coin
int lvl = 0;
while (probablity < 0.5 && lvl < MAXIMUM_ALLOWED_LEVEL_INDEX){
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){
Node *current = head; // start at head node
vector update(MAXIMUM_ALLOWED_LEVEL_INDEX+1, nullptr); // this will hold the
nodes that need updating at every level after the insert takes place
for (int i = currentHighestLevelIndex; i >= 0; i--){ // start at the highest level and move
down so that more nodes may be skipped
// for level i, if value is to be inserted here then find out where (i.e. after which node)
while (current->next[i] != nullptr && current->next[i]->value < value){
current = current->next[i];
}
// found the node after which the value is to be placed at level i
update[i] = current;
// move down a level, if possible
}
// at level 0, where current is pointing to by the end of the preceding loop, move over one
node to the right where the value is to be inserted to see if the value already exists there (NO
REPEATS allowed)
current = current->next[0];
if (current == nullptr || current->value != value){
int ranLevel = RandomLevel(); // choose a random level upto where the new node will be
placed (starting from level 0)
if (ranLevel > currentHighestLevelIndex){
// if random level for current node is higher than the current maximum level for
existing nodes; then set head as the node after which the new value is to be inserted for each
level over current maximum to the level chosen for new value to be inserted (update currently
contains nulls for these level(s)).
for (int i = currentHighestLevelIndex+1; i <= ranLevel; i++){
update[i] = head;
}
// also change the current maximum level for the existing nodes
currentHighestLevelIndex = ranLevel;
}
// create new node with next links for every level from ranLevel to zero
Node* n = CreateNode(value, ranLevel);
// placing new node in the correct place at every level
for (int i = 0; i<=ranLevel; i++){
// n connects as a new node between update[i] and update[i]->next[i]
n->next[i] = update[i]->next[i];
n->prev[i] = update[i];
// update[i] connects to n (update[i] is the node before n)
update[i]->next[i] = n;
// n->next[i] (this was originally update[i]->next[i], the node that should be after n)
connects back to n
n->next[i]->prev[i] = n;
}
}
}
void Delete(int value){
// remove value from skip list (all levels) if it exists (output a message if it does not exist)
Node *current = head; // start at head node
vector update(MAXIMUM_ALLOWED_LEVEL_INDEX+1, nullptr); // this will hold the
nodes that need updating at every level after the delete takes place
for (int i = currentHighestLevelIndex; i >= 0; i--){ // start at the highest level and move
down so that more nodes may be skipped
// for level i, if value is to be deleted from here then find out where from (i.e. after which
node could it be)
while (current->next[i] != nullptr && current->next[i]->value < value){
current = current->next[i]; // moving right on level i
}
if(current->next[i] != nullptr && current->next[i]->value == value){
// found the node after which exists the target node (node that is to be deleted from
level i)
update[i] = current;
}
// move down a level, if possible
}
// at level 0, where current is pointing to by the end of the preceding loop, move over one
node to the right to see value exists there before we try to delete it
current = current->next[0];
if (current != nullptr && current->value == value){
// value exists at level 0, so delete it from all levels where it is located.
for (int i = 0; i <= currentHighestLevelIndex; i++){
if(update[i]){ // first we ensure that value exists at level i (otherwise update[i] will be
null)
// update[i] connects to the node after current (current->next[i])
update[i]->next[i] = current->next[i];
// current->next[i] (the node that should be after update[i]) connects back to
update[i]
current->next[i]->prev[i] = update[i];
// disconnect current from level i - are these necessary?
current->next[i] = nullptr;
current->prev[i] = nullptr;
}
}
// delete current
delete current;
// update currentHighestLevelIndex if necessary
while (currentHighestLevelIndex >= 0 && head->next[currentHighestLevelIndex] ==
tail){
currentHighestLevelIndex--;
}
}
else{
// if update[0] is nullptr then preceding loop was unable to find value at level 0
cout << "Value " << value << " does not exist in the skip list.n";
}
}
bool Search(int value){
// TO BE COMPLETED
// search for value in skip list and return true if it exists; false otherwise
Node *current = head; // start at head node
for (int i = currentHighestLevelIndex; i >= 0; i--){ // start at the highest level and move
down so that more nodes may be skipped
// for level i, keep moving right while values are less than what we are looking for (and if
it is possible to move any further right
while (current->next[i] != nullptr && current->next[i]->value < value){
current = current->next[i]; // moving right
}
if(current->next[i] != nullptr && current->next[i]->value == value){
return true; // found the value at level i
}
// move down a level, if possible
}
return false; // value not found, we are at level 0 and to the farthest right at level 0 (value
always exists at level 0 if in the list)
}
void ShowForward(){
cout << "Showing forward:n";
for (int i = currentHighestLevelIndex; i >= 0;i--){
Node *node = head->next[i];
cout << "Level " << i << ": ";
while (node != nullptr && node->next[i] != tail){
cout << node->value << " -> ";
node = node->next[i];
}
if(node){
cout << node->value << " .n";
}
else{
cout << " .n";
}
}
}
void ShowBackward(){
cout << "Showing backward:n";
for (int i = currentHighestLevelIndex; i >= 0;i--){
Node *node = tail->prev[i];
cout << "Level " << i << ": ";
while (node != nullptr && node->prev[i] != head){
cout << node->value << " -> ";
node = node->prev[i];
}
if(node){
cout << node->value << " .n";
}
else{
cout << " .n";
}
}
}
};
#endif
PRIORITYQ file code below
#ifndef PRIORITYQ_H
#define PRIORITYQ_H
#include
#include
#include
#include
#include "Skiplist.h"
using namespace std;
class PriorityQ{
public:
Node *head;
Node *tail;
int priority;
int maxlevel;
PriorityQ(int Level){
srand(time(0));
//priority = rand() % (Level-1);
maxlevel = Level-1;
head = new Node(INT_MIN, Level-1);
tail = new Node(INT_MAX, Level-1);
}
bool isEmpty() {
return head->next[0] == tail;
}
void enqueue(int data) {
priority = rand() % (maxlevel + 1);
//cout << "priority " << priority << endl;
Node *curr = head;
for (int i = maxlevel; i >= 0; i--) {
curr = head;
while (curr->next[i] != nullptr && curr->next[i]->value < data) {
curr = curr->next[i];
}
if (i <= priority) {
Node *newNode = new Node(data, i);
newNode->next[i] = curr->next[i];
curr->next[i] = newNode;
}
}
}
void dequeue(int value, int level) {
for (int i = level; i >= 0; i--) {
Node *curr = head->next[i];
while (curr->next[i] != nullptr && curr->value < value) {
if(curr->value == value){
Node *temp = curr;
curr->prev[i]->next[i] = curr->next[i];
curr->next[i]->prev[i]=curr->prev[i];
curr = curr->next[i];
delete temp;
}
curr = curr->next[i];
}
}
}
void process() {
for (int i = maxlevel; i >= 0; i--) {
Node *curr = head;
cout << "Process " << i << ": ";
while (curr->next[i] != tail) {
cout << curr->next[i]->value << ", ";
dequeue(curr->next[i]->value, i);
curr = curr->next[i];
}
}
}
void show(){
for (int i = maxlevel; i >= 0;i--){
Node *node = head->next[i];
cout << "Level " << i << ": ";
while (node != nullptr && node->next[i] != tail){
cout << node->value << " -> ";
node = node->next[i];
}
if(node){
cout << node->value << " .n";
}
else{
cout << " .n";
}
}
}
};
#endif
in my priorityq file code my process method is not working correctly, its outputting the below
Level 2: 5 -> 55 -> .
Level 1: 5 -> 10 -> 55 -> 1000 -> .
Level 0: 2 -> 5 -> 10 -> 22 -> 55 -> 700 -> 1000 -> .
Process 2: 5, 55,
WHEN l should be getting
Level 2: 5 -> 55 -> .
Level 1: 5 -> 10 -> 55 -> 1000 -> .
Level 0: 2 -> 5 -> 10 -> 22 -> 55 -> 700 -> 1000 -> .
Process 2: 5, 55,
Process 1: 10,1000
Process 0 2,22,700
please fix my process and dequeue code if you see the problem

More Related Content

Similar to Node file code below#ifndef NODE_H#define NODE_H#include .pdf

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.pdfarjunstores123
 
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.pdffastechsrv
 
#include iostream using namespace std; const int nil = 0; cl.docx
#include iostream using namespace std; const int nil = 0; cl.docx#include iostream using namespace std; const int nil = 0; cl.docx
#include iostream using namespace std; const int nil = 0; cl.docxajoy21
 
#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.docxajoy21
 
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdfTHE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdffathimahardwareelect
 
#includeiostream #includecstdio #includecstdlib using na.pdf
#includeiostream #includecstdio #includecstdlib using na.pdf#includeiostream #includecstdio #includecstdlib using na.pdf
#includeiostream #includecstdio #includecstdlib using na.pdfharihelectronicspune
 
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.pdfanupambedcovers
 
Im creating a skip list program for my Algorithms and Advanced Data.pdf
Im creating a skip list program for my Algorithms and Advanced Data.pdfIm creating a skip list program for my Algorithms and Advanced Data.pdf
Im creating a skip list program for my Algorithms and Advanced Data.pdfarkmuzikllc
 
C++ Program to Implement Singly Linked List #includeiostream.pdf
 C++ Program to Implement Singly Linked List #includeiostream.pdf C++ Program to Implement Singly Linked List #includeiostream.pdf
C++ Program to Implement Singly Linked List #includeiostream.pdfangelsfashion1
 
Write an algorithm that reads a list of integers from the keyboard, .pdf
Write an algorithm that reads a list of integers from the keyboard, .pdfWrite an algorithm that reads a list of integers from the keyboard, .pdf
Write an algorithm that reads a list of integers from the keyboard, .pdfArrowdeepak
 
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfInspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfvishalateen
 
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.pdfsooryasalini
 
Program In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdfProgram In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdfamitbagga0808
 
Please correct my errors upvote Clears our entire .pdf
Please correct my errors upvote      Clears our entire .pdfPlease correct my errors upvote      Clears our entire .pdf
Please correct my errors upvote Clears our entire .pdfkitty811
 
Help I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdfHelp I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdfmail931892
 
#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.pdfangelsfashion1
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfJUSTSTYLISH3B2MOHALI
 

Similar to Node file code below#ifndef NODE_H#define NODE_H#include .pdf (20)

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
 
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
 
#include iostream using namespace std; const int nil = 0; cl.docx
#include iostream using namespace std; const int nil = 0; cl.docx#include iostream using namespace std; const int nil = 0; cl.docx
#include iostream using namespace std; const int nil = 0; cl.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
#include stdafx.h #include iostream using namespace std;vo.docx
 
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdfTHE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
 
Linkedlist
LinkedlistLinkedlist
Linkedlist
 
#includeiostream #includecstdio #includecstdlib using na.pdf
#includeiostream #includecstdio #includecstdlib using na.pdf#includeiostream #includecstdio #includecstdlib using na.pdf
#includeiostream #includecstdio #includecstdlib using na.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
 
Im creating a skip list program for my Algorithms and Advanced Data.pdf
Im creating a skip list program for my Algorithms and Advanced Data.pdfIm creating a skip list program for my Algorithms and Advanced Data.pdf
Im creating a skip list program for my Algorithms and Advanced Data.pdf
 
DSA(1).pptx
DSA(1).pptxDSA(1).pptx
DSA(1).pptx
 
C++ Program to Implement Singly Linked List #includeiostream.pdf
 C++ Program to Implement Singly Linked List #includeiostream.pdf C++ Program to Implement Singly Linked List #includeiostream.pdf
C++ Program to Implement Singly Linked List #includeiostream.pdf
 
Write an algorithm that reads a list of integers from the keyboard, .pdf
Write an algorithm that reads a list of integers from the keyboard, .pdfWrite an algorithm that reads a list of integers from the keyboard, .pdf
Write an algorithm that reads a list of integers from the keyboard, .pdf
 
17 linkedlist (1)
17 linkedlist (1)17 linkedlist (1)
17 linkedlist (1)
 
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfInspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.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
 
Program In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdfProgram In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdf
 
Please correct my errors upvote Clears our entire .pdf
Please correct my errors upvote      Clears our entire .pdfPlease correct my errors upvote      Clears our entire .pdf
Please correct my errors upvote Clears our entire .pdf
 
Help I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdfHelp I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.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
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
 

More from contact41

Need to elaborate on this discussion post Marvin Mower (MM) has t.pdf
Need to elaborate on this discussion post Marvin Mower (MM) has t.pdfNeed to elaborate on this discussion post Marvin Mower (MM) has t.pdf
Need to elaborate on this discussion post Marvin Mower (MM) has t.pdfcontact41
 
Manufacturing and service operations can be divided into different t.pdf
Manufacturing and service operations can be divided into different t.pdfManufacturing and service operations can be divided into different t.pdf
Manufacturing and service operations can be divided into different t.pdfcontact41
 
Make sure its correct for a thumbs up! Thank you! 6. Suppose the qu.pdf
Make sure its correct for a thumbs up! Thank you! 6. Suppose the qu.pdfMake sure its correct for a thumbs up! Thank you! 6. Suppose the qu.pdf
Make sure its correct for a thumbs up! Thank you! 6. Suppose the qu.pdfcontact41
 
John Stewart was a political candidate running for the Garden Hills .pdf
John Stewart was a political candidate running for the Garden Hills .pdfJohn Stewart was a political candidate running for the Garden Hills .pdf
John Stewart was a political candidate running for the Garden Hills .pdfcontact41
 
Joe and Jessie are married and have one dependent child, Lizzie. L.pdf
Joe and Jessie are married and have one dependent child, Lizzie. L.pdfJoe and Jessie are married and have one dependent child, Lizzie. L.pdf
Joe and Jessie are married and have one dependent child, Lizzie. L.pdfcontact41
 
In the current ever-evolving landscape of higher education, the Univer.pdf
In the current ever-evolving landscape of higher education, the Univer.pdfIn the current ever-evolving landscape of higher education, the Univer.pdf
In the current ever-evolving landscape of higher education, the Univer.pdfcontact41
 
In this lab, we will write an application to store a deck of cards i.pdf
In this lab, we will write an application to store a deck of cards i.pdfIn this lab, we will write an application to store a deck of cards i.pdf
In this lab, we will write an application to store a deck of cards i.pdfcontact41
 
Imagine youre part of a security team of a startup that is creating.pdf
Imagine youre part of a security team of a startup that is creating.pdfImagine youre part of a security team of a startup that is creating.pdf
Imagine youre part of a security team of a startup that is creating.pdfcontact41
 
In September 2016, the bodies of two children and their father were .pdf
In September 2016, the bodies of two children and their father were .pdfIn September 2016, the bodies of two children and their father were .pdf
In September 2016, the bodies of two children and their father were .pdfcontact41
 
Im writing an abstract class in java right now for this json file a.pdf
Im writing an abstract class in java right now for this json file a.pdfIm writing an abstract class in java right now for this json file a.pdf
Im writing an abstract class in java right now for this json file a.pdfcontact41
 
Imagine you are a cybersecurity consultant tasked with designing aut.pdf
Imagine you are a cybersecurity consultant tasked with designing aut.pdfImagine you are a cybersecurity consultant tasked with designing aut.pdf
Imagine you are a cybersecurity consultant tasked with designing aut.pdfcontact41
 
Im looking for coding help I dont really need this to be explained.pdf
Im looking for coding help I dont really need this to be explained.pdfIm looking for coding help I dont really need this to be explained.pdf
Im looking for coding help I dont really need this to be explained.pdfcontact41
 
I. Common stocks increase. III. Accounts .pdf
I.   Common stocks increase.                         III.  Accounts .pdfI.   Common stocks increase.                         III.  Accounts .pdf
I. Common stocks increase. III. Accounts .pdfcontact41
 
I. Inventories increase. III. Accounts rece.pdf
I.   Inventories increase.                     III.  Accounts rece.pdfI.   Inventories increase.                     III.  Accounts rece.pdf
I. Inventories increase. III. Accounts rece.pdfcontact41
 

More from contact41 (14)

Need to elaborate on this discussion post Marvin Mower (MM) has t.pdf
Need to elaborate on this discussion post Marvin Mower (MM) has t.pdfNeed to elaborate on this discussion post Marvin Mower (MM) has t.pdf
Need to elaborate on this discussion post Marvin Mower (MM) has t.pdf
 
Manufacturing and service operations can be divided into different t.pdf
Manufacturing and service operations can be divided into different t.pdfManufacturing and service operations can be divided into different t.pdf
Manufacturing and service operations can be divided into different t.pdf
 
Make sure its correct for a thumbs up! Thank you! 6. Suppose the qu.pdf
Make sure its correct for a thumbs up! Thank you! 6. Suppose the qu.pdfMake sure its correct for a thumbs up! Thank you! 6. Suppose the qu.pdf
Make sure its correct for a thumbs up! Thank you! 6. Suppose the qu.pdf
 
John Stewart was a political candidate running for the Garden Hills .pdf
John Stewart was a political candidate running for the Garden Hills .pdfJohn Stewart was a political candidate running for the Garden Hills .pdf
John Stewart was a political candidate running for the Garden Hills .pdf
 
Joe and Jessie are married and have one dependent child, Lizzie. L.pdf
Joe and Jessie are married and have one dependent child, Lizzie. L.pdfJoe and Jessie are married and have one dependent child, Lizzie. L.pdf
Joe and Jessie are married and have one dependent child, Lizzie. L.pdf
 
In the current ever-evolving landscape of higher education, the Univer.pdf
In the current ever-evolving landscape of higher education, the Univer.pdfIn the current ever-evolving landscape of higher education, the Univer.pdf
In the current ever-evolving landscape of higher education, the Univer.pdf
 
In this lab, we will write an application to store a deck of cards i.pdf
In this lab, we will write an application to store a deck of cards i.pdfIn this lab, we will write an application to store a deck of cards i.pdf
In this lab, we will write an application to store a deck of cards i.pdf
 
Imagine youre part of a security team of a startup that is creating.pdf
Imagine youre part of a security team of a startup that is creating.pdfImagine youre part of a security team of a startup that is creating.pdf
Imagine youre part of a security team of a startup that is creating.pdf
 
In September 2016, the bodies of two children and their father were .pdf
In September 2016, the bodies of two children and their father were .pdfIn September 2016, the bodies of two children and their father were .pdf
In September 2016, the bodies of two children and their father were .pdf
 
Im writing an abstract class in java right now for this json file a.pdf
Im writing an abstract class in java right now for this json file a.pdfIm writing an abstract class in java right now for this json file a.pdf
Im writing an abstract class in java right now for this json file a.pdf
 
Imagine you are a cybersecurity consultant tasked with designing aut.pdf
Imagine you are a cybersecurity consultant tasked with designing aut.pdfImagine you are a cybersecurity consultant tasked with designing aut.pdf
Imagine you are a cybersecurity consultant tasked with designing aut.pdf
 
Im looking for coding help I dont really need this to be explained.pdf
Im looking for coding help I dont really need this to be explained.pdfIm looking for coding help I dont really need this to be explained.pdf
Im looking for coding help I dont really need this to be explained.pdf
 
I. Common stocks increase. III. Accounts .pdf
I.   Common stocks increase.                         III.  Accounts .pdfI.   Common stocks increase.                         III.  Accounts .pdf
I. Common stocks increase. III. Accounts .pdf
 
I. Inventories increase. III. Accounts rece.pdf
I.   Inventories increase.                     III.  Accounts rece.pdfI.   Inventories increase.                     III.  Accounts rece.pdf
I. Inventories increase. III. Accounts rece.pdf
 

Recently uploaded

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsNbelano25
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationNeilDeclaro1
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 

Recently uploaded (20)

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 

Node file code below#ifndef NODE_H#define NODE_H#include .pdf

  • 1. Node file code below #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 may be located at various levels) // next[i] is the next link for level i // the size of this vector determines the number of levels that the current node is part of vector next; vector prev; // previous links for each level the node is part of Node(int val, int level){ value = val; next = vector(level+1, nullptr); // initialize array of pointers to nulls prev = vector(level+1, nullptr); // initialize array of pointers to nulls }; }; #endif SKIPLIST.H FILE CODE BELOW #include #include #include #include #include
  • 2. #include #include "Node.h" #ifndef SKIPLIST_H #define SKIPLIST_H using namespace std; class SkipList{ public: // Maximum allowed level index int MAXIMUM_ALLOWED_LEVEL_INDEX; // current maximum level amongst the inserted nodes int currentHighestLevelIndex; // the head node's next links are connected to the first node at every level Node *head; Node* tail; // last node at every level SkipList(int maxLevels){ MAXIMUM_ALLOWED_LEVEL_INDEX = maxLevels; // initially we have the bottom-most level only currentHighestLevelIndex = 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 (initially this node is the first node at every level) head = new Node(INT_MIN, MAXIMUM_ALLOWED_LEVEL_INDEX);
  • 3. tail = new Node(INT_MAX, MAXIMUM_ALLOWED_LEVEL_INDEX); // last nodes at each level // connect head to tail at every level for(int i = 0; i <= MAXIMUM_ALLOWED_LEVEL_INDEX; i++){ head->next[i] = tail; // head's prev is null tail->prev[i] = head; // tail's next is null } } int RandomLevel(){ float probablity = (float)rand()/RAND_MAX; // flip a coin int lvl = 0; while (probablity < 0.5 && lvl < MAXIMUM_ALLOWED_LEVEL_INDEX){ 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){ Node *current = head; // start at head node vector update(MAXIMUM_ALLOWED_LEVEL_INDEX+1, nullptr); // this will hold the nodes that need updating at every level after the insert takes place for (int i = currentHighestLevelIndex; i >= 0; i--){ // start at the highest level and move down so that more nodes may be skipped // for level i, if value is to be inserted here then find out where (i.e. after which node) while (current->next[i] != nullptr && current->next[i]->value < value){
  • 4. current = current->next[i]; } // found the node after which the value is to be placed at level i update[i] = current; // move down a level, if possible } // at level 0, where current is pointing to by the end of the preceding loop, move over one node to the right where the value is to be inserted to see if the value already exists there (NO REPEATS allowed) current = current->next[0]; if (current == nullptr || current->value != value){ int ranLevel = RandomLevel(); // choose a random level upto where the new node will be placed (starting from level 0) if (ranLevel > currentHighestLevelIndex){ // if random level for current node is higher than the current maximum level for existing nodes; then set head as the node after which the new value is to be inserted for each level over current maximum to the level chosen for new value to be inserted (update currently contains nulls for these level(s)). for (int i = currentHighestLevelIndex+1; i <= ranLevel; i++){ update[i] = head; } // also change the current maximum level for the existing nodes currentHighestLevelIndex = ranLevel; } // create new node with next links for every level from ranLevel to zero Node* n = CreateNode(value, ranLevel); // placing new node in the correct place at every level for (int i = 0; i<=ranLevel; i++){ // n connects as a new node between update[i] and update[i]->next[i] n->next[i] = update[i]->next[i];
  • 5. n->prev[i] = update[i]; // update[i] connects to n (update[i] is the node before n) update[i]->next[i] = n; // n->next[i] (this was originally update[i]->next[i], the node that should be after n) connects back to n n->next[i]->prev[i] = n; } } } void Delete(int value){ // remove value from skip list (all levels) if it exists (output a message if it does not exist) Node *current = head; // start at head node vector update(MAXIMUM_ALLOWED_LEVEL_INDEX+1, nullptr); // this will hold the nodes that need updating at every level after the delete takes place for (int i = currentHighestLevelIndex; i >= 0; i--){ // start at the highest level and move down so that more nodes may be skipped // for level i, if value is to be deleted from here then find out where from (i.e. after which node could it be) while (current->next[i] != nullptr && current->next[i]->value < value){ current = current->next[i]; // moving right on level i } if(current->next[i] != nullptr && current->next[i]->value == value){ // found the node after which exists the target node (node that is to be deleted from level i) update[i] = current; } // move down a level, if possible } // at level 0, where current is pointing to by the end of the preceding loop, move over one node to the right to see value exists there before we try to delete it current = current->next[0];
  • 6. if (current != nullptr && current->value == value){ // value exists at level 0, so delete it from all levels where it is located. for (int i = 0; i <= currentHighestLevelIndex; i++){ if(update[i]){ // first we ensure that value exists at level i (otherwise update[i] will be null) // update[i] connects to the node after current (current->next[i]) update[i]->next[i] = current->next[i]; // current->next[i] (the node that should be after update[i]) connects back to update[i] current->next[i]->prev[i] = update[i]; // disconnect current from level i - are these necessary? current->next[i] = nullptr; current->prev[i] = nullptr; } } // delete current delete current; // update currentHighestLevelIndex if necessary while (currentHighestLevelIndex >= 0 && head->next[currentHighestLevelIndex] == tail){ currentHighestLevelIndex--; } } else{ // if update[0] is nullptr then preceding loop was unable to find value at level 0 cout << "Value " << value << " does not exist in the skip list.n"; } } bool Search(int value){ // TO BE COMPLETED // search for value in skip list and return true if it exists; false otherwise
  • 7. Node *current = head; // start at head node for (int i = currentHighestLevelIndex; i >= 0; i--){ // start at the highest level and move down so that more nodes may be skipped // for level i, keep moving right while values are less than what we are looking for (and if it is possible to move any further right while (current->next[i] != nullptr && current->next[i]->value < value){ current = current->next[i]; // moving right } if(current->next[i] != nullptr && current->next[i]->value == value){ return true; // found the value at level i } // move down a level, if possible } return false; // value not found, we are at level 0 and to the farthest right at level 0 (value always exists at level 0 if in the list) } void ShowForward(){ cout << "Showing forward:n"; for (int i = currentHighestLevelIndex; i >= 0;i--){ Node *node = head->next[i]; cout << "Level " << i << ": "; while (node != nullptr && node->next[i] != tail){ cout << node->value << " -> "; node = node->next[i]; } if(node){ cout << node->value << " .n"; } else{ cout << " .n"; } } }
  • 8. void ShowBackward(){ cout << "Showing backward:n"; for (int i = currentHighestLevelIndex; i >= 0;i--){ Node *node = tail->prev[i]; cout << "Level " << i << ": "; while (node != nullptr && node->prev[i] != head){ cout << node->value << " -> "; node = node->prev[i]; } if(node){ cout << node->value << " .n"; } else{ cout << " .n"; } } } }; #endif PRIORITYQ file code below #ifndef PRIORITYQ_H #define PRIORITYQ_H #include #include #include #include #include "Skiplist.h" using namespace std; class PriorityQ{ public: Node *head;
  • 9. Node *tail; int priority; int maxlevel; PriorityQ(int Level){ srand(time(0)); //priority = rand() % (Level-1); maxlevel = Level-1; head = new Node(INT_MIN, Level-1); tail = new Node(INT_MAX, Level-1); } bool isEmpty() { return head->next[0] == tail; } void enqueue(int data) { priority = rand() % (maxlevel + 1); //cout << "priority " << priority << endl; Node *curr = head; for (int i = maxlevel; i >= 0; i--) { curr = head; while (curr->next[i] != nullptr && curr->next[i]->value < data) { curr = curr->next[i]; } if (i <= priority) { Node *newNode = new Node(data, i); newNode->next[i] = curr->next[i]; curr->next[i] = newNode; } } } void dequeue(int value, int level) { for (int i = level; i >= 0; i--) { Node *curr = head->next[i]; while (curr->next[i] != nullptr && curr->value < value) {
  • 10. if(curr->value == value){ Node *temp = curr; curr->prev[i]->next[i] = curr->next[i]; curr->next[i]->prev[i]=curr->prev[i]; curr = curr->next[i]; delete temp; } curr = curr->next[i]; } } } void process() { for (int i = maxlevel; i >= 0; i--) { Node *curr = head; cout << "Process " << i << ": "; while (curr->next[i] != tail) { cout << curr->next[i]->value << ", "; dequeue(curr->next[i]->value, i); curr = curr->next[i]; } } } void show(){ for (int i = maxlevel; i >= 0;i--){ Node *node = head->next[i]; cout << "Level " << i << ": "; while (node != nullptr && node->next[i] != tail){ cout << node->value << " -> "; node = node->next[i]; } if(node){ cout << node->value << " .n"; } else{ cout << " .n";
  • 11. } } } }; #endif in my priorityq file code my process method is not working correctly, its outputting the below Level 2: 5 -> 55 -> . Level 1: 5 -> 10 -> 55 -> 1000 -> . Level 0: 2 -> 5 -> 10 -> 22 -> 55 -> 700 -> 1000 -> . Process 2: 5, 55, WHEN l should be getting Level 2: 5 -> 55 -> . Level 1: 5 -> 10 -> 55 -> 1000 -> . Level 0: 2 -> 5 -> 10 -> 22 -> 55 -> 700 -> 1000 -> . Process 2: 5, 55, Process 1: 10,1000 Process 0 2,22,700 please fix my process and dequeue code if you see the problem