Question

need c++ format

need SkipListNode.h & .cpp and main.cpp

**********************************************************

1.3 The Skip Lists Node Class Each node of a skip list must be capable of pointing to multiple sucessors in the skip list structure. This could be accomplished with an array of pointers of fixed size, but this is wasteful in terms of storage since the size would have to be large enough to accommodate the biggest possible skip list. It is more efficient to the skip list itself just prior to the creation of the node. The number of next pointers can be passed to the skip list node constructor along with the value to be stored. Note that the pointer access methods must y allocate an array of pointers for the node based on information which can be determined by specify one of the next pointers; the number of pointers in the nodes array should be stored so that the access methods can verify legitimate requests1.3.1 Displaying the Skip List Node From a purely object-oriented standpoint, the only piece of human-readable information in a skip list node is the data stored there; the next pointers are essentially random numbers which most likely change from execution to execution of the program. From a program development point of view, however, it will be helpful to be able to visually confirm the next pointer contents. To do so, the write method for one node shall parenthetically display the values (and only the values) of successor nodes in addition to its own value. // write nodes key value for // each next pointer if // nexts[i] leads to a node // write (nodes key value) else // write Add the interface and implementation of write to the project and overload the << operator so that it invokes method write. Test it with the following program segment in function main SkipListNode n1 (103, 3); SkipListNode n2 (201, 1); cout << w/o setNext: << n1 << endl << n1.setNext (0, &n2); cout << setNext: << n1 << endl << << n2 << endl; << n2 << endl; The results should appear as follows w/o setNext: 201 ( /) 103 201) (( /) 201 /) setNext:

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER:

Code in C++. Tested on Microsoft Visual Studio.

#include<iostream>
using namespace std;

/*
*class that represent the Skip list node
*where each node contains data of type double
*and variable number of pointers to the same
*class
*/
class SkipListNode{
   double data;
   SkipListNode** next;//pointer of pointers
   int size;//size of array of pointers
public:
   /*
   *constructor that accepts @value as data in the node
   *and @size as number of pointers
   */
   SkipListNode(double value,int size){
       data = value;
       next = new SkipListNode*[size];
       for (int i = 0; i < size; i++){
           next[i] = nullptr;
       }
       this->size = size;
   }
   /*
   *method to set the next pointers at @index with given node
   *@address
   */
   void setNext(int index, SkipListNode*address){
       next[index] = address;
   }
   /*
   *method to display the information in the node
   */
   ostream & write(ostream& out){
       out << data;
       for (int i = 0; i < size; i++){
           if (next[i] != nullptr){
               out << " ( " << next[i]->data << " )";
           }
           else{
               out << " ( / )";
           }
       }
       return out;
   }
   friend ostream & operator << (ostream& out, SkipListNode& node);
};
//<< operator overloaded to print SkipListNode class object
ostream & operator << (ostream& out, SkipListNode& node){
   return node.write(out);
}
int main(){
   SkipListNode n1(103, 3);
   SkipListNode n2(201, 1);
   cout << "w/o setNext :   " << n1 << endl<<"       "<<n2<<endl;
   n1.setNext(0, &n2);
   cout << "w/ setNext :   " << n1 << endl << "       " << n2 << endl;
   getchar();
   return 0;
}

Output:

w/o setNext :   103 ( / ) ( / ) ( / )
                       201 ( / )
w/ setNext :   103 ( 201 ) ( / ) ( / )
                      201 ( / )

clusersluser documents visual studio 20131ProjectslSkipList ,DebuglSkipList.exe 3 o setNext 103 C>//> 201 〈/> w/ setNext : 103 〈201〉 〈/〉 〈/〉

