【发布时间】:2014-07-08 19:41:35
【问题描述】:
我不明白为什么一个功能不起作用。它与 const 指针有关。
//prints the contents of the list interval [begin,end)
//to the output stream os; values should be separated by spaces
void print(const Node* begin, const Node* end, ostream& os)
{
Node* current_node = begin;
do
{
os << current_node->value << " ";
current_node = current_node->next;
}
while(current_node != end);
os << endl;
}
我不能改变这个函数的头部。
这个功能会起作用吗?
//erases the list interval [begin,end)
//attention: this should not erase 'end', see remarks above
void erase(Node* begin, Node* end){
Node* current_node = begin;
Node* next_node = begin->next;
Node* begin_node = begin->prev;
while(current_node != end){
next_node->prev = current_node->prev;
begin_node->next = next_node;
delete current_node;
current_node = next_node;
next_node = current_node->next;
}
}
这是节点的结构
struct Node
{
int value;
struct Node* next;
struct Node* prev;
};
【问题讨论】:
-
实际上有什么问题?编译器错误、运行时崩溃、意外结果?您的问题不清楚。
标签: c++ c printing delete-operator doubly-linked-list