Hash Table(CHAINED)

  • Thread starter Thread starter ArthurLIT
  • Start date Start date
A

ArthurLIT

Guest
//Hi all GURU c++.Need help with hash table(chained).I know the hash function should be looking

//like this: int hash = (key % TABLE_SIZE);

//How do i implement it in my code below? This is what i got so far:





#ifndef PERSONDATA_H
#define PERSONDATA_H

#include "stdafx.h"
#include"iostream"

using namespace std;



struct PersonData
{
string name;
string surname;
string idNum;
string GPAnum;


PersonData::PersonData(string newName, string newSurname, string newidNum,string newGPAnum)
{
name = newName; surname = newSurname; idNum = newidNum; newGPAnum = GPAnum;
};
PersonData::PersonData(string newSurname)
{
surname = newSurname; name = ""; idNum = ""; GPAnum = "";
};
PersonData::PersonData()
{
name = ""; surname = ""; idNum = "";
};


bool PersonData::operator<(const PersonData& PersonDataReference)
{
return (this->surname.compare(PersonDataReference.surname) < 0);
}
bool PersonData::operator>(const PersonData& PersonDataReference)
{
return !(*this < PersonDataReference) && !(*this == PersonDataReference);
};
bool PersonData::operator==(const PersonData& PersonDataReference)
{
return (this->surname.compare(PersonDataReference.surname) == 0);
}
PersonData& PersonData::operator=(const PersonData& PersonDataReference)
{
name = PersonDataReference.name;
surname = PersonDataReference.surname;
idNum = PersonDataReference.idNum;
GPAnum = PersonDataReference.GPAnum;

return *this;
}
};

ostream& operator<<(ostream& os, PersonData data)
{
cout << "Name: " << data.name << endl;
cout << "Surname: " << data.surname << endl;
cout << "IDnum: " << data.idNum << endl << endl;
cout << "GPAnum: " << data.GPAnum << endl << endl;

return os;

}

#endif
#ifndef BST_H
#define BST_H

#include "stdafx.h"

using namespace std;

// Class is templated.
template <class ItemType>
struct NodeType;

// Assumption: ItemType is a type for which the
// operators "<" and "==" are defined either as
// an appropriate built-in type or a class that
// overloads these operators.

template <class ItemType>
class UnsortedType
{
public:
UnsortedType(); // Class constructor
~UnsortedType(); // Class destructor
void InsertItem(ItemType item);
void PrintList();
void SetCurrentPos();
void MoveToNextItem();
void RetrieveItem(ItemType& item, bool & found);
void DeleteItem(ItemType item);
NodeType<ItemType>* GetCurrentPos();

private:
NodeType<ItemType>* listData;
int length;
NodeType<ItemType>* currentPos;
};


template<class ItemType>
struct NodeType
{
ItemType info;
NodeType<ItemType>* next;
};

template <class ItemType>
UnsortedType<ItemType>::UnsortedType()
{
length = 0;
listData = NULL;
}

template <class ItemType>
UnsortedType<ItemType>::~UnsortedType()
{
NodeType<ItemType>* tempPtr;
while (listData != NULL)
{
tempPtr = listData;
listData = listData->next;
delete tempPtr;
}
length = 0;
}

template <class ItemType>
void UnsortedType<ItemType>::InsertItem(ItemType item)
{
NodeType<ItemType>* location;
location = new NodeType<ItemType>;
location->info = item;
location->next = listData;
listData = location;
length++;
}


template <class ItemType>
void UnsortedType<ItemType>::PrintList()
{
NodeType<ItemType>* location = currentPos;
while (location != NULL)
{
location = location->next;
cout << "\t" << location->info << " ";

}


}

template <class ItemType>
void UnsortedType<ItemType>::SetCurrentPos()
{
currentPos = listData;

}


template<class ItemType>
void UnsortedType<ItemType>::RetrieveItem(ItemType& item, bool& found)

{
bool moreToSearch;
NodeType<ItemType>* location;
location = listData;
found = false;
moreToSearch = (location != NULL);
while (moreToSearch && !found)
{
if (item == location->info)
{
found = true;
item = location->info;
}
else
{
location = location->next;
moreToSearch = (location != NULL);
}
}



}
template<class ItemType>
void UnsortedType<ItemType>::DeleteItem(ItemType item)
{
NodeType<ItemType>* location = listData;
NodeType<ItemType>* tempLocation;

// Locate node to be deleted.
if (item == listData->info)
{
tempLocation = location;
listData = listData->next;
// Delete first node.
}
else
{
while (!(item == (location->next)->info))
location = location->next;
// Delete node at location->next
tempLocation = location->next;
location->next = (location->next)->next;
}
delete tempLocation;
length--;


}


