programmingblues
programmingblues

Reputation: 113

Array of Linked Lists (using Hashing)

I am working on a hashing project and is currently having difficulty with an array of linked-lists. My linked-list can only store 1 item, so I've created a Pair class with 2 member variables(string key and string value) and various member functions. My question is how can I make Pair work with my Linked-list class? My assumption is to do the following: If I wanted to add data into my linked list class I would create a function with the parameter - void insert(Pair data) - will this help me insert 2 items in my list? Here is my c++ code, can someone proof-read it for me and help me spot 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(); //default constructor
    List(const List& anotherList); //copy constructor -iffy, I'm not sure if my definition for this function is correct-
    virtual ~List(); //destructor
    void insert(Pair data); //insert item
    bool remove(Pair data); //remove item
    bool find(Pair data); //find item
    int getSize(); //size of list
    bool isEmpty(); //checks if list is empty
    void clear(); //clear list
};
#include "List.cpp"
#endif

.cpp

//inserting data to list
void List::insert(Pair data){

        Node<string>* newptr = new Node<string> (); //create new node
        newptr->setItem(data); //set character into node
        newptr->setNext(headPtr); //sets the ptr to headptr(null)
        headPtr = newptr; //headptr points to the node you've just created
        size++; //increment the size
}


 //clears the entire list
void List::clear(){
    Node<string>* delPtr = headPtr; //delPtr points to the top of the list
    while(delPtr != nullptr){
        headPtr = delPtr->getNext(); //sets the head pointer to the next node
        delPtr->setNext(nullptr); //begins the process of removing the data from the top of the list here
        delete delPtr;
        delPtr = headPtr; //sets the delPtr to the headptr after deleting this way we will continue to delete data from the list until the list is empty
    }

    headPtr = nullptr;
    delPtr = nullptr;
    size = 0;
}

 //destructor
List::~List() {
    clear();
}

Here is how my Pair.h file looks:

#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; // A data item
   Node<ItemType>* next; // Pointer to next node

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 ;
}; // end Node

#include "Node.cpp"
#endif

With all that said, my best guess is that when I do create an array of lists in my dictionary ADT, my private members would be:

List* hashtable[size];

const int size = 31; //31 is a arbitrary prime number for the hashtable

Upvotes: 4

Views: 882

Answers (1)

user3101311
user3101311

Reputation: 11

Make a new nlist = new List(); (creating a new list object), set nlist headptr = anotherList.headptr.getItem() and continue to copy the ITEMS pointing to the anotherList nodes to your nlist nodes then return nlist;

newchainPtr->setNext(nullptr); seems unnecessary and you should check for NULL from anotherList instead. An array OF linked lists means an array of linked list objects, the nodes are a part of the list object.

Look up DEEP copy if this confuses you and you'll spot it. Make sure to free up any unused memory. Hope this helps!

Upvotes: 1

Related Questions