Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

C/C++

Skip Lists in C++


The SkipNode Class

Skip nodes are implemented in SkipNode.h (Listing Three). Each node represents a single element in a skip list, and contains pointers to the node's key, its object, and subsequent skip nodes at or below its height.

Listing Three

#ifndef SKIP_LIST_NODE
#define SKIP_LIST_NODE

struct Product
{
  float cost;
  int   quantity;
  int   location;
};

typedef Product productData;

template <class Key, class Obj>
  class SkipList;

template <class Key, class Obj>
  class SkipNode
  {
  public:
    SkipNode(Key*, Obj*, int);
    SkipNode(int);
    ~SkipNode();

    Key* getKey(void);
    Obj* getObj(void);
    int   getHgt(void);
    SkipNode** fwdNodes;

  private:
    int nodeHeight;
    Key* key;
    Obj* obj;
  };

template <class Key, class Obj>
  SkipNode<Key,Obj>::~SkipNode()
  {
    delete key;
    delete obj;
    delete [] fwdNodes;
  }

template <class Key, class Obj>
  SkipNode<Key,Obj>::SkipNode(Key* k, 
    Obj* o, int h)
  {
    nodeHeight = h;
    key = k;
    obj = o;
    fwdNodes =
  new SkipNode<Key,Obj>* [h+1];
    for ( int x = 1; x <= nodeHeight; x++ )
      fwdNodes[x] =
  (SkipNode<Key,Obj>*) NULL;
  }

template <class Key, class Obj>
  SkipNode<Key,Obj>::SkipNode(int h)
  {
    nodeHeight = h;
    key = (Key*) NULL;
    obj = (Obj*) NULL;
    fwdNodes =
  new SkipNode<Key,Obj>* [h+1];
    for ( int x = 1; x <= nodeHeight; x++ )
      fwdNodes[x] =
  (SkipNode<Key,Obj>*) NULL;
  }


template <class Key, class Obj>
  Key* SkipNode<Key,Obj>::getKey(void)
  {
    return key;
  }

template <class Key, class Obj>
  Obj* SkipNode<Key,Obj>::getObj(void)
  {
    return obj;
  }

template <class Key, class Obj>
  int SkipNode<Key,Obj>::getHgt(void)
  {
    return nodeHeight;
  }

#endif //SKIP_LIST_NODE

/* End of File */

A skip node can be instantiated with one of two constructors. The first takes pointers to a key and an object, and an integer indicating the height of the new node. The node's height is kept in a private attribute called nodeHeight, and is reflected in the number of allocated forward node pointers kept in fwdNodes. To make the code easier to read, I've made the fwdNodes attribute public. This also avoids the overhead of a method call when checking whether any of the node's forward pointers actually point to anything.

The second SkipNode constructor takes an integer identifying the node height. When the node is constructed with only a height, the forward pointers are allocated, and the key and object pointers are set to NULL. This constructor is used only for the list's head node. Both constructors set all allocated forward pointers to NULL. These forward pointers will be used heavily by the SkipList class when inserting, searching for, and deleting SkipNode objects.

SkipNode also contains methods you can call to retrieve a key (getKey), an object (getObj), and the node's height (getHgt). Here's a short example in which I instantiate a product node with a key, object, and height, followed by a query for the key and height:

String* key =
    new String("Bill Whitney");
productData* obj  = new productData;
SkipNode* newNode =
    new SkipNode<Key,Obj>(key, obj,
            rndGen->newLevel());

    String* x = newNode->getKey();
    int hgt = newNode->getHgt();

SkipNode's destructor deletes memory allocated for the key, object, and forward pointers.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.