簡體   English   中英

C++ 鏈接哈希表復制

[英]C++ linked hashtable copying

我必須用鏈表實現一個 HashTable。 它幾乎完成了(仍然缺少模板,會及時完成),但我遇到了一些問題。 基本上,我想在負載因子達到給定值(例如 50%)時調整哈希表的大小。 但我不知道我應該怎么做。 我有一個基本的想法:

  1. 創建一個具有新大小的臨時 HT
  2. Hash 從舊的HT到臨時的每個列表中的每個數據
  3. 刪除舊的HT
  4. 歸還臨時HT

雖然我無法弄清楚它的實現......

這是我到目前為止所擁有的:

//List:
struct Node
{
  string data;
  Node *next;
};

class List
{
  private:
    Node *head, *tail;
    int length;
    friend class HashTable;
  public:
    List();
    List(const List &L);
    //~List() {delete this;};
    List& operator =(List L);
    int find(string);
    void insert(string value);
    void remove_head();
    void remove_poz(int);
    void remove_tail();
    void clear();
    void display();
};

List::List()
{
   head = NULL;
   tail = NULL;
   length = 0;
}

List::List(const List& L)
{
    Node** temp = &head;
    const Node* source = L.head;
    while(source)
    {
        *temp = new Node(*source);
        temp = &(*temp)->next;
        source = source->next;
    }
}

List& List::operator =(List L)
{
  swap(head, L.head);
  return *this;
}

void List::insert(string value)
{
  Node* temp = new Node;
  temp->data = value;
  temp->next = NULL;

  if (!head)
      head = temp;
  if (tail)
      tail->next = temp;
  tail = temp;
  length++;
}

void List::display()
{
  Node *temp = new Node;
  temp = head;
  while (temp != NULL)
  {
    cout<<temp->data<<" ";
    temp = temp->next;
  }
  delete temp;
}


//HashTable:
class HashTable
{
  private:
    List *table;
    float load, stored;
    int slots;
    friend class List;
  public:
    HashTable();
    HashTable(int);
    ~HashTable();
    int hashFunc(string key);
    int findTable(string);
    int findList(string);
    HashTable& operator =(const HashTable&);
    void resize();    //I need this one
    void insert(string);
    void remove(string);
    void clear(int);
    void clear();
    void display();
};

HashTable::HashTable()
{
  stored = 0;
  load = 0.00;
  slots = 15;
  table = new List[slots];
}

HashTable::HashTable(int size)
{
    stored = 0;
    load = 0.00;
    slots = size;
    table = new List[slots];
}

int HashTable::hashFunc(string key)
{
  unsigned int i, ind = 0;
  for (i = 0; i<key.length(); ++i)
      ind = ind + key[i];
  ind %= slots;
  return ind;
}

HashTable& HashTable::operator =(const HashTable& T) //I suppose it is incorrect
{
    int i;
    HashTable temp(T.slots);
    for (i = 0; i < slots; ++i)
    {
        temp.table[i] = T.table[i];
    }

    return temp;
}

void HashTable::insert(string value)
{
  int ind = hashFunc(value);

  table[ind].insert(value);

  if (!table[ind].head->next) stored++;

  load = stored / slots;
  if (load > 0.50) resize();
}

(注意:這里只顯示可能需要的功能)

任何幫助、更正或建議將不勝感激:)

更新:

設法把這個拉下來:

void HashTable::resize()
{
  int i;
  int newSize = slots * 2;
  int newLoad = stored / newSize;
  HashTable HT(newSize);
  Node* temp;

  for (i = 0; i < slots; ++i)
  {
      temp = table[i].head;
      while (temp != NULL)
      {
          HT.insert(temp->data);
          temp = temp->next;
      }
  }
}

現在我有了一個名為 HT 的新 HashTable,其大小是原來的兩倍,並且所有元素都已正確插入。 但我不知道如何進行。

最簡單的方法是在 HashTable class 中添加一個 swapContents() 方法:

void HashTable::swapContents(HashTable & rhs)
{
   std::swap(table, rhs.table);
   std::swap(load, rhs.load);
   std::swap(stored, rhs.stored);
   std::swap(slots, rhs.slots);
   // any other member-variables of the HashTable class would get swapped here too
}

...然后在resize()方法的末尾調用swapContents(HT) ,以便 HT 成為較舊/較小的 HashTable(並被丟棄),而this將成為較新/更大的表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM