該函數用於在節點尾部插入一個節點。這只是我們書中的一個實踐問題,我很難解決。 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