Module 5. Linked Lists, Stacks, Queues Exercises

1.  Complete the ZooAnimal constructor function.  It should insert
the new ZooAnimal class object in the linked list at the end after the
object currently pointed to by the static data member current.  Make
the nextAnml pointer of the current object point to the object being
constructed.  Be sure to update the pointer current.

   class ZooAnimal  
   {
    private:
      char *name;
      ZooAnimal* prevAnml;                // previous link in the list
      ZooAnimal* nextAnml;                // next link in the list
    public:
      static ZooAnimal *start;            // first element of linked list
      static ZooAnimal *current;          // last element of linked list
    public:
      ZooAnimal (char*);                  // constructor function
      ~ZooAnimal ();                      // destructor function
      char* reptName () { return name; }
      ZooAnimal* prevAnimal ();
   };

   ZooAnimal* ZooAnimal::start = NULL;
   ZooAnimal* ZooAnimal::current = NULL;

   ZooAnimal::ZooAnimal (char* theName)
   {
      char *name = new char[20];
      strcpy(name, theName);
   }


2.  The destructor function is called to destroy the last item in the
linked list.  The pointer to the object being destroyed is the this
pointer.  The destructor function updates the static data member
current.  Write the destructor function.  Make current the same as the
prevAnml pointer of the object being destroyed.

   class ZooAnimal  
   {
    private:
      char *name;
      ZooAnimal* prevAnml;                // previous link in the list
      ZooAnimal* nextAnml;                // next link in the list
    public:
      static ZooAnimal *start;            // first element of linked list
      static ZooAnimal *current;          // last element of linked list
    public:
      ZooAnimal (char*);                  // constructor function
      ~ZooAnimal ();                      // destructor function
      char* reptName () { return name; }
      ZooAnimal* prevAnimal ();
   };

   ZooAnimal* ZooAnimal::start = NULL;
   ZooAnimal* ZooAnimal::current = NULL;


3.  Modify the cout statement below so that it invokes the ZooAnimal
member function reptName to report the name of the creature object
denoted by subscript whichOne.

   class ZooAnimal  
   {
    private:
      char *name;
      int cageNumber;
      int weightDate;
      int weight;
    public:
      ZooAnimal (char*, int, int, int); // constructor function
      ~ZooAnimal (); // destructor function
      void changeWeight (int pounds);
      char* reptName ();
      int reptWeight ();
      int daysSinceLastWeighed (int today);
   };

   // ========== an application to use the ZooAnimal class
   void main ()
   {
    ZooAnimal creature [200];
    int whichOne = 173;

    cout << "This animal's name is " << ________________________ << endl;
   }