我正在从事一个散列项目,目前在处理一系列链表时遇到困难。我的链表只能存储 1 个项目,所以我创建了一个具有 2 个成员变量(字符串键和字符串值)和各种成员函数的 Pair 类。我的问题是如何让 Pair 与我的 Linked-list 类一起工作?我的假设是执行以下操作:如果我想将数据添加到我的链表类中,我将创建一个带有参数的函数 - void insert(Pair data) - 这会帮助我在我的列表中插入 2 个项目吗?这是我的 C++ 代码,有人可以为我校对它并帮助我发现一些错误。
#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();
}
这是我的 Pair.h 文件的样子:
#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
这是我的 Node.h 文件:
#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 中创建一个列表数组时,我的私有(private)成员将是:
List* hashtable[size];
const int size = 31;//31 是哈希表的任意素数
最佳答案
Make a new nlist = new List();
(新建一个list对象),set nlist headptr = anotherList.headptr.getItem()
继续复制将指向 anotherList
节点的 ITEMS 指向您的 nlist
节点,然后返回 nlist
;
newchainPtr->setNext(nullptr);
似乎没有必要,您应该从 anotherList
检查 NULL。链表数组是指链表对象的数组,节点是链表对象的一部分。
如果这让您感到困惑,请查找 DEEP 拷贝,您会发现它。确保释放所有未使用的内存。希望这对您有所帮助!
关于c++ - 链接列表数组(使用散列),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34459684/