int
main(int argc, char *argv[]) {
  struct node *list;
  int i;

  /* 4 <-> 3 <-> 2 <-> 1 <-> 0 */
  for (i = 0; i < 5; i++)
    add_as_head(&list, i);

  /* 4 <-> 3 <-> 2 <-> 1 <-> 0 <-> 99 */
  add_as_tail(&list, 99);

  /* 4 <-> 3 <-> 2 <-> 1 <-> 0 */
  delete_node_by_value(&list, 99);

  /* 3 <-> 2 <-> 1 <-> 0 */
  delete_head(&list);
  
  /* 3 <-> 1 <-> 0 */
  delete_node(list->next);

  /* 3 <-> 1 */
  delete_tail(&list);
  
  disp_list(list);

  free_list(&list);

  return EXIT_SUCCESS;
}
void FreeRegionList::add_ordered(FreeRegionList* from_list) {
  check_mt_safety();
  from_list->check_mt_safety();

  verify_optional();
  from_list->verify_optional();

  if (from_list->is_empty()) {
    return;
  }

  if (is_empty()) {
    add_as_head(from_list);
    return;
  }

  #ifdef ASSERT
  FreeRegionListIterator iter(from_list);
  while (iter.more_available()) {
    HeapRegion* hr = iter.get_next();
    // In set_containing_set() we check that we either set the value
    // from NULL to non-NULL or vice versa to catch bugs. So, we have
    // to NULL it first before setting it to the value.
    hr->set_containing_set(NULL);
    hr->set_containing_set(this);
  }
  #endif // ASSERT

  HeapRegion* curr_to = _head;
  HeapRegion* curr_from = from_list->_head;

  while (curr_from != NULL) {
    while (curr_to != NULL && curr_to->hrs_index() < curr_from->hrs_index()) {
      curr_to = curr_to->next();
    }

    if (curr_to == NULL) {
      // The rest of the from list should be added as tail
      _tail->set_next(curr_from);
      curr_from->set_prev(_tail);
      curr_from = NULL;
    } else {
      HeapRegion* next_from = curr_from->next();

      curr_from->set_next(curr_to);
      curr_from->set_prev(curr_to->prev());
      if (curr_to->prev() == NULL) {
        _head = curr_from;
      } else {
        curr_to->prev()->set_next(curr_from);
      }
      curr_to->set_prev(curr_from);

      curr_from = next_from;
    }
  }

  if (_tail->hrs_index() < from_list->_tail->hrs_index()) {
    _tail = from_list->_tail;
  }

  _count.increment(from_list->length(), from_list->total_capacity_bytes());
  from_list->clear();

  verify_optional();
  from_list->verify_optional();
}