0
我一直想弄清楚如何使用指針連接兩個不同的結構節點。但我無法做到這一點。看到我的畫波紋管。在左邊我 有一個「treeNode」與兩個指針(下和右)。 r指針連接到一個名爲「branchNode」的不同節點,每個「treeNode」我有五個鏈接的「branchNodes」。如何使用指針連接兩個不同的節點類型(結構)?
這是我的問題:例如,如果「branchNode」1 不存在,我就想創建一個臨時節點 來插入它。但我不知道如何使這個臨時節點 收到 「branchNode」 2.
see image here \ n請查看的內存地址我的代碼波紋管:
的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;
}
}
}
你是什麼意思「接收內存地址」?如果你正在談論一個指針,那麼'treeNode * temp'就是你的答案。 – tadman
@tedman,我的意思是臨時節點將指向最後創建的branchNode。我可以使用treeNode *創建一個臨時節點,但是我不能像這樣插入一個分支節點。 –
你在找演員嗎? 'branchNode * node =(branchNode *)temp;' – Rabbid76