C++ 如何使用指针连接两种不同的节点类型(结构)?
我一直在试图找出如何使用指针连接两个不同的结构节点。但我很抱歉我没能做到。看我下面的画。在左边我 有一个带有两个指针的“树节点”(下方和右侧)。r指针连接到称为“branchNode”的不同节点,对于每个“treeNode”,我有五个链接的“branchNode” 这是我的问题:例如,如果“branchNode”1 不存在,我想创建一个临时节点 插入它。但是我不知道如何创建这个临时节点 接收“branchNode”2的内存地址 \nSee我的代码如下: main.cpp 表1.cppC++ 如何使用指针连接两种不同的节点类型(结构)?,c++,pointers,struct,containers,C++,Pointers,Struct,Containers,我一直在试图找出如何使用指针连接两个不同的结构节点。但我很抱歉我没能做到。看我下面的画。在左边我 有一个带有两个指针的“树节点”(下方和右侧)。r指针连接到称为“branchNode”的不同节点,对于每个“treeNode”,我有五个链接的“branchNode” 这是我的问题:例如,如果“branchNode”1 不存在,我想创建一个临时节点 插入它。但是我不知道如何创建这个临时节点 接收“branchNode”2的内存地址 \nSee我的代码如下: main.cpp 表1.cpp #包括“
#包括“table.h”
#包括
#包括
表::表(){
branchDim=5;
head=NULL;
treeTemp=NULL;
treeCurr=NULL;
branchHead=NULL;
}
int Table::treeAddress(int地址,int dim){
//此函数用于计算地址
//三烯醇类化合物。
浮点值=1+(整数)((浮点)地址/(浮点)dim);
如果(地址%dim==0){
瓦尔--;
}
返回val;
}
void表::addTreeNodes(int-pos,int-value){
//如果需要,此部件将按顺序创建一个树节点
//需要。很好,跳过这部分。
treeNode*tn=新的treeNode;
tn->address=treeAddress(pos,branchDim);
//如果该表不存在,请创建一个树节点
if(head==NULL){
tn->below=NULL;
tn->right=NULL;
水头=tn;
}
否则{
//在之前插入treeNode。
if(tn->addressaddress){
tn->低于=水头;
tn->right=NULL;
水头=tn;
}
否则{
treeCurr=头部;
树顶=头部;
while(treeCurr!=NULL&&treeCurr->addressaddress){
treeTemp=treeCurr;
treeCurr=treeCurr->下方;
}
//在尾部插入treeNode。
如果(treeCurr==NULL&&tn->address>treeTemp->address){
treeTemp->below=tn;
tn->below=treeCurr;
tn->right=NULL;
}
否则{
//在其他两个节点之间插入treeNode。
如果(tn->addressaddress){
treeTemp->below=tn;
tn->below=treeCurr;
tn->right=NULL;
}
否则{
删除[]tn;
}
}
}
}
//这部分将创建一个branchNode。这是一个大问题。。。
branchNode*bn=新的branchNode;
bn->address=pos;
treeCurr=头部;
int tPos=树首饰(位置、分支);
while(treeCurr!=NULL&&tPos!=treeCurr->address){
treeCurr=treeCurr->下方;
}
//如果分支是空的。
if(treeCurr->right==NULL){
treeCurr->right=bn;
bn->next=NULL;
bn->address=pos;
}
否则{
//这里我想把分支节点放在第一个分支节点之前。
如果(位置<(treeCurr->右侧)->地址){
branchHead=treeCurr->right;//不知为什么,
bn->next=branchHead;//我不能那样做!!!!!!!!!!。
treeCurr->right=bn;
bn->data=value;
}
}
}
为了简单起见,假设只有一个“树节点”和一些“分支节点”。根据@rabbi76(谢谢!)的提示,我能够正确地指向另一种类型的节点。也许还有别的办法,但我已经很高兴了
main.cpp
表1.cpp
#包括“table.h”
#包括
#包括
表::表(){
head=NULL;
branchCurr=NULL;
tn=新的三烯醚;
}
void表::addTreeNodes(int-pos,int-value){
branchNode*bn=新的branchNode;
bn->address=pos;
bn->data=value;
tn->below=NULL;
if(head==NULL){
水头=tn;
bn->next=NULL;
tn->right=bn;
}
否则{
如果(位置<((分支节点*)头部->右侧)->地址){
branchCurr=(branchNode*)头->右;
bn->address=pos;
bn->data=value;
tn->right=bn;
bn->next=branchCurr;
}
否则{
删除[]bn;
}
}
}
void表::print(){
branchCurr=(branchNode*)头->右;
while(branchCurr!=NULL){
你说的“接收内存地址”是什么意思?如果你说的是指针,那么treeNode*temp
就是你的答案。@tedman,我的意思是临时节点将指向创建的最后一个branchNode。我可以使用treeNode*创建临时节点,但我不能像这样插入branchNode。你为什么不能插入这样的节点呢?我想你对指针的理解是n需要更多的实验和研究来支撑它。@Rabbi76,谢谢你的提示。我会研究更多的!@tadman是的,我需要了解更多。问题是:插入这样的节点,可能在某个时候我必须转换它,我还不知道如何做。Rabbi76给了我一个建议,我正在检查。
#include "table.h"
#include <iostream>
int main(){
Table xxx;
xxx.addTreeNodes(2,100);
xxx.addTreeNodes(3,100);
xxx.addTreeNodes(1,100);
return 0;
#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
#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;
}
}
}
#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;
}
#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
#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;
}
}