template<class ItemType>
void UnsortedType<ItemType>::MoveToNextItem()
{
currentPos = currentPos->next;


}
#endif // BST_H
// PhonereferenceToTreeTypeSearch.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "UnsortedType.h"
#include "Record.h"

using namespace std;



template<class T>
void CreateHashTable(TreeType<T>& referenceToTreeType);


template<class T>
void ClearTable(TreeType<T>& referenceToTreeType)

template<class T>
void AddData(TreeType<T>& referenceToTreeType);

template<class T>
void RemoveEntry(TreeType<T>& referenceToTreeType);

template<class T>
void PrintAll(TreeType<T>& referenceToTreeType);

template<class T>
void SearchByKey(TreeType<T>& referenceToTreeType);

template<class T>
void ProcessMenu(TreeType<T>& referenceToTreeType);

int ShowMenu(void);


/////////////////////////////////


int _tmain(int argc, _TCHAR* argv[])
{


//// ?????
return 0;
}


int ShowMenu(void)
{
int option;
cout << "\n\t" << endl;
cout << "\t\t===============================" << endl;
cout << "\n\t" << endl;
cout << "\t\tHash Table" << endl;
cout << "\n\t" << endl;
cout << "\t\t===============================" << endl;
cout << "\n\t" << endl;
cout << "\t\t1. create a hash table." << endl;
cout << "\t\t2. clear a hash table of its contents" << endl;
cout << "\t\t3. add data to the hash table" << endl;
cout << "\t\t4. remove data from the hash table (also print each value as it is removed)" << endl;
cout << "\t\t5. print the contents of the hash table " << endl;
cout << "\t\t6.search the hash table for some target key value" << endl;
cout << "\t\t7. Exit" << endl;
cout << "\n";
cout << "\t\tEnter Option Here: ";

cin >> option;
return option;
}

template<class T>
void ProcessMenu(TreeType<T>& referenceToTreeType)
{
bool quit = false; // quit flag

do{
switch (ShowMenu())
{
case 1:
ClearTable(referenceToTreeType);
break;
case 2:
AddDataToTable(referenceToTreeType);
break;
case 3:
RemoveEntry(referenceToTreeType);
break;
case 4:
SearchByKey(referenceToTreeType);
break;

case 5:
PrintAll(referenceToTreeType);
break;
case 6:
CreateHashTable(referenceToTreeType);
break;
case 6: //??
quit = true;
break;
default : ///what is wrong on this line?
cout << "Invalid Entry, Please Try Again";
}

cout << "\n\n\n";
} while (!quit);
}



template<class T>
void SearchByKey(TreeType<T>& referenceToTreeType)
{


///????



}
template<class T>
void ClearTable(TreeType<T>& referenceToTreeType)
{
referenceToTreeType.MakeEmpty();
cout << "\n\n\........................Empty now,ready for insert.............................\n" << endl;
}

template<class T>
void AddData(TreeType<T>& referenceToTreeType)
{
cout << "\nAdd New Data.\n\n" << endl;
cout << "\nEnter name: ";
string name;
cin >> name;
cout << "\nEnter surname: ";
string surname;
cin >> surname;
cout << "\nEnter 5 number ID: ";
string ID;
cin >> ID;
cout << "\Enter a GPA(1-4)";
string gpa;
cin >> gpa;


Record newData(name, surname, ID,gpa);

referenceToTreeType.InsertItem(newData);
cout << "\n\n\t........................Entry is added ................................\n" << endl;

}



template<class T>
void CreatHashTable(TreeType<T>& referenceToTreeType)
{


///????


}

template<class T>
void RemoveEntry(TreeType<T>& referenceToTreeType)
{
cout << "\nRemove Contact.\n\n" << endl;
cout << "\nEnter key: ";
string key;
cin >> key;

referenceToTreeType.DeleteItem(key);



///How to print each value as its removed?

}







template<class T>
void PrintAll(TreeType<T>& referenceToTreeType)
{
referenceToTreeType.PrintTree();
}

Continue reading...
 
Back
Top