2017-07-04 1 views
0

私はポインタを使って2つの異なる構造ノードを接続する方法を理解しようとしています。しかし、私はそれをすることができませんでした。私の描き下ろしを見てください。左には に2つのポインタ(下と右)を持つ "treeNode"があります。 r-pointerは "branchNode"と呼ばれる別のノードに接続し、 "treeNode"ごとに5つのリンクされた "branchNodes"を持っています。ポインタを使用して2つの異なるノードタイプ(構造)を接続するにはどうすればよいですか?

ここに私の問題があります:たとえば、 "branchNode" 1 が存在しない場合は、それを挿入するために一時ノード を作成するように傷つきます。しかし、私は、この一時的なノード は "branchNode" nSee \ 2.

see image here のメモリアドレスを受け取るようにする方法がわからない怒鳴る私のコード:

main.cppに

#include "table.h" 
#include <iostream> 

int main(){ 

    Table xxx; 

    xxx.addTreeNodes(2,100); 
    xxx.addTreeNodes(3,100); 
    xxx.addTreeNodes(1,100); 

    return 0; 

table.h

#ifndef TABLE_H_ 
#define TABLE_H_ 

class Table{ 
public: 
    Table(); 
    int treeAddress(int newAddress, int dim); 
    void addTreeNodes(int pos, int value); 


private: 
    struct treeNode { 
    public: class branchNode; 
     int address; 
     treeNode* right; 
     treeNode* below; 
    }; 

    struct branchNode : public treeNode{ 
     int address; 
     int data; 
     branchNode* next; 
    }; 

    treeNode* treeCurr; 
    treeNode* treeTemp; 
    treeNode* head; 
    branchNode* branchHead; 

    int branchDim; 

}; 

#endif 

table.cpp

#include "table.h" 
#include <iostream> 
#include <stddef.h> 

Table::Table(){ 
    branchDim = 5; 
    head = NULL; 
    treeTemp = NULL; 
    treeCurr = NULL; 

    branchHead = NULL; 

} 

int Table::treeAddress(int Address, int dim){ 
    // This function is used to calculate the address 
    // of treeNodes. 
    float val = 1 + (int)((float)Address/(float)dim); 
    if (Address % dim == 0){ 
     val--; 
    } 

    return val; 
} 

void Table::addTreeNodes(int pos, int value){ 
    // This part will create one treeNode in order, if 
    // needed. Works fine, just skip this part. 
    treeNode* tn = new treeNode; 
    tn -> address = treeAddress(pos, branchDim); 

    // if the table doesn't exist. Create one treeNode 
    if (head == NULL){ 
     tn -> below = NULL; 
     tn -> right = NULL; 
     head = tn; 
    } 
    else{ 
     // insert treeNode before. 
     if(tn -> address < head -> address){ 
      tn -> below = head; 
      tn -> right = NULL; 
      head = tn; 
     } 
     else{ 
      treeCurr = head;  
      treeTemp = head; 
      while(treeCurr != NULL && treeCurr -> address < tn -> address){ 
       treeTemp = treeCurr; 
       treeCurr = treeCurr -> below; 
      } 

      // insert treeNode on tail. 
      if (treeCurr == NULL && tn -> address > treeTemp -> address){ 
       treeTemp -> below = tn; 
       tn -> below = treeCurr; 
       tn -> right = NULL; 
      } 
      else{ 
      // insert treeNode between two others nodes. 
       if (tn -> address < treeCurr -> address){ 
        treeTemp -> below = tn; 
        tn -> below = treeCurr; 
        tn -> right = NULL; 
       } 
       else{ 
        delete[] tn; 
       } 
      } 
     } 
    } 

    // This part will create one branchNode. Here is the big problem... 

    branchNode* bn = new branchNode; 
    bn -> address = pos; 

    treeCurr = head; 
    int tPos = treeAddress(pos, branchDim); 
    while(treeCurr != NULL && tPos != treeCurr -> address){ 
     treeCurr = treeCurr -> below; 
    } 
    //If the branch is empty. 
    if (treeCurr -> right == NULL){ 
     treeCurr -> right = bn; 
     bn -> next = NULL; 
     bn -> address = pos; 
    } 

    else{ 
     //Here I wanna put the branchNode before the first branchNode. 
     if (pos < (treeCurr -> right) -> address){ 
      branchHead = treeCurr -> right; // for some reason, I don't know why, 
      bn -> next = branchHead;  // I can't do that!!!!!!!!!!. 
      treeCurr -> right = bn; 
      bn -> data = value; 
     } 
    } 
} 
+0

「受信メモリアドレス」とはどういう意味ですか?あなたがポインタについて話しているなら、 'treeNode * temp'があなたの答えです。 – tadman

+0

@tedman、私は、一時ノードが最後に作成されたbranchNodeを指すことを意味しました。私はtreeNode *を使って一時ノードを作成できますが、このようにbranchNodeを挿入することはできません。 –

+0

キャストをお探しですか? 'branchNode * node =(branchNode *)temp;' – Rabbid76

答えて

0

一つだけ "のTreeNode" と簡略化のために、いくつかの "branchNode" が存在すると仮定。 @ Rabbid76(感謝!)のヒントにしたがって、別のタイプのノードを正しく指すことができました。多分別の方法があるかもしれませんが、私はすでに幸せです。

main.cppに

#include "table.h" 
#include <iostream> 

int main(){ 

    Table xxx; 

    xxx.addTreeNodes(5,100); 
    xxx.addTreeNodes(3,140); 
    xxx.addTreeNodes(2,20); 

    xxx.print(); 

    return 0; 
} 

table.h

#ifndef TABLE_H_ 
#define TABLE_H_ 

class Table{ 
public: 
    Table(); 
    void addTreeNodes(int pos, int value); 
    void print(); 

private: 
    struct treeNode { 
    public: class branchNode; 
     treeNode* right; 
     treeNode* below; 
    }; 

    struct branchNode : public treeNode{ 
     int address; 
     int data; 
     branchNode* next; 
    }; 

    branchNode* branchCurr; 
    treeNode* head; 
    treeNode* tn; 
}; 

#endif 

table.cpp

#include "table.h" 
#include <iostream> 
#include <stddef.h> 

Table::Table(){ 
    head = NULL; 
    branchCurr = NULL; 
    tn = new treeNode; 
} 

void Table::addTreeNodes(int pos, int value){ 

    branchNode* bn = new branchNode; 
    bn -> address = pos; 
    bn -> data = value; 

    tn -> below = NULL; 

    if (head == NULL){ 
     head = tn; 
     bn -> next = NULL; 
     tn -> right = bn; 
    }   
    else{ 
     if (pos < ((branchNode*)head -> right) -> address){ 
      branchCurr = (branchNode*)head -> right; 
      bn -> address = pos; 
      bn -> data = value; 
      tn -> right = bn; 
      bn -> next = branchCurr; 
     } 
     else{ 
      delete[] bn; 
     } 
    } 
} 

void Table::print(){ 
    branchCurr = (branchNode*)head -> right;    
    while(branchCurr != NULL){ 
     std::cout << "address: " << branchCurr -> address 
        << ", stored value: " << branchCurr -> data << std::endl; 
     branchCurr = branchCurr -> next; 
    } 
} 
関連する問題