CS210 Lab: STL Lists Prelab Answers


Prelab Answers:

  1. Given the following template class for linked lists:
    template <class T>
     class LinkedList; 
    
     template <class T>
     class ListElement 
     { 
           T datum; 
           ListElement* next; 
           ListElement (T const&, ListElement*); 
    
           friend LinkedList <T>;
     }; 
    
     template <class T>
     class LinkedList 
     { 
           ListElement<T>* head; 
     public: 
           LinkedList (); 
           ~LinkedList (); 
           bool IsEmpty () const; 
           T const& First () const; 
           T const& Last () const; 
           void Prepend (T const&); 
           void Append (T const&); 
           void Extract (T const&); 
           void Print (); 
     }; 
    		
    How would you declare a linked list of
    1. integers

    2. LinkedList<int> mylist
    3. characters

    4. LinkedList<char> mylist
    5. floats

    6. LinkedList<float> mylist
  2. If you were given the following data type:
    struct DataType 
    {
        int position;              // (Key) Packet's position w/in message
        char body[packetSize];     // Characters in the packet
        int getKey () const
            { return position; }   // Returns the key field
    };
    
    
    
    and had declared an object: DataType currPacket
    Could you have the following statement:
    cout << currPacket;
    If not, how could you fix this statement?
    No, you cannot do that, you will end up with a compiler error.
    To fix it, you have a couple of options:
    1. write instead
      cout << currPacket.body;
      
    2. overload the << operator with the following code:
      ostream& operator<<(ostream& io, DataType curr)
      {
      	io << curr.body;
      	return io;
      }
      

    Back to Exercise click here
    Back to STL Lists Lab click here

    CS Dept Home Page
    CS Dept Class Files
    CS210 Class Files

    Copyright: Department of Computer Science, University of Regina.