3SFE504 - Object Oriented Programming - Linear and Independent Hash Table code

Authors Avatar

CONTENTS

                                                                                                 Page

Introduction        3

Implementation

        Group Work        5

        Student A

                Implementation        10

                Assumption        24

        Student B                

                Implementation        25

Testing

        Student A:

                 Test Plan        44

                ScreenShots        45

        Student B:        

                Test Plan        50

                ScreenShots        51

Self-Evaluation

        Individual Reports        59

                

       

INTRODUCTION

This coursework was done in teams’ of two group members.  

Group V was made of up of:

        N.T. I Kavindya         and         Malshani Nanayakkara

        In this project, we were asked to create a dictionary object, which was capable of storing words and its definitions. Each word could contain more than one definition. The dictionary object we created should contain the facilities of viewing the words in the dictionary, adding new words, searching new words and printing it to a file.

        The words and their definitions would be stored in an external file called ‘defs.dat’. So every time, our application is run, the words and their definitions should be read in. We each created a hash table to accommodate these. Student A’s part, done by Kavindya, reads in the word and stores them using the linear rehashing technique. Student B’s part, done by Malshani, stores the words in the hash table using the independent rehashing technique.

IMPLEMENTATION

GROUP WORK

                                Definition and List Class files

Definition.h

//Definition.h

#ifndef Definition_h

#define Definition_h

#include <string>

#include <cstdlib>

using namespace std;

class Definition

{

        public:

                Definition();                //constructor (initialisation)

                ~Definition();

        

                //setter methods

                void setDefinition(string def);

                void setNext(Definition*);

                

                //getter methods

                string getDefinition();

                Definition* getNext();

                

        private:

                string definition;

                Definition* next;

                

};

#endif

Definition.cc

#include "Definition.h"

#include <string>

#include <cstdlib>

#include <iostream>

using namespace std;

Definition::Definition()

{

        definition = "NULL";

        next = NULL;

}

Definition::~Definition()

{

}

void Definition::setDefinition(string def)

{

        definition=def;

}

void Definition::setNext(Definition* n)

{

        next=n;

}

string Definition::getDefinition()

{

        return definition;

}

Definition* Definition::getNext()

{

        return next;

}

List.h

//List.h

#ifndef List_h

#define List_h

#include <string>

#include "Definition.h"

using namespace std;

class List

{

        public:

                List();                

                ~List();        

                

                //inserts definition at the front of the list

                void insertH(string definition);

                                

                //whether list is empty

                bool isEmpty() const;

                

                //display the list contents

                void printList() const;

                                

                

        private:

                Definition* head;        //points to head of list

};

#endif

List.cc

//List.cc

#include "Definition.h"

#include "List.h"

#include <iostream>

using namespace std;

//constructor - initialises empty list

List::List()

{

        head = NULL;

}

List::~List()

{

        Definition* current;

    Definition* i;

    i = head;

    while (i != NULL)

    {

        current = i;

        i = i->getNext();

        delete current;

    }

}

bool List::isEmpty() const

{

        return head==NULL;

}

void List::insertH(string definition)        //inserts node at head

{

        Definition* n = new Definition;

        

        n->setDefinition(definition);

        n->setNext(head);

        head = n;

}

void List::printList() const

{

        if ( isEmpty() )

        {

                cout << "The List is empty" << endl;

        }

        else

        {

                Definition* n = NULL;

                

                for ( n=head; n!=NULL; n = n->getNext() )

                {

                        cout << "\t" << n->getDefinition() << endl;                 //display data value

                }

                cout << endl;

        }

}

STUDENT A -

LINEAR REHASHING TECHNIQUE

Student A

//Definition .h (header file)

//Definition.h

#include<string>

using namespace std;

class Definition

{

public:

        Definition();

        ~Definition();

        void setDefinition(string); //setter method

        void setNext(Definition*); //setter method

        string getDefinition();        //getter method

        Definition* getNext(); // Definition pointed to getNext method

        

private:

        string definition;

        Definition* next;

};

//Definition.cpp

#include<iostream>

#include "Definition.h"

#include<string>

using namespace std;

Definition::Definition()

{

        definition="";

        next=NULL;

}

Definition ::~Definition()

{

}

