Skip to content

Instantly share code, notes, and snippets.

@kYroL01
Forked from Karlina-Bytes/HashTable.cpp
Last active August 29, 2015 14:25
Show Gist options
  • Save kYroL01/4f4f7868d63cc3c24078 to your computer and use it in GitHub Desktop.
Save kYroL01/4f4f7868d63cc3c24078 to your computer and use it in GitHub Desktop.
Hash Table Example
//*****************************************************************
// HashTable.cpp
// HashTable
//
// Created by Kar Beringer on June 18, 2014.
//
// This header file contains the Hash Table class definition.
// Hash Table array elements consist of Linked List objects.
//*****************************************************************
#include "HashTable.h"
// Constructs the empty Hash Table object.
// Array length is set to 13 by default.
HashTable::HashTable( int tableLength )
{
if (tableLength <= 0) tableLength = 13;
array = new LinkedList[ tableLength ];
length = tableLength;
}
// Returns an array location for a given item key.
int HashTable::hash( string itemKey )
{
int value = 0;
for ( int i = 0; i < itemKey.length(); i++ )
value += itemKey[i];
return (value * itemKey.length() ) % length;
}
// Adds an item to the Hash Table.
void HashTable::insertItem( Item * newItem )
{
int index = hash( newItem -> key );
array[ index ].insertItem( newItem );
}
// Deletes an Item by key from the Hash Table.
// Returns true if the operation is successful.
bool HashTable::removeItem( string itemKey )
{
int index = hash( itemKey );
return array[ index ].removeItem( itemKey );
}
// Returns an item from the Hash Table by key.
// If the item isn't found, a null pointer is returned.
Item * HashTable::getItemByKey( string itemKey )
{
int index = hash( itemKey );
return array[ index ].getItem( itemKey );
}
// Display the contents of the Hash Table to console window.
void HashTable::printTable()
{
cout << "\n\nHash Table:\n";
for ( int i = 0; i < length; i++ )
{
cout << "Bucket " << i + 1 << ": ";
array[i].printList();
}
}
// Prints a histogram illustrating the Item distribution.
void HashTable::printHistogram()
{
cout << "\n\nHash Table Contains ";
cout << getNumberOfItems() << " Items total\n";
for ( int i = 0; i < length; i++ )
{
cout << i + 1 << ":\t";
for ( int j = 0; j < array[i].getLength(); j++ )
cout << " X";
cout << "\n";
}
}
// Returns the number of locations in the Hash Table.
int HashTable::getLength()
{
return length;
}
// Returns the number of Items in the Hash Table.
int HashTable::getNumberOfItems()
{
int itemCount = 0;
for ( int i = 0; i < length; i++ )
{
itemCount += array[i].getLength();
}
return itemCount;
}
// De-allocates all memory used for the Hash Table.
HashTable::~HashTable()
{
delete [] array;
}
//*****************************************************************
// End of File
//*****************************************************************
//*****************************************************************
// HashTable.h
// HashTable
//
// Created by Karlina Beringer on June 18, 2014.
//
// This header file contains the Hash Table class declaration.
// Hash Table array elements consist of Linked List objects.
//*****************************************************************
#ifndef HashTable_h
#define HashTable_h
#include "LinkedList.h"
//*****************************************************************
// Hash Table objects store a fixed number of Linked Lists.
//*****************************************************************
class HashTable
{
private:
// Array is a reference to an array of Linked Lists.
LinkedList * array;
// Length is the size of the Hash Table array.
int length;
// Returns an array location for a given item key.
int hash( string itemKey );
public:
// Constructs the empty Hash Table object.
// Array length is set to 13 by default.
HashTable( int tableLength = 13 );
// Adds an item to the Hash Table.
void insertItem( Item * newItem );
// Deletes an Item by key from the Hash Table.
// Returns true if the operation is successful.
bool removeItem( string itemKey );
// Returns an item from the Hash Table by key.
// If the item isn't found, a null pointer is returned.
Item * getItemByKey( string itemKey );
// Display the contents of the Hash Table to console window.
void printTable();
// Prints a histogram illustrating the Item distribution.
void printHistogram();
// Returns the number of locations in the Hash Table.
int getLength();
// Returns the number of Items in the Hash Table.
int getNumberOfItems();
// De-allocates all memory used for the Hash Table.
~HashTable();
};
#endif
//*****************************************************************
// End of File
//*****************************************************************
//*****************************************************************
// LinkedList.cpp
// HashTable
//
// Created by Karlina Beringer on June 16, 2014.
//
// This header file contains the Linked List class declaration.
// Hash Table array elements consist of Linked List objects.
//*****************************************************************
#include "LinkedList.h"
// Constructs the empty linked list object.
// Creates the head node and sets length to zero.
LinkedList::LinkedList()
{
head = new Item;
head -> next = NULL;
length = 0;
}
// Inserts an item at the end of the list.
void LinkedList::insertItem( Item * newItem )
{
if (!head -> next)
{
head -> next = newItem;
length++;
return;
}
Item * p = head;
Item * q = head;
while (q)
{
p = q;
q = p -> next;
}
p -> next = newItem;
newItem -> next = NULL;
length++;
}
// Removes an item from the list by item key.
// Returns true if the operation is successful.
bool LinkedList::removeItem( string itemKey )
{
if (!head -> next) return false;
Item * p = head;
Item * q = head;
while (q)
{
if (q -> key == itemKey)
{
p -> next = q -> next;
delete q;
length--;
return true;
}
p = q;
q = p -> next;
}
return false;
}
// Searches for an item by its key.
// Returns a reference to first match.
// Returns a NULL pointer if no match is found.
Item * LinkedList::getItem( string itemKey )
{
Item * p = head;
Item * q = head;
while (q)
{
p = q;
if ((p != head) && (p -> key == itemKey))
return p;
q = p -> next;
}
return NULL;
}
// Displays list contents to the console window.
void LinkedList::printList()
{
if (length == 0)
{
cout << "\n{ }\n";
return;
}
Item * p = head;
Item * q = head;
cout << "\n{ ";
while (q)
{
p = q;
if (p != head)
{
cout << p -> key;
if (p -> next) cout << ", ";
else cout << " ";
}
q = p -> next;
}
cout << "}\n";
}
// Returns the length of the list.
int LinkedList::getLength()
{
return length;
}
// De-allocates list memory when the program terminates.
LinkedList::~LinkedList()
{
Item * p = head;
Item * q = head;
while (q)
{
p = q;
q = p -> next;
if (q) delete p;
}
}
//*****************************************************************
// End of File
//*****************************************************************
//*****************************************************************
// LinkedList.h
// HashTable
//
// Created by Karlina Beringer on June 16, 2014.
//
// This header file contains the Linked List class declaration.
// Hash Table array elements consist of Linked List objects.
//*****************************************************************
#ifndef LinkedList_h
#define LinkedList_h
#include <iostream>
#include <string>
using namespace std;
//*****************************************************************
// List items are keys with pointers to the next item.
//*****************************************************************
struct Item
{
string key;
Item * next;
};
//*****************************************************************
// Linked lists store a variable number of items.
//*****************************************************************
class LinkedList
{
private:
// Head is a reference to a list of data nodes.
Item * head;
// Length is the number of data nodes.
int length;
public:
// Constructs the empty linked list object.
// Creates the head node and sets length to zero.
LinkedList();
// Inserts an item at the end of the list.
void insertItem( Item * newItem );
// Removes an item from the list by item key.
// Returns true if the operation is successful.
bool removeItem( string itemKey );
// Searches for an item by its key.
// Returns a reference to first match.
// Returns a NULL pointer if no match is found.
Item * getItem( string itemKey );
// Displays list contents to the console window.
void printList();
// Returns the length of the list.
int getLength();
// De-allocates list memory when the program terminates.
~LinkedList();
};
#endif
//*****************************************************************
// End of File
//*****************************************************************
//**************************************************************
// main.cpp
// HashTable
//
// Created by Kar Beringer on June 19, 2014.
// Demonstrate a simple Hash Table in C++.
// Implements a Linked List class.
//**************************************************************
#include "HashTable.h"
int main()
{
// Create 26 Items to store in the Hash Table.
Item * A = new Item {"Apple", NULL};
Item * B = new Item {"Banana", NULL};
Item * C = new Item {"Caterpillar", NULL};
Item * D = new Item {"Dog", NULL};
Item * E = new Item {"Elephant", NULL};
Item * F = new Item {"Fedora", NULL};
Item * G = new Item {"Goosebumps", NULL};
Item * H = new Item {"House", NULL};
Item * I = new Item {"Insects", NULL};
Item * J = new Item {"Jam", NULL};
Item * K = new Item {"Kite", NULL};
Item * L = new Item {"Limestone", NULL};
Item * M = new Item {"Mountaineering", NULL};
Item * N = new Item {"Night", NULL};
Item * O = new Item {"Open Sesame", NULL};
Item * P = new Item {"Potatoes", NULL};
Item * Q = new Item {"Quantum Mechanics", NULL};
Item * R = new Item {"Rrrrrrrrrrawr", NULL};
Item * S = new Item {"Snakes", NULL};
Item * T = new Item {"Tizzy Tube", NULL};
Item * U = new Item {"Underworld", NULL};
Item * V = new Item {"Volcanic Ash", NULL};
Item * W = new Item {"Who When What Why", NULL};
Item * X = new Item {"XXX", NULL};
Item * Y = new Item {"Yellow", NULL};
Item * Z = new Item {"Zest of Lemon", NULL};
// Create a Hash Table of 13 Linked List elements.
HashTable table;
// Add 3 Items to Hash Table.
table.insertItem(A);
table.insertItem(B);
table.insertItem(C);
table.printTable();
table.printHistogram();
// Remove one item from Hash Table.
table.removeItem("Apple");
table.printTable();
table.printHistogram();
// Add 23 items to Hash Table.
table.insertItem(D);
table.insertItem(E);
table.insertItem(F);
table.insertItem(G);
table.insertItem(H);
table.insertItem(I);
table.insertItem(J);
table.insertItem(K);
table.insertItem(L);
table.insertItem(M);
table.insertItem(N);
table.insertItem(O);
table.insertItem(P);
table.insertItem(Q);
table.insertItem(R);
table.insertItem(S);
table.insertItem(T);
table.insertItem(U);
table.insertItem(V);
table.insertItem(W);
table.insertItem(X);
table.insertItem(Y);
table.insertItem(Z);
table.printTable();
table.printHistogram();
// Look up an item in the hash table
Item * result = table.getItemByKey("Snakes");
cout << result -> key << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment