//******* List.h ***************************************************** // // Derek Harter // dharter@online.tamu-commerce.edu // Assignment #1 - Linked Lists // June 16, 2005 // KDevelop Integrated Development Environment & gnu g++ compiler // // This header file defines the data type List for holding the // elements of a basic list type. This class implements and fills // out the template of the List ADT given in Nyhoff "ADTs, Data // Structures and Problem Solving with C++ 2ed", ch 6.5 p. 296. // Basic operations are: // empty: To determine if the list is currently empty or not. // traverse: To traverse and display the elements currently // in the list. // insert: To add an item into the list. // delete: To remove an item from the list. //******************************************************************* #ifndef LIST #define LIST #include using namespace std; typedef int ElementType; // change this for List holding different types class List { public: // Construct a List (default). // // Precondition: None. // Postcondition: The List is initialized and initially has no // elements. List(); // Destruct a List, free up any dynamically allocated memory // used by the List object. // // Precondition: None. // Postcondition: All memory used by the List object is // successfully returned to the OS for reuse. ~List(); // Provides information about the current state of the List, // whether or not the List is empty. // // Precondition: None. // Postcondition: Returns boolean value true if the List is // empty (contains no items) false otherwise. bool empty() const; // Traverse the list displaying all of the items currently in // the list on the provided output stream. // // Precondition: The ostream out is open. // Postcondition: The items in the List have been inserted into // ostream out. void traverse(ostream& out) const; // Insert the indicated data item into my List of items. // // Precondition: first is pointing to the first Node in // my linked List (or is NULL if the list is empty). // Postcondition: A new Node is dynamically created and // inserted onto the front of my linked List. The // data item of the new Node contains the value that // was asked to be inserted. mySize is updated by 1 // to indicate the new size of the list after item // was inserted. void insert(ElementType data); // Delete the indicated data element from my List // // Precondition: first is pointing to the first Node in // my linked List (or is NULL if the list is empty). // Postcondition: If a Node is found containing the // indicated data item, it will be removed from my // linked list and its memory will be freed. mySize // is decremented by 1 for each item that is deleted // from my List. void del(ElementType data); private: // Node class is a private class of List that is used to // implement a pointer-based linked list data structure. class Node { public: ElementType data; Node* next; }; typedef Node* NodePointer; NodePointer first; // points to the head of the list int mySize; // number of items/Nodes in list }; #endif // LIST