我基本上建立了一个数据库结构,我可以添加联赛,足球队和固定装置。我制作了一个链表模板,并在每个团队中都有一个固定装置列表和每个联盟中的团队链表。我的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
}
对于为什么会发生这种情况,我会非常感激。
答案 0 :(得分: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
}