Skip to content

Commit

Permalink
- Added popNode and contains function to LinkedList class
Browse files Browse the repository at this point in the history
- Added HashTable class (string implementation)
  • Loading branch information
seancyw committed Jan 6, 2017
1 parent 4eeae65 commit 33f83e0
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 2 deletions.
1 change: 1 addition & 0 deletions DataStructure/DataStructure.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="HashTable.h" />
<ClInclude Include="LinkedList.h" />
<ClInclude Include="Queue.h" />
<ClInclude Include="Stack.h" />
Expand Down
9 changes: 9 additions & 0 deletions DataStructure/DataStructure.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,19 @@
<ClInclude Include="Queue.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="HashTable.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Queue.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Stack.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
115 changes: 115 additions & 0 deletions DataStructure/HashTable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@

#ifndef HASHTABLE_H
#define HASHTABLE_H

#include <iostream>
#include "LinkedList.h"
#include <string>

//string hash table implementation
namespace hashTableStructure {
class HashTable
{
public:
HashTable(size_t size)
: tableSize(size)
{
hashTable = new linkedListStructure::LinkedList<std::string>[size];
}

~HashTable()
{
if (hashTable != nullptr)
delete[] hashTable;
}

bool insertItem(std::string data)
{
size_t key = hash(data);

if (!hashTable[key].contains(data)) {
hashTable[key].pushFront(data);
return true;
}

return false;
}

bool removeItem(std::string data)
{
size_t key = hash(data);

if (hashTable[key].popNode(data))
return true;
else
return false;
}

size_t getTableSize() const
{
return tableSize;
}

void printTable() const
{
std::cout << "Hash Table List" << std::endl;
for (size_t i = 0; i < tableSize; ++i) {
std::cout << "Array " << i + 1 << std::endl;
hashTable[i].printList();
}
}

void printHistogram() const
{
std::cout << "Hash Table Histogram" << std::endl;
for (size_t i = 0; i < tableSize; ++i) {

std::cout << "Array " << i + 1 << ": ";
for (size_t j = 0; j < hashTable[i].getSize(); ++j) {
std::cout << " X ";
}
std::cout << std::endl;
}
}

private:
linkedListStructure::LinkedList<std::string> *hashTable;
size_t tableSize;

//utility function to get hash key for string type
size_t hash(std::string& data)
{
size_t value = 0;

for (size_t i = 0; i < data.length(); ++i)
value += data[i];

return value % tableSize;
}
};

void testHashTable()
{
HashTable table(10);

table.insertItem("Happy");
table.insertItem("New");
table.insertItem("Year");
table.insertItem("This");
table.insertItem("is");
table.insertItem("a");
table.insertItem("testing");
table.insertItem("function");

table.printTable();
table.printHistogram();

table.removeItem("a");
table.printTable();
table.printHistogram();

}

}

#endif
62 changes: 62 additions & 0 deletions DataStructure/LinkedList.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@ namespace linkedListStructure {
bool popBack(T&);
bool popFront(T&);

//Use for hash table
bool popNode(T&);

bool isEmpty() const;
int getSize() const;

bool contains(const T&);

void printList() const;

private:
Expand Down Expand Up @@ -179,6 +184,63 @@ namespace linkedListStructure {
return size;
}

template<typename T>
bool LinkedList<T>::contains(const T& value)
{
if(isEmpty())
return false;

Node<T>* current = head;

while(current) {

if(current->data == value)
return true;
else
current = current->next;
}

return false;
}

template<typename T>
//Use for hash table
bool LinkedList<T>::popNode(T& value)
{
if (isEmpty() && !contains(value))
return false;

//if the list only contains one node
if (head == tail) {
if (head->data == value) {
head = tail = nullptr;
size = 0;
return true;
}
}

Node<T>* current = head;
Node<T>* tempNode = head;

while(current) {

if(current->data == value) {
tempNode->next = current->next;
delete current;
--size;
return true;
}

//assign current node to tempNode
tempNode = current;

//assign next node to current node
current = current->next;
}

return false;
}

template<typename T>
void LinkedList<T>::printList() const
{
Expand Down
16 changes: 14 additions & 2 deletions DataStructure/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "Stack.h"
#include <string>
#include "Queue.h"
#include "HashTable.h"

using namespace std;

Expand All @@ -12,7 +13,8 @@ enum TEST_CASE {
STACK,
STACK_LIST,
QUEUE,
QUEUE_STACK
QUEUE_STACK,
HASH_TABLE
};

//function prototype
Expand All @@ -21,10 +23,11 @@ void stackTest();
void stackListTest();
void queueTest();
void queueStackTest();
void hashTableTest();

int main()
{
TEST_CASE test = QUEUE_STACK;
TEST_CASE test = HASH_TABLE;

switch (test)
{
Expand All @@ -48,6 +51,10 @@ int main()
queueStackTest();
break;

case HASH_TABLE:
hashTableTest();
break;

default:
break;
}
Expand Down Expand Up @@ -94,4 +101,9 @@ void queueStackTest()
queueOnStackStructure::Queue<int> intQueue;

queueOnStackStructure::testQueue<int>(intQueue, 5, 10, "intQueue");
}

void hashTableTest()
{
hashTableStructure::testHashTable();
}

0 comments on commit 33f83e0

Please sign in to comment.