my code stock.com

Hash table

Snippet options

Download: Download snippet as hashtable.cpp.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

//
//  HashTable.cpp
//  
//
//  Created by cami on 7/19/18.
//
//
#include <vector>
#include <string>
#include <stdlib.h>
#include <iostream>
#include <climits>
#include "HashTable.hpp"
#include <stack>
using namespace std;

template <int ARRAY_LEN>
HashTable<ARRAY_LEN>::HashTable() {
    tableSize = ARRAY_LEN;
    for(int j = 0; j < tableSize; j++){
        lookupTable[j] = NULL;
    }
}


template <int ARRAY_LEN>
HashTable<ARRAY_LEN>::~HashTable() {
    stack <ZippedBookNode*> wordItemStack;
    for(int i = 0; i<ARRAY_LEN; i++){
        ZippedBookNode* c;
        for(ZippedBookNode*c=lookupTable[i]; c; c=c->next) wordItemStack.push(c);
        while(!wordItemStack.empty()){
            c=wordItemStack.top();
            delete c;
            wordItemStack.pop();
        }
    }
}

template <int ARRAY_LEN>
int HashTable<ARRAY_LEN>::getTableSize() {
    if(ARRAY_LEN <= 0){
        return -1;
    }
    else{
       tableSize = ARRAY_LEN;
     return tableSize;
    }
}
//my hashsum is coded in such way so that it tries to assign different location to value
// using this hashsum I was able to creat a varities of number so it maynot need linked list
//But as we know its not possible , still comparatively I have a less amount of conflicts
// few title have same hashsum and occupies same space
//due to this my print is quit different
template <int ARRAY_LEN>
int HashTable<ARRAY_LEN>::hashSum(string bookTitle){
    int sum = 0;
    for (int i = 0; i < bookTitle.length(); i++){
	if(i%2==0)
	    sum = sum + bookTitle[i];
    }
    sum = sum % ARRAY_LEN ;
    return sum;
}


template <int ARRAY_LEN>
void HashTable<ARRAY_LEN>::insertNode(ZippedBookNode* node) {
 int sum = hashSum(node->title);
if(lookupTable[sum]==NULL){
    lookupTable[sum]=node;
    return;
}
else{    
    ZippedBookNode* tmp=lookupTable[sum];
    while(tmp!=NULL){
        if(tmp->title==node->title) return;
        tmp=tmp->next;
    }
    tmp=lookupTable[sum];
    while(tmp->next!=NULL && node->title > tmp->title){
        tmp=tmp->next;
    }
    node->next=tmp->next;
    tmp->next=node;
}
}

    // Where will you insert the new node? How do you know that?
    //We will insert it in lookupTable node which is inside HashTable class, It is know because lookupTable act as universal node which can be access by other function
    // What cases should you consider?
    //wheather node exist or not, if title are same 
    // What should you do in case of collision?-> just go to next 



template <int ARRAY_LEN>
void HashTable<ARRAY_LEN>::printTitles() {
  ZippedBookNode *current = new ZippedBookNode;
  for (int i = 0; i < ARRAY_LEN; i++)       {
    if (lookupTable[i] != NULL){
      current = lookupTable[i];
      cout <<"#"<<current->title;
      while(current -> next != NULL)  {
	current = current -> next;
	cout<<"->"<<current->title;
                   
      }
       cout<<endl;
          
    }
     
  }
    
}




Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.