该函数用于在节点尾部插入一个节点。这只是我们书中的一个实践问题,我很难解决。 data()和link()函数分别用于检索节点的信息和下一个指针。编译器在这行上给我一个错误:cursor-> set_link(i);在C++(walter/savitch)中插入一个链接列表末尾的节点
void list_tail_insert(node* head_ptr, const node::value_type& entry)
{
assert (head_ptr != NULL);
const node *temp = head_ptr;
const node *cursor;
node *i = new node; // this is the new tail pointer
i->set_data(entry);
i->set_link(NULL);
if (!head_ptr) // if the linked list is empty
{
head_ptr = i; // the tail pointer is the new head pointer
}
for (cursor = head_ptr; cursor != NULL; cursor = cursor -> link())
{
cout << "Iterating to the tail pointer" << endl;
}
cursor->set_link(i);
}
在循环结束时,'光标== NULL'。 – 0x499602D2 2015-04-03 20:25:46