加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 链接列表数组(使用哈希)

发布时间:2020-12-16 07:15:05 所属栏目:百科 来源:网络整理
导读:我正在进行一个散列项目,目前在链接列表数组方面遇到了困难.我的链表只能存储1个项目,所以我创建了一个包含2个成员变量(字符串键和字符串值)和各种成员函数的Pair类.我的问题是如何使Pair与我的Linked-list类一起工作?我的假设是执行以下操作:如果我想将数
我正在进行一个散列项目,目前在链接列表数组方面遇到了困难.我的链表只能存储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中创建一个列表数组时,我的私人成员将是:

List * hashtable [size];

const int size = 31; // 31是哈希表的任意素数

解决方法

制作一个新的nlist = new List(); (创建一个新的列表对象),设置nlist headptr = anotherList.headptr.getItem()并继续将指向anotherList节点的ITEMS复制到你的nlist节点然后返回nlist;

newchainPtr-> setNext(nullptr);似乎没必要,你应该从anotherList检查NULL.链接列表数组表示链接列表对象的数组,节点是列表对象的一部分.

查看DEEP副本如果这让您感到困惑,您会发现它.确保释放任何未使用的内存.希望这可以帮助!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读