【发布时间】:2014-02-04 22:55:08
【问题描述】:
我正在处理一个链表项目,我有两个无法正常工作的功能。
首先我有一个插入函数,可以将元素添加到列表中。但是该函数首先要检查元素是否已经在列表中。它应该通过使用 bool 函数 contains() 来做到这一点。如果元素已经在列表中,那应该返回 true
我的插入函数:
void StringSet::insert(string element)
{
NodePtr temp =head;
if (contains("element") == true)
{
return;
}
if(head == NULL)
{
head = new StringNode;
head->setData(element);
head->setLink(NULL);
}
else
{
temp = new StringNode;
temp->setData(element);
temp->setLink(head);
head = temp;
}
}
还有我的包含功能:
bool StringSet::contains(string element)
{
NodePtr temp = head;
while(temp != NULL)
{
if (temp->getData() == element)
{
cout<<"This country has already been registered!"<<endl;
return true;
}
temp = temp->getLink();
}
}
【问题讨论】:
-
getLink()是如何定义的?contains看起来不错(除了它缺少return false。 -
contains("element")与contains(element)大不相同。
标签: c++ linked-list