void Definition::setDefinition(string d)

{

        definition = d;

}

void Definition::setNext(Definition* n)

{

        next = n;

}

string Definition::getDefinition()

{

        return definition;        

}

Definition* Definition::getNext()

{

        return next;

}

//List.h

#include <string>

#include "Definition.h"

using namespace std;

class List

{

public:

        List();

        ~List();

        void insertAtHead(const string);

        int  getHead() const;

        void printList() const;

        bool isln (const int) const;

        bool isEmpty() const;

private:

        Definition* head;

};

//List.cpp

#include<iostream>

#include "List.h"

#include <string>

using namespace std;

List::List()

{

        head = NULL;

}

List::~List()

{

}

bool List::isEmpty()const

{

        return head == NULL;

}

void List::insertAtHead(const string value)

{

        Definition* p = new Definition;

        p->setDefinition(value);

        p->setNext(head);

        head=p;

}

void List::printList() const

{

        if(isEmpty())

        {

                cout<<"list empty"<<endl;

        }

        else

        {

                Definition* p =NULL;

                for(p=head ; p != NULL ; p=p->getNext())

                {

                        cout<<p->getDefinition()<<endl;

                }

        }

}

//LinHTable.h

#include <string>

#include "List.h"

using namespace std;

const int TABLE_SIZE=29;

class LinHTable

{

public:

        LinHTable();

        ~LinHTable();

        void put(string word, string def);

        List get(string word);

        void traverse();//Traverse until non-empty dic entry

        void search(string); // search t he word

        //void remove();

private:

        struct b

        {

                string word;

                List defList;

        }dictionary[TABLE_SIZE];

};

//LinHTable.cpp

#include<iostream>

#include<string>

#include "LinHTable.h"

using namespace std;

LinHTable::LinHTable()

{

}

LinHTable::~LinHTable()

{

}

// calculating the hash value for the string word

int HashVal(string str)

{

        int n=0;

        char alp[]={'a','b','c','d','e','f','g','h','i','j','k','l','m'

                                ,'n','o','p','q','r','s','t','u','v','w','x','y','z'};

        

        for(int i=0; i<=str.length(); i++)

        {

                for(int ch=0; ch <=25 ; ch++) // calculate string ch between A-Z

                {

                        if(str[i]==alp[ch])

                        {

                                n+=ch+1;

                        }

                }

        }

return (n%TABLE_SIZE); //calculate remain table size

}

//puting the word and definition to the hash table

void LinHTable::put(string word, string def)

{

int dic=HashVal(word);

        if(dictionary[dic].word=="" || dictionary[dic].word==word)

        {

                        dictionary[dic].word=word;

                    dictionary[dic].defList.insertAtHead(def);

        }

        else if(dictionary[dic].word!="")

        {

    int main=dic; //initialise head

        int t=0;

                do

                {

                        dic++;

                        if(dic>=TABLE_SIZE)

                        {

                                dic=dic-TABLE_SIZE;

                        }

                        if(dictionary[dic].word==word)

                        {

                                dictionary[dic].word=word;

                                dictionary[dic].defList.insertAtHead(def);

Join now!

                                t++;

                        }

                }while(dictionary[dic].word!="" && main!=dic);

                        if(t==0)

                        {

                                dictionary[dic].word=word;

                                dictionary[dic].defList.insertAtHead(def);

                        }

        }

}

List LinHTable::get(string w)

{

        int key=HashVal(w);

        while(dictionary[key].word!=w)

        {

                key++;// key  is represents increment to be used .

        }

        return dictionary[key].defList;

}

void LinHTable::traverse()

{

    //displaying word and defini

        for(int h=0 ;h<TABLE_SIZE; h++)

        {

                if(dictionary[h].word!="")

                {

                        cout<<dictionary[h].word<<": "<<endl;

                        get(dictionary[h].word).printList();

                        cout<<endl;

                }

        }

}

// to search , it will display the search word

void LinHTable::search(string str)

{

        int inc=0;

        for(int key=0 ; key<TABLE_SIZE; key++)

        {//hash function to calculate the table index for search key.

                if(dictionary[key].word==str)

                {

                        get(str).printList();

                        inc++; // inc is represents increment to be ...

This is a preview of the whole essay