Lab 2, CS 312 Algorithm Analysis and Design Purpose: Review of inheritance and polymorphism, Templates I. Implement the classes defined in List.h, shown below: ------ List. h --------------------------------------------------------- #include #include #include #include #include "Member.h" #ifndef LIST_H #define LIST_H // ----- Node Interface ------------------------------------------------- template class Node { public: T * info; // Make a list to hold mixed objects Node * next, *prev; Node(T * p = 0); // let info point at p. ~Node(); // delete object(s) pointed by non-null pointer(s). char * toString( bool lab = true ); // call T::toString(). }; // ------ Doubly-linked List Interface ----------------------------------- // The general list interface. The general list serve as base of other // linked list structures such as dequeue, stack, sorted queue. // The interface and its implementation will have all necessary data and // function members. To maximize its use, the general list and its drived // classes are implemented as templates. //------------------------------------------------------------------------ template class List { public: // Define an inner class called iterator, which can be used to go through the // items in the linked list just like using an index to go through elements in // an array. The inner class of a base class will be inherited by derived class. // The inner class of a class is static implicitly and cannot be referred through // an instance of the outer class. // Notice that inner class cannot see members of containing class in C++. // In Java, inner class can see the members of containing class in Java. class Iterator { protected: Node * cur; bool forward; // true: from front to rear, false: from rear to front. public: Iterator (Node * start, bool fwd); bool hasNext(); T & next(); // Called by getIterator() and setIterator to set initial node // and the direction for cur to move. void setDirection(Node* start, bool fwd); }; protected: Node * front, * rear; int count; Iterator * it; // Set it to null in List(). Set it once. // Add and remove node at front or rear. void addToFront(T *p); // pass me ptr to info, I will get node. void addToRear(T *p); // pass me ptr to info, I will get node. T * removeFront(); T * removeRear(); public: List(); virtual ~List(); T * getFront(); // peek element at front. inline bool isEmpty(); inline bool isFulll(); virtual void add(T *p); void clear(); // Remove each of all node and objects pointed by info. inline int getCount(); inline Iterator & iterator(); inline void setIterator(bool fwd = true); }; // -------- Stack Interface ------------------------- // An Last In First Out Queue; push() and pop() are // common names used. //--------------------------------------------------- template class Stack: public List { public: Stack(); ~Stack(); virtual void add(T * p) ; // call push(p); for polymorphism. void push( T * p); // to allow without new T. T * pop( ); // to allow i = pop(); }; // -------- Queue Interface ------------------------- // An First In and First Out Queue. To common // functions are enqueue() and dequeue(). //--------------------------------------------------- template class Queue: public List { public: Queue(); ~Queue(); virtual void add( T * p); // call enqueue(); for polymorphism void enqueue( T * p); T * dequeue( ); }; // ------ Sorted/Priority Queue Interface ---------- // The common methods include add() and delete() //--------------------------------------------------- // Sorted queue will use Comparator class to sort the queue. // Extend the header file for community members with the following: // a abstract class called Comparator with pure virtual function // int compare( Member * c1, Member *c2 ); // two classes implementing Comparator class: // class Comparator1: public Comparator // which will compare two members' SSN // class Comparator2: public Comparator // which will compare two members' last name and first name if necessary. template class SortedQueue: public List { protected: Comparator * pc; public: SortedQueue( Comparator * c ); ~SortedQueue(); virtual void add(T * p); T * remove( ); }; #endif ---- End of List.h ----------------------------------------------------- II. Necessary data structures. Assume that Member is the class name for your base community member class. Define a array of pointers to List: List *l[] = { new Stack, new Queue, new SortedQueue(comparator1), new SortedQueue<(comarator2) }; III. Write a program which 1. displays the following line: Enter a command (?/h/H for help, q/Q to exit) : 2. displays the following menu when ?, h or H is entered: +------------- 350 Assignment 2 --------------+ | G/g: Generate 50 mixed community members, | | added them to each of four list | | structures, with each member object | | pointed by 4 nodes from each of 4 list.| | The first list is a stack. The second | | one is FIFO queue. The third one is a | | queue sorted by SSN in ascending order.| | The last one is queue sorted by last | | and first name in ascending order. | | 1 : print out member object in stack using | | iterator, 20 object per screen, one | | object per line with sequence number | | and object type. Allow user to quit or | | continue listing after each 20. | | 2 : print out members in FIFO queue with | | same requirement as 1 (itertor ...). | | 3 : print out members in sorted queue 1 | | from the front to the rear of the queue| | with iterator, 20 per screen, and etc. | | 4 : print out members in sorted queue 2 | | from the rear to the front with | | iterator, 20 per screen, and etc. | +---------------------------------------------+ | ?/h/H: display this help menu. | | q/Q: Exit program. | +---------------------------------------------+