Please send questions to
st10@humboldt.edu .
/************************************************************/
/* ADAPTED FROM: */
/* http://www.cs.colorado.edu/~main/chapter4/node1.h */
/* */
/* Main and Savitch, "Data Structures and Other Objects */
/* using C++", 2nd edition, Addison-Wesley, Ch.5, */
/* p. 210 (mostly) */
/************************************************************/
//---------------------------------------------------------------------
// File: node.h
// Name: Michael Main, Walter Savitch
// (adapted by Sharon M. Tuttle)
// last modified: 4-20-05 (also includes linked-list toolkit functions)
//
// Template class name: node
//
// Description: 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*).
//---------------------------------------------------------------------
#ifndef NODE_H
#define NODE_H
#include <cstdlib> // provides size_t and NULL
using namespace std;
template <typename Item>
class node
{
public:
/****************************************************/
/* TYPEDEFS and MEMBER CONSTANTS */
/****************************************************/
// node::value_type is the data type of the data_field
// of the node. It may be of any of the C++ built-in
// types (int, char, etc.), or a class with a
// default constructor, a copy constructor, an
// assignment operator, and a test for equality
// (x == y).
//
// (CHANGE to desired type...)
//
typedef Item value_type;
/*****************************************************/
/* 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.
//
node( );
// postcondition: creates a node with data_field set to
// init_data, and next_field set to NULL.
//
node(const value_type& init_data);
// 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* init_next);
// postcondition: create a node with data_field set to
// init_data, and next_field set to init_next.
//
node(const value_type& init_data, node* init_next);
/*************************************************************/
/* ACCESSORS and other constant member functions (observers) */
/*************************************************************/
// postcondition: returns the data from this node
//
value_type get_data( ) const;
// 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* get_next( ) const;
node* get_next( );
/*****************************************************/
/* MODIFIERS and other modifying member functions */
/*****************************************************/
// postcondition: the node now contains the specified
// new_data.
//
void set_data(const value_type& new_data);
// postcondition: the node now contains the specified
// new_next pointer.
//
void set_next(node* new_next);
private:
/*****************************************************/
/* DATA FIELDS */
/*****************************************************/
value_type data_field;
node* next_field;
};
/*****************************************************/
/* LINKED LIST "TOOLKIT" FUNCTIONS */
/* */
/* functions to manipulate a linked list */
/*****************************************************/
// list_clear
//
// precondition: head_ptr is the head pointer of a linked list.
// postconditions: All nodes of the list have been returned
// to the heap, and the head_ptr is now NULL.
// (thus, the need for head_ptr to be passed BY REFERENCE...)
//
template <typename Item>
void list_clear(node<Item>*& head_ptr);
// list_copy
//
// precondition: source_ptr is the head pointer of a linked list.
// postconditions: head_ptr and tail_ptr are the head and tail
// pointers for a new list that contains the same items as the
// list pointed to by source_ptr. The original list is unaltered.
//
template <typename Item>
void list_copy
(const node<Item>* source_ptr, node<Item>*& head_ptr, node<Item>*& tail_ptr);
// list_head_insert
//
// precondition: head_ptr is the head pointer of a linked list.
// postconditions: A new node containing the given entry has been
// added at the head of the linked list; head_ptr now points to
// the head of the new, longer linked list.
//
template <typename Item>
void list_head_insert(node<Item>*& head_ptr, const Item& entry);
// list_head_remove
//
// preconditions: head_ptr is the head pointer of a linked list,
// with at least one node.
// postconditions: The head node has been removed and returned to
// the heap; head_ptr is now the head pointer of the new, shorter
// linked list.
//
template <typename Item>
void list_head_remove(node<Item>*& head_ptr);
// list_insert
//
// precondition: previous_ptr points to a node in a linked list.
// postconditions: A new node containing the given entry has been
// added after the node that previous_ptr points to.
//
template <typename Item>
void list_insert(node<Item>* previous_ptr, const Item& entry);
// list_length
//
// precondition: head_ptr is the head pointer of a linked list.
// postconditions: The value returned is the number of nodes in
// the linked list.
//
template <typename Item>
std::size_t list_length(const node<Item>* head_ptr);
#include "node.template"
#endif