【发布时间】:2014-02-03 22:38:09
【问题描述】:
我有一个工作正常的函数 headinsert。这在“头部”添加了元素。 但我正在尝试制作一个函数 endinsert 在链表的末尾添加一个元素。 到目前为止我的代码是:
void IntList::endInsert(int the_number)
{
if (head == NULL)//if list is empty
{
head = new IntNode; //create new dynamic variable
head -> data = the_number; //add value to new variable
head -> link = NULL;
}
else
{
NodePtr temp = head; //initialize
//Now we want to insert in the back of list. Use for loop to get to the last element of list
for(temp = head; temp-> link != NULL ; temp = temp -> link)
{
temp->link = new IntNode; //create a new var
temp = temp ->link; //give it a "position"
temp ->data = the_number; //Give it a value
temp ->link = NULL; //The new variable will be the last element and therefore points to NULL
}
}
}
但由于某种原因它不起作用:(。有什么提示吗?
提前致谢!
【问题讨论】:
标签: c++ linked-list