#ifndef __LIST_H
#define __LIST_H
//
// LinkedList.h
//
#include <string>
using namespace std;
/////////// Class LinkedList ///////////////
class LinkedList
{
class Link; // Forward reference
public:
LinkedList(); // Constructor
~LinkedList(); // Destructor
// Access
Link *GetFirst() const; // Returns ptr to first item in list
Link *GetLast() const; // Returns ptr to last item in list
int GetCount() const; // Returns count of items in list
// Operations
void Append(Link &item); // Appends an item to the end of the list
void Purge(); // Removes all items from the list
////////// A public nested Iterator class ////////
class Iterator
{
public:
Iterator(const LinkedList &list); // Constructor
~Iterator(); // Destructor
// Access
const LinkedList *GetList() const; // Return ptr to the iterator's list
Link *GetCurrent() const; // Return ptr to current list item
Link *GetNext(); // Move to next item and return ptr to it
Link *GetPrevious(); // Move to previous item and return ptr to it
bool AtFirst() const; // return true if current item is first item
bool AtLast() const; // return true if current item is last item
private:
// Disable copy constructor for Iterator
Iterator(const Iterator &);
// Disable assignment for Iterator
Iterator &operator=(const Iterator &);
// Data
const LinkedList *m_list; // The list we are iterating through
Link *m_current; // Points to the current item in the list
};
private:
// Disable copy constructor for LinkedList
LinkedList(const LinkedList &);
// Disable assignment for LinkedList
LinkedList &operator=(const LinkedList &);
// Data members
Link *m_first; // Pointer to the first list item
Link *m_last; // Pointer to the last list item
int m_count; // Count of list items currently in list
////////// A private nested Link class //////////////
class Link
{
public:
Link(void *data); // Constructor (associates with data)
~Link(); // Destructor
// Operations
void Add(LinkedList &list, Link *prevItem);
// Add this Link to the specified list
// following the specified prevItem
// (if prevItem is NULL, place at front of list)
void Remove(); // Remove this list item from the list it is in
// Access member functions
Link *GetNext() const; // Return ptr to next item in list
Link *GetPrevious() const; // Return ptr to previous item in list
const LinkedList *GetList() const; // Return ptr to the list item's list
void *GetData(); // Returns ptr to data for item
private:
// Disable copy constructor for Link
Link(const Link &);
// Disable assignment for Link
Link &operator=(const Link &);
// Data members
Link *m_next; // Pointer to next item in the list
Link *m_prev; // Pointer to previous item in the list
LinkedList *m_list; // Pointer to the list the item currently is in
void *m_data; // Pointer to data
};
};
#endif // __LIST_H
|