COSC 2415 - Data Structures
Bob Comer, Professor of Computer Studies


Program 4 - Linked-list Processing

Be sure to read through Chapter 6 of the textbook before starting this assignment. In particular, you need to study section 6.5.

Your assignment is to build a complete List class using the linked-list implementation as described in section 6.5 of your textbook.

I have included the header file for the List class below. You must include implementations for all the functions listed in the header file (see exceptions in next paragraph). If you like, you can add private (utility) member functions.

Extra Credit - The copy constructor and overloaded assignment operator are optional. You can earn up to 4% extra credit for including a correct and tested copy constructor. You can earn another 4% extra credit for including a correct and tested overloaded assignment operator.

Write a driver program to test your functions for a linked list of strings. Use the driver program from Program 3 as a starting point.

Your project should be divided into separate files - a class definition file, a class implementation file, and the client (driver) program.

 
// List.h

#ifndef LIST
#define LIST

#include <iostream>
#include <cstdlib>
#include <string>

typedef string ElementType;

class List
{
private:
   class Node
   {
   public:
      ElementType data;
      Node * next;
      
      Node( )
      : data( ElementType( ) ), next( NULL )
      { }
              
      Node( ElementType initData )
      : data( initData ), next( NULL )
      { }
   }; // end of Node class
           
   typedef Node * NodePointer;
           
public:
   List( );
   /* Construct a List object

      Precondition: none.
      Postcondition: An empty List object has been constructed.
   */
              
   List( const List &source );
   /* Construct a copy of a List object.

      Precondition: None. 
      Postcondition: A copy of source has been constructed.
   */
              
   ~List( );
   /* Destroys a List object.

     Precondition:  None.
     Postcondition: All memory allocated to the List object has been freed.
   */
              
   const List & operator=( const List &rightSide );
   /* Assign a copy of a List object to the current object.

      Precondition: none 
      Postcondition: A copy of rightside has been assigned to this
         object. A constant reference to this list is returned.
   */

   int getSize( ) const;
   /* Returns the size of the list (number of items in the list)

      Precondition: none 
      Postcondition: The return value is the number of items in the list.
   */

   bool empty( ) const;
   /* Check if this List is empty

      Precondition: none 
      Postcondition: The return value is true if this List object is empty;
         otherwise the return value is false.
   */
               
   void insert( ElementType dataVal, int index );
   /* Insert a value into this List at a given index

      Precondition:  The index is valid (0 <= index <= the list size). 
         The first position is index 0, the second position is index 1, etc. 
      Postcondition: dataval has been inserted into the list at the position
         determined by index (provided there is room and index is a legal
         position).
   */         

   void erase( int index );
   /* Remove the value from this List at a given index.

      Precondition:  The list is not empty and index is valid 
         (0 <= index < the list size).
      Postcondition: the element at position index has been
         removed (provided index is a legal position).
   */
         
   void display( ostream &out ) const;
   /* Display the contents of this List

      Precondition:  ostream out is open
      Postcondition: the items in this List have been output to stream out
   */ 


   int find( ElementType value) const;
   /* Find the first occurrence of a value in this List

      Preconditions:  None
      Postconditions: The return value is the index of the first List item 
         that matches value. The first list item has index 0, the second has 
         index 1, etc. The return value is -1 if value is not found in the list. 
   */

              
private:
   NodePointer first;
   int mySize;
}; // end of List class
      
#endif          

Hints/Suggestions:

  1. Write and test just one or a few functions at a time. For example, you may want to start with the default constructor, the display function and the insert function.
  2. Be sure that you understand the code fragments given in section 6.5 of your book.
  3. The textbook includes the following hint. This List class has many similarities to the linked-list implementation for the Stack class in Chapter 7. If you are having trouble getting started on the assignment operator or copy constructor, you might want to look at the code for the linked-list implementation for the Stack.


Return to Data Structures homepage 

Return to ACC Homepage

URL: http://www.austincc.edu/comer/cosc 2436/
Copyright: © 2015 by the Austin Community College
Department of Computer Studies. All rights reserved.
Comments to: Bob Comer
Last updated: September 1, 2015

Austin Community College is an equal opportunity educator and employer.