0

我基本上是在构建一个数据库结构,我可以在其中添加联赛、足球队和固定装置。我已经制作了一个链表模板,并有一个每个球队的赛程链表和每个联赛球队的链表。我还有一个dataBase班级内的联赛链接列表。我设法添加了将联赛添加到数据库的功能,但现在我无法添加团队。我已经逐步完成了我的 add 函数,它分配了正确的值,但是当从函数返回时team_list,指定联赛中的链表为空。

这是我addTeam在数据库中的功能。

void Database::addTeam(Team team)
{
    Node<League>* ptr = league_list.getHead();

    while(ptr!=0)
    {
        // if the team's league matches current in the list add to this league
        if(ptr->getData().getLeagueName()==team.getLeagueName())
        {
            ptr->getData().getTeamList().add(team);
            return;
        }
        else // doesn't match so go to next league in list
        {
            ptr=ptr->getNextNode();
        }
    }
    if(ptr==NULL) // if not found prompt user
    {
        throw exception(ERRORMSG);
    }
}

这是我在链接列表中的添加功能。当我在调试器中单步执行时,这似乎会修改和更改预期值。但是,当它从此函数返回到addTeam数据库中的函数时,什么都没有改变。

template<class data>
void LinkedList<data>::add(data a)
{
    Node<data> *nodePtr;
    nodePtr=new Node<data>(a);

    nodePtr->setNextPointer(0);

    if(head==0) // if head is empty
    {
        head=nodePtr; // make the new node the first node
        head->setPreviousPointer(0); // make the new node point to null
    }

    else
    {
        if(nodePtr==0) // if head is null
        {
            last=0; // if list unoccupied _last is null
        }
        while(nodePtr->getNextNode()!=0)
        {
            last=nodePtr->getNextNode();
        }
        nodePtr->setPreviousPointer(last);
        // set the old last node to point to the new node
        last->setNextPointer(nodePtr);
    }

    last=nodePtr;       // point to the new end of list
    current=nodePtr;    // set the current node to the last in list
}

对于为什么会发生这种情况的任何帮助,我将不胜感激。

4

1 回答 1

0

您的功能存在一些问题addTeam

template<class data>
void LinkedList<data>::add(data a)
{
    Node<data> *nodePtr;
    nodePtr=new Node<data>(a);

    // this should not be necessary, do that in the constructor of Node
    nodePtr->setNextPointer(0);

    if(head==0) // if head is empty
    {
        head=nodePtr; // make the new node the first node
        // this is not necessary, nodePtr already has been set up above
        // not to have a next node
        // head->setPreviousPointer(0); // make the new node point to null
    }
    else
    {
        // this check will never fail; what do you want to check here?
        if(nodePtr==0) // if head is null
        {
            last=0; // if list unoccupied _last is null
        }

        // this loop will never run
        /*
        while(nodePtr->getNextNode()!=0)
        {
            last=nodePtr->getNextNode();
        }
        */
        // either you have stored a Node<data>* last as member of LinkedList
        // or you need something like
        Node<data>* last = head;
        while(last->getNextNode() != 0)
        {
            last = last->getNextNode();
        }

        nodePtr->setPreviousPointer(last);
        // set the old last node to point to the new node
        last->setNextPointer(nodePtr);
    }

    last=nodePtr;       // point to the new end of list
    current=nodePtr;    // set the current node to the last in list
}
于 2013-06-10T01:12:29.240 回答