------------------------------------------------------------------------------
MC logo
Linked List Node Classes
[^] CSc 220 Home
------------------------------------------------------------------------------
[Introduction] [Boolean and Control] [Functions] [Arrays and Pointers] [Dynamic Arrays] [Array Errors] [Command Array] [Standard Containers] [Structs and Classes] [Automatic Pointer] [Multi-File Programs] [Copying Objects] [Templates] [Inheritance] [Plain C I/O] [Type Odds And Ends] [Plain C Strings] [File I/O]
[Bounded Safe Array] [Bounded Safe Array Driver] [PrintMe Class] [PrintMe2 Class] [PrintMeLots Class] [PrintMeLots2 Class] [PrintMe Test Driver] [Linked List Node Classes] [Linked List Classes] [Linked List Implementation] [Linked List Driver] [Expression Calculator]
linkednode.h
#ifndef _NODE_
#define _NODE_

#include <iostream>

using namespace std;

/*
 * This contains the objects which describe nodes for single and double linked
 * list objects.  (List objects defined in list.h.)
 */

// Node with one next for a single-linked list.
class OneWayNode
{
    protected:
        int cont;                       // Denotes contents of node.
        OneWayNode *next;               // Next node in linked list.

    private:
        friend class OneWayList;        // User of the private calls.

        // This is used to destroy the list.  Just call it on the head
        // node, if any.
        void Die()
        {
                if(next != NULL)
                        next->Die();
                delete this;
        }

        // This is used to print the list members.
        void Print(ostream &st)
        {
                st << cont;
                if(next != NULL)
                {
                        st << ',';
                        next->Print(st);
                }
        }

    public:
        // Construct, with given contents.
        OneWayNode(int i, OneWayNode *n = NULL)
        {
                cont = i;
                next = n;
        }

        // Manipulate contents.
        int Cont() const { return cont; }
        void Cont(int i) { cont = i; }

        // Manipulate the link.
        OneWayNode* Next() const { return next; }
        void Next(OneWayNode *n) { next = n; }
};

// Node with two links for a double-linked list.
class TwoWayNode: public OneWayNode
{
    protected:
        TwoWayNode *prev;                       // Point to previous node.
    public:
        // Build me one with the specified contents.
        TwoWayNode(int i, TwoWayNode *pv = NULL, TwoWayNode *nx = NULL):
                                OneWayNode(i, nx)
        {
                prev = pv;
        }

        // Also, manipulate the previous pointer.
        TwoWayNode* Prev() const { return prev; }
        void Prev(TwoWayNode *n) { prev = n; }
};

#endif