我基本上是建立一个数据库结构,我可以添加联赛,足球队和固定装置。我做了一个链表模板,并在每支球队和每个联赛的球队链表的装置链表。我也有一个联赛的链表在我的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);
}
}
这是我在链表中的add函数。当我在调试器中逐步执行时,这似乎修改和改变了预期的值。但是,当它从这个函数返回到数据库中的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
}
如果你能告诉我为什么会这样,我将非常感激。
您的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
}