如何使用指针连接两种不同的节点类型(结构)?
How to connect two differents nodes types (structures) using pointers?
我一直在试图弄清楚如何使用指针连接两个不同的结构节点。但我做不到。请看我的画图。在左边 I 有一个带有两个指针(下方和右侧)的"treeNode"。r指针连接到一个名为"branchNode"的不同节点,对于每个"treeNode",我有五个链接的"branchNodes"。
这是我的问题:例如,如果"分支节点"1 不存在,我想创建一个临时节点 以插入它。但是我不知道如何制作这个临时节点 接收"分支节点"的内存地址 2.
见图片 这里 请参阅下面的代码:
主.cpp
#include "table.h"
#include <iostream>
int main(){
Table xxx;
xxx.addTreeNodes(2,100);
xxx.addTreeNodes(3,100);
xxx.addTreeNodes(1,100);
return 0;
表.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
表.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"和一些"branchNode"。我能够按照@Rabbid76的提示正确指向另一种类型的节点(谢谢!也许还有另一种方式,但我已经很高兴了。
主.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;
}
表.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
表.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;
}
}
相关文章:
- 为什么将函数的返回类型从结构节点*更改为void后,链表的元素没有显示create_ll和显示?
- 无法使用类型为"结构节点 *"的左值初始化类型为"结构节点 *"的参数
- C++错误(从不兼容的类型"void"分配给树节点*)
- 创建的节点类型不同
- 基于给定字符串数据类型的链表删除节点
- 媒体基础获取拓扑节点的 IMFMedia类型
- 如何使用指针连接两种不同的节点类型(结构)?
- 如何在没有动态调用的情况下确定子节点的类型
- 查找树(不属于任何特定类型的简单连接树)中两个节点之间的路径
- 当我返回节点*时,C++中出现未知类型错误
- 我不知道如何调用参数为节点类型的函数
- 联合而不是aligned_storage_t作为容器节点类型的一部分
- 如何将不同类型的节点存储在一个链接列表中
- 如何使用同一链接列表一次处理各种类型的节点
- 错误 - 列表节点未命名类型
- 如何为链接列表节点定义一个以指针为模板类型的模板类
- 使用公共类函数初始化节点类型<Type>指针C++
- 错误:请求"cur"中的成员"成员",该成员属于非类类型节点<int>* 二叉树
- 节点C++插件 - 当类型数组(Float32Array)作为参数传递时,如何访问它?
- 指向两种不同类型节点的指针