Add a comment
Know the answer?
Add Answer to:
need c++ format need SkipListNode.h & .cpp and main.cpp ********************************************************** 1.3 The Skip List's Node Class...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PLEASE USE C++ Source Code Attached is a linked list with 2 nodes. You can use...

    PLEASE USE C++ Source Code Attached is a linked list with 2 nodes. You can use this or write a similar one. The assignment is to write 2 functions. One function will add another node at the end of the list. The other function will delete a node. Don't forget - No dangling pointers ! Example linked source code attached below #include<iostream> using namespace std; class Node { int data; Node *next; public: void setdata(int d) {data = d;} void...

  • Working in C++, Complete the code. // Lab 9 code /* Insert leading comments with your...

    Working in C++, Complete the code. // Lab 9 code /* Insert leading comments with your name and the date. Describe the purpose of the code. Also, list each pointer and describe how it is used to enable the selection sort for the linked list structure. */ /* Insert code as described by the comments. */ /* Add comments to each line of code that uses a pointer, describing how it is being used. */ #include <iostream> using namespace std;...

  • C++ assignment about doubly linked list

    You are required to write the following functions using this class: class Doubly_linked_list // Use a class Doubly_linked_list to represent an object {     public:     // constructor initialize the nextPtr     Doubly_linked_list()     {         prevPtr = 0; // point to null at the beginning         nextPtr = 0; // point to null at the beginning     }     // get a number     int GetNum()     {         return number;     }     // set a number     void SetNum(int num)     {         number = num;     }     // get the prev pointer     Doubly_linked_list ...

  • ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a...

    ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a STRUCTURE for each node, write FUNCTION which starts at the head and outputs the value for each node until the last node is reached. Note: your function should work with the structure that is in this program. Please do not use the example which is for a class, but this example canbe helkpful.  Also note that the function must work no matter how many nodes...

  • (C++) You are tasked with implementing a recursive function void distanceFrom(int key) on the IntList class...

    (C++) You are tasked with implementing a recursive function void distanceFrom(int key) on the IntList class (provided). The function will first search through the list for the provided key, and then, recursively, change all previous values in the list to instead be their distance from the node containing the key value. Do not update the node containing the key value or any nodes after it. If the key does not exist in the list, each node should contain its distance...

  • This is a c++ class utilizing class templates and linked lists. I need to implement the...

    This is a c++ class utilizing class templates and linked lists. I need to implement the following member function(s) to List.cpp. Node.hpp/cpp should be fine but if you feel like there needs to be a change for compilation or testing, feel free to do so but make sure to comment on why it was done. /** @pre assumes position is valid, if position is > item_count_ it returns an empty List, also assumes that operators <= and >= are defined...

  • Need this in C++ Goals: Your task is to implement a binary search tree of linked...

    Need this in C++ Goals: Your task is to implement a binary search tree of linked lists of movies. Tree nodes will contain a letter of the alphabet and a linked list. The linked list will be an alphabetically sorted list of movies which start with that letter. MovieTree() ➔ Constructor: Initialize any member variables of the class to default ~MovieTree() ➔ Destructor: Free all memory that was allocated void printMovieInventory() ➔ Print every movie in the data structure in...

  • I need this in C++. This is all one question Program 2: Linked List Class For...

    I need this in C++. This is all one question Program 2: Linked List Class For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class. For extra practice with pointers we'll expand its functionality and make it a doubly linked list with the ability to traverse in both directions. Since the list is doubly linked, each node will have the following structure: struct...

  • You are tasked with implementing a recursive function void distanceFrom(int key) on the IntList class (provided)....

    You are tasked with implementing a recursive function void distanceFrom(int key) on the IntList class (provided). The function will first search through the list for the provided key, and then, recursively, change all previous values in the list to instead be their distance from the node containing the key value. Do not update the node containing the key value or any nodes after it. If the key does not exist in the list, each node should contain its distance from...

  • Linkedlist implementation in C++ The below code I have written is almost done, I only need...

    Linkedlist implementation in C++ The below code I have written is almost done, I only need help to write the definition for delete_last() functio​n. ​ Language C++ // LinkedList.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <string> #include <iostream> using namespace std; struct Node { int dataItem;//Our link list stores integers Node *next;//this is a Node pointer that will be areference to next node in the list }; class LinkedList { private: Node *first;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT