2013-05-11 40 views
0
void insert_node(Node *p){ 
    Node *tmp; 
    tmp = new Node; 
    tmp = p; 
    if(first == NULL){ 
     status ++; 
     tmp->next = NULL; 
     first = tmp; 
    } 
    else{ 
     status ++; 
     tmp->next = first; 
     first = tmp; 
    } 
} 


void Resize_content(){ 

    if(status2 >= 7*space/10){ 
     Entry *tmp; 
     tmp = new Entry[2*space]; 
     for(int i=0;i<space;i++){ 
      Node *paux; 
      paux = content[i].first; 
      while(paux){ 
       //Node &caux = *paux; 
       tmp[paux->hash_index%(2*space)].insert_node(paux); 
       paux = paux ->next; 
      } 
     } 
     delete[] content; 
     content = new Entry[2*space]; 
     content = tmp; 
     space = 2*space; 
    } 

} 

content是一個向量列表大小空間的條目。一旦元素的數量超過空間msut的70%,就會將元素移動到不同的位置。麻煩的是與元素構成同一列表,因爲paux->未來insert_node成爲NULL並不會被移動如何使函數不改變指針形式參數一次完成?

+2

你的功能沒有意義。首先創建一個'new Node',將其地址存儲在'tmp'中。然後通過將'p'分配給'tmp'來泄漏它(丟失唯一的指針)。然後你在'* p'(通過指針'tmp')做一些操作。你想做什麼? – Angew 2013-05-11 12:39:47

+0

我正在嘗試將節點從列表移動到另一個節點 – 2013-05-11 12:41:27

+0

我們的信息太少:什麼是「第一」和「狀態」?嘗試提供[最小的,自給自足的例子](http://sscce.org/)。 – Angew 2013-05-11 12:42:49

回答

0

假如我正確地理解你的意圖後,這應該是解決方案:

void insert_node(Node *p){ 
    if(first == NULL){ 
     status ++; 
     //make p the start of a new 1-member list 
     p->next = NULL; 
     first = p; 
    } 
    else{ 
     status ++; 
     // prepend p to existing list 
     p->next = first; 
     first = p; 
    } 
} 


void Resize_content(){ 
    if(status2 >= 7*space/10){ 
     Entry *tmp; 
     tmp = new Entry[2*space]; 
     for(int i=0;i<space;i++){ 
      Node *paux; 
      paux = content[i].first; 
      while(paux){ 
       Node *caux = paux; //store current node's address 
       paux = paux ->next; // remember next node to process 
       tmp[paux->hash_index%(2*space)].insert_node(caux); //move current node 
      } 
     } 
     delete[] content; 
     content = tmp; 
     space = 2*space; 
    } 

} 

是否有特定的之所以你自己這樣做,而不是使用std::vector,std::list(或std::forward_list)和/或std::unordered_map(用於整個散列)?

+0

我更新了您的評論 – 2013-05-11 13:03:56

+0

我更新了我的初始代碼 – 2013-05-11 13:11:40

+0

@AntonacheAlexandru編輯以反映您的問題編輯。 – Angew 2013-05-11 13:31:03