|
-
November 9th, 2009, 01:13 AM
#1
Hash Tables & Linked Lists
Hi,
I'm a java programmer and I have never used C++ before. But I have to code in C++ for an assignment. After all my research I managed to get this far and I'm totally lost. I'm supposed to implement hash tables and use linked list in the case of collisions. I would appreciate any help.
Thanks a ton.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
typedef string ObjectType;
struct ObjectNode{
ObjectType cars;
string model;
string status;
ObjectNode *next;
};
class LinkedList{
private:
ObjectNode *curNode, *headNode, *possitionNode;
public:
LinkedList(LinkedList& list);
void addRec(ObjectNode *&headNode, ObjectType cars, string status, string model);
bool isEmpty();
void printList();
};
LinkedList::LinkedList(LinkedList& list){
if(list.headNode == NULL)
headNode = NULL;
else{
headNode = new ObjectNode;
headNode->cars = list.headNode->cars;
ObjectNode *newNode = headNode;
for (ObjectNode *head = list.headNode->next; headNode != NULL; headNode = headNode->next)
{
newNode->next = new ObjectNode;
newNode = newNode->next;
newNode->cars = headNode->cars;
}
newNode->next = NULL;
}
}
void LinkedList::addRec(ObjectNode *&headNode, ObjectType cars, string status, string model){
if ((headNode == NULL) || (cars<headNode->cars))
{
ObjectNode *newNode = new ObjectNode;
newNode->cars = cars;
newNode->next = headNode;
cars<newNode->status;
cars<newNode->model;
headNode = newNode;
}
else
{
addRec(headNode->next, cars, status, model);
}
}
void LinkedList: rintList(){
ObjectNode *temp;
temp = new ObjectNode;
cout << temp->status;
cout << temp->model;
}
class HashTable{
public:
ObjectNode *indexNode;
LinkedList Table[26];
int size;
//Functions
HashTable();
void add(string newObject);
};
HashTable::HashTable(){
}
void HashTable::add(string newObject){
int index = getIndex(newObject);
indexNode = &T[index];
//indexNode.addRec(string newObject);
size++;
}
int main(){
return 0;
}
-
November 9th, 2009, 10:07 AM
#2
Re: Hash Tables & Linked Lists
Depending on precisely what the assignment says you need to do, you could use a std::list for the linked-list part.
If it weren't an assignment, I'd say just use a std::tr1::unordered_map, which is a hash table.
If you throw code tags around that, the formatting will be preserved. That makes it much easier to read.
You're doing okay for a start. The biggest problem I see right now is that you need to at the least give the LinkedList class a destructor which cleans up the dynamic memory. You should probably also give it a copy constructor and assignment operator, or else define those two private to make the class uncopyable.
-
November 9th, 2009, 10:30 AM
#3
Re: Hash Tables & Linked Lists
Allways use code tags correctly. You tend to get more help if you do.
In java, you can simply forget an object and java will remove the object itself. In c++ every object you create (using "new") must be deleted explicitly with "delete". Consiquently (as lindly says), you have to also write a destructor method which is responsible for calling delete on everything else that you're object created with "new".
Also,
Code:
indexNode.addRec(string newObject);
// should be
indexNode->addRec(string newObject);
If the left hand side is a pointer use "->", other wise if it is the object itself use "."
If you have any specific questions, they will be easier to answer than just "I'm lost".
Signature
Please use: [ code ][/ code ] tags and reasonably correct tabbing. They really help us read your code
End Signature
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|