#include #include //for exit #include //for assert #include "node.h" using namespace std; node::node(const value_type& initial_value) { value = initial_value; next = prev = 0; } //Link the two nodes together. //Sever n1 from its successor, if any. Let n2 be the successor of n1. //Sever n2 from its predecessor, if any. Let n1 be the predecessor of n2. void link(node *n1, node *n2) { assert(n1 != n2 || n1 == 0); if (n1) { if (n1->next) { //Make sure n1->next->prev is correct before blowing it away. assert(n1->next->prev == n1); n1->next->prev = 0; } n1->next = n2; } if (n2) { if (n2->prev) { //Make sure n2->prev->next is correct before blowing it away. assert(n2->prev->next == n2); n2->prev->next = 0; } n2->prev = n1; } } //Insert this node into the list that contains n, immediately before or after n. void node::insert_this_before(node *n) { if (n) { link(n->prev, this); } link(this, n); } void node::insert_this_after(node *n) { if (n) { link(this, n->next); } link(n, this); }