0

我正在创建一个多级双向链接列表,我正在其中执行添加节点操作。我很困惑,因为根据我下面提到的逻辑代码,它应该打印该值,但它没有打印。输出没有错,但也许我做错了什么,这可能会在展平该列表时产生影响。

我有这个列表的 3 个级别。addNode 函数将在第一级添加,而 addChild 函数将在第二级或第三级的任何位置添加一个节点。

代码是:

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;
}

}

请帮助我,在此先感谢大家。

4

1 回答 1

0

从您在评论中输入的输出来看,似乎没有在第二级添加任何内容。您期望在第二级添加节点的地方,它们被添加到第一级。

我认为问题出在添加子函数的底部:

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而不是我认为你期望的子节点。可能添加子函数应该返回newChild

于 2013-09-20T15:53:31.913 回答