CS 311 Spring 2008  >  Assignment 6

CS 311 Spring 2008
Assignment 6

Assignment 6 is due at 5 p.m. Thursday, April 17. It is worth 20 points.

Procedures

E-mail answers to the exercises below to ffggc@uaf.edu, using the subjectDA6”.

Exercises (20 pts total)

Exercise A — Linked List Class Template

Purpose

In this exercise, you will write a Linked List. You will also write a function to reverse a Linked List.

Your Linked List will be used in the next exercise.

Instructions

Note: Restrictions on extra public members are lifted, for this exercise. You may include public members in your classes, other than those specifically mentioned below. Because of compiler troubles, I recommend that you do not declare any friends in this exercise; if some data or functionality needs to be accessible to some other class, instead of making a friend, simply make the appropriate members public.

Implement a C++ class template that manages a Linked List. The type of item in the list should be specified by the client. Be sure to follow the coding standards. All standards now apply!

Exercise B — Templated Stack

Purpose

In this exercise, you will write a Stack that uses the Linked List from Exercise A to hold its data.

Instructions

Implement a C++ class template that manages a Stack. The type of item in the Stack should be specified by the client. Be sure to follow the coding standards. All standards now apply!

Notes

About Iterators

Some facts:

Thus, SList::read and SList::write will look something like this.

template <typename T>
class SList {

[ A LOT IS LEFT OUT HERE ]

public:
    template <typename InputIterator>
    void read(InputIterator first, InputIterator last)
    {
        [ CLEAR THE ITEMS IN THE LIST HERE ]
        while (first != last)
        {
            value_type val = *first++;
            [ PUT val INTO THE LIST HERE ]
        }
    }

    template <typename OutputIterator>
    void write(OutputIterator dest) const
    {
        for ( [ ??? ] ) // ITERATE THROUGH LINKED LIST, SOMEHOW)
        {
            value_type val;
            [ HERE, PUT NEXT LIST ITEM INTO VAL ]
            *dest++ = val;
        }
    }
};

About Friendship

Many C++ compilers do not correctly support friends that are templates. Therefore, I strongly suggest that you avoid declaring friends in this assignment.

If you need to give a global function or class access to some other class’s private members, you may (if you wish) make those members public. (Thus, the usual requirements for the proper use of “private” are loosened, for this assignment.

Test Program & Grading

I have written a single test program for both exercises: da6_test.cpp. If you compile and run your package with this program (unmodified!), then it will test whether your code works properly.

Do not turn in da6_test.cpp.

Because of the single test program, the code for both exercises must at least exist, and must compile with the test program. Code that does not compile at all will not be graded.

Other Thoughts

If you do a good job on Exercise A (the Linked List), then Exercise B (the Stack) should be very easy. In particular, if the Linked List knows how to initialize, copy, and destroy itself properly, then the compiler-written default ctor and Big-Three functions should be fine, for the Stack class. That leaves empty, top, push, and pop, none of which is likely to be very difficult.

For the Linked List class, remember the swap trick for writing copy assignment. Also remember that it is probably easier to put necessary functionality into class SList than to have the Stack manage the internal details of the Linked List.


CS 311 Spring 2008: Assignment 6 / Updated: 12 Apr 2008 / Glenn G. Chappell / ffggc@uaf.edu Valid HTML 4.01!