// Computer Science 367, Section 3, Fall 1997 // Instructor: Michael Siff (siff@cs.wisc.edu) // // Programming Assignment One: Solution // // // FILE: list.h // // // TEMPLATE CLASS PROVIDED: List (a container template class // for a list of ItemTypes) // // The template parameter, ItemType, is the data type of the items in // the List. It may be any of the C++ built-in types (int, char, etc.), or // a class with a default constructor, a copy constructor, an assignment // operator, and operators to test for equality (x==y) and inequality (x!=y). // // // MEMBER CONSTANTS // static const size_t DEFAULT_CAPACITY = _ // List::DEFAULT_CAPACITY is the capacity of a List // created by the default constructor. // // // CONSTRUCTORS for the List template class: // List(size_t cap = DEFAULT_CAPACITY) // Postcondition: The list has been initialized as an empty list with a // capacity given by the parameter cap. // The cursor is at the end of the list (which is, for an empty list, the // same as the front of the list). // // List(const List& source) // Postcondition: The list has been initialized as a complete copy of // source. (This is the copy constructor.) // // // DESTRUCTOR for the List template class: // ~List() // Postcondition: The memory allocated to store the list has been freed. // // // MODIFICATION MEMBER FUNCTIONS for the List template class: // void insert(const ItemType& entry) // Precondition: The list is not full. // Postcondition: The entry has been added to the front of the // list. If the list was empty the cursor is now at the front of the // list, otherwise it is unchanged. // // void append(const ItemType& entry) // Precondition: The list is not full. // Postcondition: The entry has been added to the end of the // list. If the list was empty then the cursor is now at the front of // the list, otherwise it is unchanged. // // void addBeforeCurrent(const ItemType& entry) // Precondition: The list is not full. // Postcondition: If the cursor is at the end then the entry is // appended to the end of the list. Otherwise, the entry has been // inserted before the cursor. // // void addAfterCurrent(const ItemType& entry) // Precondition: The list is not full and the cursor is not at the end // of the list. // Postcondition: The entry has been inserted after the list cursor. // // void remove() // Precondition: The list cursor is not at the end of the list. // Postcondition: The current item is removed from the list. The cursor // is now at next item or, if there is no next item, the end of the list. // // void reset() // Postcondition: If the list is not empty, the cursor is now at the // first item on the list. Otherwise, no change. // // void atEnd() // Postcondition: The cursor now points to the end of the list. // // void advance() // Precondition: The list cursor is not at the end of the list. // Postcondition: If the cursor is at the last item on the list, // then the cursor no longer points to any item (it's at the end of the // list). Otherwise, the cursor points to the next item on the list. // // void back() // Precondition: The list cursor is not at the front of the list. // Postcondition: If the cursor is at the end of the list (not pointing // to any item), then the cursor points to the last item on the // list. Otherwise, the cursor points to the previous item on the list. // // void find(const ItemType& item) // Postcondition: If the item occurs in the list, the cursor is now at // the first occurrence of the item at or after the previous cursor // location. If the item is not at the list, the cursor is at the end. // // void operator=(const List& source) // Postcondition: The list that activated this function is now a // complete copy of source. The memory needed to store the former // contents of the activating list is now freed. // // // CONSTANT MEMBER FUNCTIONS for the List template class: // size_t length() const // Postcondition: The return value is the total number of items in the list. // // bool isEmpty() const // Postcondition: True if the list is empty, false otherwise. // // bool isFull() const // Postcondition: True if the list is full, false otherwise. // // bool isFront() const // Postcondition: True if the cursor is at the front of the list, false // otherwise. // // bool isEnd() const // Postcondition: True if the cursor is at the end of the list // (i.e. not pointing to any item), false otherwise. // // bool isLast() const // Postcondition: True if the cursor is at the last item of the list // (not the end), false otherwise. If the list is empty this is false. // // bool isMember(const ListItem& item) const // Postcondition: True if item occurs in the list, false otherwise. // // ItemType current() const // Precondition: The list cursor is not at the end of the list. // Postcondition: The string returned is the current item. // // bool operator==(const List& operand) const // Postcondition: True if this list and the list operand are // equivalent: they have the same items in the same order. Lists can be // equiavlent even if they have different capacities. #ifndef LINKED_LIST_TEMPLATE_H #define LINKED_LIST_TEMPLATE_H #include // Provides size_t, NULL // auxiliary class for nodes of the list template class ListItem { friend class List; private: ItemType val; ListItem *next; ListItem *prev; ListItem(){next=prev=NULL;} }; template class List { public: // MEMBER CONSTANTS static const size_t DEFAULT_CAPACITY = 100; // CONSTRUCTORS List(size_t cap = DEFAULT_CAPACITY); List(const List& source); // copy constructor // DESTRUCTOR ~List(); // MODIFICATION MEMBER FUNCTIONS void insert(const ItemType& entry); void append(const ItemType& entry); void addBeforeCurrent(const ItemType& entry); void addAfterCurrent(const ItemType& entry); void remove(); void reset(); void atEnd(); void advance(); void back(); void find(const ItemType& item); void operator=(const List& source); // CONSTANT MEMBER FUNCTIONS size_t length() const; bool isEmpty() const; bool isFull() const; bool isFront() const; bool isEnd() const; bool isLast() const; bool isMember(const ItemType& item) const; ItemType current() const; bool operator==(const List& operand) const; private: // PRIVATE MEMBER FUNCTIONS (utilities) void deleteList(); void copyList(const List& source); // MEMBER VARIABLES size_t capacity; size_t count; // number of items on the list ListItem *head; // pointer to the first item on the list ListItem *tail; // pointer to the last item on the list ListItem *location; // the cursor }; #include "list.C" // the template implementation #endif