Please send questions to
st10@humboldt.edu .
/************************************************************/
/* ADAPTED FROM: */
/* http://www.cs.colorado.edu/~main/chapter4/node1.cxx */
/* */
/* Main and Savitch, "Data Structures and Other Objects */
/* using C++", 2nd edition, Addison-Wesley, Ch.5 */
/************************************************************/
//---------------------------------------------------------------------
// File: node.cpp
// Name: Michael Main, Walter Savitch
// (adapted by Sharon M. Tuttle)
// last modified: 3-10-05
//
// Class name: node
//
// Purpose: serves as the unit from which a singly-linked list
// can be built.
//
// DYNAMIC MEMORY USAGE by the node class:
// If there is insufficient dynamic memory, then the
// following throw bad_alloc:
// the constructors,
//
// NOTE:
// Some of the methods have return value that is
// a pointer to a node. Each of these comes in TWO
// versions: a non-const version (where the return value is
// node*) and a const version (where the return value is
// const node*).
//---------------------------------------------------------------------
#include <cassert>
#include "node.h"
using namespace std;
/*****************************************************/
/* CONSTRUCTORS and DESTRUCTOR */
/*****************************************************/
//---------------------------------------------------------------------
// postcondition: creates a node with data_field obtained from
// the default constructor of the value_type, and
// next_field set to NULL.
//
// NOTE: In the ANSI/ISO standard, this default-constructor
// notation is also allowed for the built-in types,
// providing a default value of zero.
//
node::node( )
{
data_field = value_type();
next_field = NULL;
}
//---------------------------------------------------------------------
// postcondition: creates a node with data_field set to
// init_data, and next_field set to NULL.
//
node::node(const value_type& init_data)
{
data_field = init_data;
next_field = NULL;
}
//---------------------------------------------------------------------
// postcondition: create a node with data_field obtained from
// the default constructor of the value_type, and
// next_field set to init_next.
//
node::node(node* init_next)
{
data_field = value_type();
next_field = init_next;
}
//---------------------------------------------------------------------
// postcondition: create a node with data_field set to
// init_data, and next_field set to init_next.
//
node::node(const value_type& init_data, node* init_next)
{
data_field = init_data;
next_field = init_next;
}
/*************************************************************/
/* ACCESSORS and other constant member functions (observers) */
/*************************************************************/
//---------------------------------------------------------------------
// postcondition: returns the data from this node
//
node::value_type node::get_data( ) const
{
return data_field;
}
//---------------------------------------------------------------------
// postcondition: returns the next pointer from this node.
// (See note above --- need both a const and non-const
// version of this, because it returns a pointer.
// See also pp. 219-221 of Savitch and Main, 3rd edition.)
//
const node* node::get_next( ) const
{
return next_field;
}
node* node::get_next( )
{
return next_field;
}
/*****************************************************/
/* MODIFIERS and other modifying member functions */
/*****************************************************/
//---------------------------------------------------------------------
// postcondition: the node now contains the specified
// new_data.
//
void node::set_data(const value_type& new_data)
{
data_field = new_data;
}
//---------------------------------------------------------------------
// postcondition: the node now contains the specified
// new_next pointer.
//
void node::set_next(node* new_next)
{
next_field = new_next;
}