Answered step by step
Verified Expert Solution
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;
Nodeint int;
int getKey const return key;
~Node
std::cout "key key destroyed.
;
;
Node::Nodeint key, int level
thiskey key;
Allocate memory to forward
forward new Node level ;
Fill forward array with NULL
memsetforward sizeofNode level ;
;
class SkipList
Maximum level for this skip list
int MAXLVL;
float P ;
current level of skip list
int level;
pointer to header node
Node header;
public:
SkipListint float;
bool searchElementint;
void insertElementint int;
void deleteElementint;
void displayList;
int randomLevel;
~SkipList
std::cout "skip list destroyed.
;
;
SkipList::SkipListint MAXLVL, float P
thisMAXLVL MAXLVL;
thisP P;
level ;
create header node and initialize key to
header new NodeINTMIN, MAXLVL;
;
create random level for node
int SkipList::randomLevel
float r floatrand RANDMAX;
int lvl ;
while r P && lvl MAXLVL
lvl;
r floatrand RANDMAX;
return lvl;
;
bool SkipList::searchElementint key
Node current header;
std::cout"Search Path:"; i
while currentforwardi && currentforwardigetKey key
current currentforwardi;
current currentforward;
if current && currentgetKey key
return true;
else
return false;
;
void SkipList::insertElementint key
Node current header;
create update array and initialize it
Node updateMAXLVL ;
memsetupdate sizeofNode MAXLVL ;
for int i level; i ; i
while currentforwardi NULL && currentforwardigetKey key
current currentforwardi;
updatei current;
current currentforward;
if current NULL currentgetKey key
int rlevel randomLevel;
if rlevel level
for int i level ; i rlevel ; i
updatei header;
level rlevel;
Node n new Nodekey rlevel;
for int i ; i rlevel; i
nforwardi updateiforwardi;
updateiforwardi n;
cout "Successfully Inserted key key
;
;
void SkipList::deleteElementint key
Node current header;
create update array and initialize it
Node updateMAXLVL ;
memsetupdate sizeofNode MAXLVL ;
for int i level; i ; i
while currentforwardi NULL &&
currentforwardigetKey key
current currentforwardi;
updatei current;
current currentforward;
if current NULL and currentgetKey key
for int i ; i level; i
if updateiforwardi current
break;
updateiforwardi currentforwardi;
Remove levels having no elements
while level && headerforwardlevel
level;
cout "Successfully deleted key key
;
;
Display skip list level wise
void SkipList::displayList
cout
Skip List
;
for int i ; i level; i
Node node headerforwardi;
cout "Level i : ;
while node NULL
cout nodegetKey;
node nodeforwardi;
cout
;
;
MUST NOT CHANGE THE MAIN FUNCTION
int main
srandstaticcasttimenullptr; Seed random number generator
SkipList lst;
std::cout Insertion
;
lstinsertElement;
lstinsertElement;
lstinsertElement;
lstinsertElement;
lstinsertElement;
lstinsertElement;
lstinsertElement;
lstinsertElement;
lstdisplayList;
std::cout
Search
;int key ;
if lstsearchElementkey
std::cout "Found key std::endl;
else
std::cout "Key key not found.
;
std::cout
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started