I am working on a hash project and am currently having difficulty with an array of linked lists. My linked list can only store 1 item, so I created a Pair class with 2 member variables (string key and string value) and various member functions. My question is, how can I get couples to work with my Linked-list class? My assumption is to do the following: if I wanted to add data to the linked list class, I would create a function with the parameter - void insert (data pair) - will this help me insert 2 elements into my list? Here is my C ++ code, can someone check - read it for me and help me find some errors.
#ifndef List_h
#define List_h
#include "Node.h"
#include "Pair.h"
#include<string>
using namespace std;
class List{
private:
int size;
Node<string>* headPtr;
public:
List();
List(const List& anotherList);
virtual ~List();
void insert(Pair data);
bool remove(Pair data);
bool find(Pair data);
int getSize();
bool isEmpty();
void clear();
};
#include "List.cpp"
#endif
.cpp
void List::insert(Pair data){
Node<string>* newptr = new Node<string> ();
newptr->setItem(data);
newptr->setNext(headPtr);
headPtr = newptr;
size++;
}
void List::clear(){
Node<string>* delPtr = headPtr;
while(delPtr != nullptr){
headPtr = delPtr->getNext();
delPtr->setNext(nullptr);
delete delPtr;
delPtr = headPtr;
}
headPtr = nullptr;
delPtr = nullptr;
size = 0;
}
List::~List() {
clear();
}
This is what my Pair.h file looks like:
#ifndef _Pair_h
#define _Pair_h
#include<iostream>
#include<string>
using namespace std;
class Pair{
private:
string key;
string value;
protected:
void setKey(const string& key);
public:
Pair();
Pair(string aValue, string key);
string getValue() const;
string getKey() const;
void setValue(const string& aValue);
};
#include "Pair.cpp"
#endif
Here is my Node.h file:
#ifndef _NODE
#define _NODE
template<class ItemType>
class Node
{
private:
ItemType item;
Node<ItemType>* next;
public:
Node();
Node(const ItemType& anItem);
Node(const ItemType& anItem, Node<ItemType>* nextNodePtr);
void setItem(const ItemType& anItem);
void setNext(Node<ItemType>* nextNodePtr);
ItemType getItem() const ;
Node<ItemType>* getNext() const ;
};
#include "Node.cpp"
#endif
, , ADT, :
List* hashtable[size];
const int size = 31; //31 is a arbitrary prime number for the hashtable