Array of linked lists (using Hashing)

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(); //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();
}

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; // 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

, , ADT, :

List* hashtable[size];

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

+4
1

Make a new nlist = new List(); ( ), set nlist headptr = anotherList.headptr.getItem() ITEMS, anotherList, nlist, nlist;

newchainPtr->setNext(nullptr); , NULL anotherList. , .

DEEP-, , . . , !

+1

Source: https://habr.com/ru/post/1621671/


All Articles