Here is the complete example with the test
struct Node
{
Node(Node* prev, int val)
: prev(prev), next(NULL), value(val)
{ }
Node *prev, *next;
int value;
};
void clear_list(Node*& head)
{
if (head != NULL)
{
Node* curr = head->next;
while (curr != NULL && curr != head)
{
std::cout << "Deleting " << curr->value << std::endl;
Node* temp = curr;
curr = curr->next;
delete temp;
};
delete head;
head = NULL;
}
}
void print_list(Node*& head)
{
if (head != NULL)
{
Node* curr = head;
do
{
std::cout << (curr == head ? "Head: " : "") << curr->value << std::endl;
curr = curr->next;
} while (curr != NULL && curr != head);
}
}
int main()
{
Node* head = new Node(NULL, 0);
Node* curr = head;
for (int i = 1; i <= 10; i++)
{
Node* prev = curr;
curr = new Node(prev, i);
prev->next = curr;
}
// Link end to head
curr->next = head;
//
print_list(head);
clear_list(head);
print_list(head);
}