Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include / / memset #include / / rand #include using namespace std; class Node { private: int key; public: / / Array to hold

#include
#include //memset
#include //rand
#include
using namespace std;
class Node
{
private:
int key;
public:
// Array to hold pointers to node of different level
Node **forward;
Node(int, int);
int getKey() const { return key; }
~Node()
{
std::cout "key " key " destroyed.
";
}
};
Node::Node(int key, int level)
{
this->key = key;
// Allocate memory to forward
forward = new Node *[level +1];
// Fill forward array with 0(NULL)
memset(forward,0, sizeof(Node *)*(level +1));
};
class SkipList
{
// Maximum level for this skip list
int MAXLVL;
float P =0.5;
// current level of skip list
int level;
// pointer to header node
Node *header;
public:
SkipList(int, float);
bool searchElement(int);
void insertElement(int, int=-1);
void deleteElement(int);
void displayList();
int randomLevel();
~SkipList()
{
std::cout "skip list destroyed.
";
}
};
SkipList::SkipList(int MAXLVL, float P)
{
this->MAXLVL = MAXLVL;
this->P = P;
level =0;
// create header node and initialize key to -1
header = new Node(INT_MIN, MAXLVL);
};
// create random level for node
int SkipList::randomLevel()
{
float r =(float)rand()/ RAND_MAX;
int lvl =0;
while (r P && lvl MAXLVL)
{
lvl++;
r =(float)rand()/ RAND_MAX;
}
return lvl;
};
bool SkipList::searchElement(int key)
{
Node *current = header;
std::cout"Search Path:"=0; i--)
{
while (current->forward[i] && current->forward[i]->getKey() key)
{
current = current->forward[i];
}
}
current = current->forward[0];
if (current && current->getKey()== key)
{
return true;
}
else
{
return false;
}
};
void SkipList::insertElement(int key)
{
Node *current = header;
// create update array and initialize it
Node *update[MAXLVL +1];
memset(update,0, sizeof(Node *)*(MAXLVL +1));
for (int i = level; i >=0; i--)
{
while (current->forward[i]!= NULL && current->forward[i]->getKey() key)
{
current = current->forward[i];
}
update[i]= current;
}
current = current->forward[0];
if (current == NULL || current->getKey()!= key)
{
int rlevel = randomLevel();
if (rlevel > level)
{
for (int i = level +1; i rlevel +1; i++)
update[i]= header;
level = rlevel;
}
Node *n = new Node(key, rlevel);
for (int i =0; i = rlevel; i++)
{
n->forward[i]= update[i]->forward[i];
update[i]->forward[i]= n;
}
cout "Successfully Inserted key " key "
";
}
};
void SkipList::deleteElement(int key)
{
Node *current = header;
// create update array and initialize it
Node *update[MAXLVL +1];
memset(update,0, sizeof(Node *)*(MAXLVL +1));
for (int i = level; i >=0; i--)
{
while (current->forward[i]!= NULL &&
current->forward[i]->getKey() key)
current = current->forward[i];
update[i]= current;
}
current = current->forward[0];
if (current != NULL and current->getKey()== key)
{
for (int i =0; i = level; i++)
{
if (update[i]->forward[i]!= current)
{
break;
}
update[i]->forward[i]= current->forward[i];
}
// Remove levels having no elements
while (level >0 && header->forward[level]==0)
{
level--;
}
cout "Successfully deleted key " key "
";
}
};
// Display skip list level wise
void SkipList::displayList()
{
cout "
*****Skip List*****"
"
";
for (int i =0; i = level; i++)
{
Node *node = header->forward[i];
cout "Level " i ": ";
while (node != NULL)
{
cout node->getKey()"";
node = node->forward[i];
}
cout "
";
}
};
// MUST NOT CHANGE THE MAIN FUNCTION
int main()
{
srand(static_cast(time(nullptr))); // Seed random number generator
SkipList lst(3,0.5);
std::cout "======== Insertion ========
";
lst.insertElement(5,1);
lst.insertElement(25,2);
lst.insertElement(30,1);
lst.insertElement(31,3);
lst.insertElement(42,1);
lst.insertElement(58,2);
lst.insertElement(62,1);
lst.insertElement(69,3);
lst.displayList();
std::cout "
======== Search ========
";int key =31;
if (lst.searchElement(key))
{
std::cout "Found " key std::endl;
}
else
{
std::cout "Key " key " not found.
";
}
std::cout "
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Hands-On Database

Authors: Steve Conger

2nd Edition

0133024415, 978-0133024418

More Books

Students also viewed these Databases questions