﻿ Hmwk 5

## Homework 5

Modify the list class you implemented in Lab 5 by adding the following class members:

```//-----------------------------------------------------
// remove
//
// Remove the first element equal to the specified item.
// Equality is based on the result returned by the ==
// operator applied to the items in the list. If there
// is no element equal to the specified item then this
// method does nothing (the list is unchanged).
//
// In Parameter: item
//-----------------------------------------------------
void remove(const itemType& item)

//-----------------------------------------------------
// sort
//
// Sort the values in ascending order based on the
// result returned by the < operator applied to the
// items in the list. The selection sort algorithm
// is used.
//-----------------------------------------------------
void sort()

//-----------------------------------------------------
// find
//
// Return a pointer to the first element in the list
// that is equal to the specified item based on the ==
// operator applied to the items in the list. If there
// is no element equal to the item, return end().
//
// In Parameter: item
//-----------------------------------------------------
itemType* find(const itemType& item)

//-----------------------------------------------------
// insertInOrder
//
// Insert the specified item into the list in sort
// order (based on the results of the < operator).
//
// Precondition: the list is in sort order prior to the
// invocation of this function.
//
// Postcondition: the list is in sort order following
// the insertion of the specified item.
//
// In Parameter: item
//--------------------------------------------------------
void insertInOrder(const itemType& item)
```

Notice that the sort and the insertInOrder functions can be used only if the less than operator (<) is defined for the items in the list. Similarly, the remove and the find functions can  be used only if the equality operator (= =) is defined for the items in the list. That is, given a list of items, these functions can be invoked only if these operators are defined for those items. This is a limitation of the list class.

For example, suppose we wanted a list of temperature records where each record consists of the low and high temperature for a given day. It might be obvious how one might define equality for two records (the low is the same in both records and the high is the same in both records). It is not at all clear how you might define less than. If less than is left undefined for two temperature records, you could still create a list of temperature records but you couldn't sort the list or insert a new record in sort order because neither one of those concepts makes any sense if you can't determine whether one record is less than another or not.

Let me emphasize that this is an inherent limitation of the list class and as the author of the list class code, you do not have to worry about defining equality or "less than" for the items in the list. It is the responsibility of the application programmer to define equality for the items in a list if the programmer wants to remove a specific item from the list or find a particular item. Similarly, it is the responsibility of the application programmer to define "less than" if the programmer wants to sort the list or insert items in order.

I would suggest you modify the test program from Lab 5 to test these newly added features.

Name your modified list class file, "Hmwk05_list.cpp" and send it to me as an email attachment. You do not need to send me your modified test program as I will have one of my own.