C++ 在c语言的多级双链接列表中添加节点
我正在创建一个多级双链接列表,我正在其中执行添加节点操作。我很困惑,因为根据我下面提到的逻辑代码,它应该打印值,但它没有打印。输出没有错,但可能我做错了什么,这可能会影响列表的平坦度 我有3个级别的列表。addNode函数将在第一级添加,其中as addChild函数将在第二级或第三级的任何位置添加节点 代码是:C++ 在c语言的多级双链接列表中添加节点,c++,c,data-structures,C++,C,Data Structures,我正在创建一个多级双链接列表,我正在其中执行添加节点操作。我很困惑,因为根据我下面提到的逻辑代码,它应该打印值,但它没有打印。输出没有错,但可能我做错了什么,这可能会影响列表的平坦度 我有3个级别的列表。addNode函数将在第一级添加,其中as addChild函数将在第二级或第三级的任何位置添加节点 代码是: Student* StudentList::addNode(int num) { Student* node = new Student(num); if(head =
Student* StudentList::addNode(int num)
{
Student* node = new Student(num);
if(head == 0) {
head = node;
tail = node;
} else {
node->prev = tail;
tail->next = node;
tail = tail->next;
}
return node;
}
添加子项:
Student* StudentList::addChild(Student* node1, int num)
{
Student* node = head;
Student* temp;
Student* temp1;
// traversing the list to find exact location
while(node != 0)
{
if(node->num == node1->num) {
break;
} else {
temp = node->child;
while(temp)
{
if(temp->num == node1->num) {
node = temp;
break;
} else {
temp1 = temp->child;
while(temp1)
{
if(temp1->num == node1->num) {
node = temp1;
break;
} else {
temp1 = temp1->next;
}
}
temp = temp->next;
}
}
node = node->next;
}
}
Student* newChild = new Student(num);
Student* curr = node->child;
if(curr == 0)
{
node->child = newChild;
} else {
while(curr->next)
{
curr = curr->next;
}
newChild->prev = curr;
curr->next = newChild;
}
return node1;
}
主要方法代码为:
StudentList* sl = new StudentList();
Student* newNode;
Student* newNode1;
newNode = sl->addNode(1);
newNode1 = sl->addChild(newNode,11);
sl->addChild(newNode1, 111);
sl->addChild(newNode1, 112);
newNode1 = sl->addChild(newNode,12);
newNode = sl->addNode(2);
newNode1 = sl->addChild(newNode,21);
newNode1 = sl->addChild(newNode,22);
sl->addChild(newNode1, 221);
sl->addChild(newNode1, 222);
sl->addChild(newNode1, 223);
newNode = sl->addNode(3);
newNode = sl->addNode(4);
newNode1 = sl->addChild(newNode,41);
sl->addChild(newNode1, 411);
sl->printList();
我的打印列表代码是:
void StudentList::printList(){
Student* curr = head;
while(curr){
cout<< *curr <<endl;
if(curr->child){
Student* newCurr = curr->child;
while(newCurr){
cout<< "*{"<<newCurr->num <<"}"<<endl;
if(newCurr->child){
Student* newCurr2 = newCurr->child;
while(newCurr2){
//according to my login this 3rd level childs num(id) value
//should be printed used only for 2nd level according to my
//logic.. where is the problem am i doing wrong any thing?
cout<< "**{"<<newCurr2->num <<"}"<<endl;
newCurr2 = newCurr2->next;
}
}
newCurr = newCurr->next;
}
}
curr = curr->next;
}
void StudentList::printList(){
学生*curr=人头;
while(curr){
cout从您在注释中输入的输出来看,第二级似乎没有添加任何内容。如果您希望在第二级添加节点,则它们将添加到第一级
我认为问题出在add child函数的底部:
Student* StudentList::addChild(Student* node1, int num)
{
// rest of function. node1 is never modified
return node1;
}
您总是将节点输入返回给该函数
newNode = sl->addNode(1);
newNode1 = sl->addChild(newNode,11);
sl->addChild(newNode1, 111);
sl->addChild(newNode1, 112);
您会发现newNode1==nodeNode
而不是我认为您期望的子节点。可能add child函数应该返回newChild
您能提供程序输出吗?然后描述输出中您不期望的内容:)@Jimbo这是程序的输出,我期待您的帮助t根据上面的代码'1st level 1*{11}*{111}*{112}*{12}1st level 2*{21}*{22}*{221}*{222}*{223}1st level 3 1st level 4*{41}*{411}使用**输出**3位数字,我同意,但当我返回newChild时,它会给我错误(访问冲突读取位置)。那是什么?我可以给你发电子邮件我的项目吗?你能帮我解决这个问题吗?访问冲突意味着你正在访问一个非法的内存部分。可能是取消引用空指针或类似的东西。不。我不会帮你解决它。这取决于你自己调试代码!我建议使用调试器来逐步检查你的代码并找到它找出违规的地方,或者使用printf语句或其他方法尝试确定代码出错的地方。感谢您的上述建议是正确的,我的代码按照我创建的逻辑工作,谢谢。Brill,很高兴您找到了答案:)