(Latest Revision: 11/22/2000)
11/22/00: Modified specs to "Print" slightly

Lists: Implementing a Deque


PRELIMINARIES:

You need to use material from chapters six and seven of Carrano for this assignment. You also need to read the directions and examples here: http://shalim.csustan.edu/~john/Classes/General_Info/progAsgRules/


THE ASSIGNMENT:

Write a class that implements a deque, write a driver for the class, and do testing to show that everything works.

A deque is like a cross between a stack and a queue. A deque has a front and a rear. You can insert an element at either the front or rear, and you can remove an element from either the front or rear.

To implement the deque, you must create a deque class similar to the example stack and queue classes we find in chapters six and seven of the Carrano text book. You must start with copies of the QueueA.h and QueueA.cpp files, and modify them as required to create a deque class.

You will be able to re-use much of the code in Queue.h and Queue.cpp. However you will have to change the names of many things, such as the name of the class and the names of the operations. You will also have to add some new operations. Rename the new versions of the files dequeCls.h and dequeCls.cpp.


SPECIFICATIONS OF DEQUE OPERATION:

The deque class must include the following operations: constructor, Length, IsEmpty, AddInFront, AddInRear, TakeOutFront, TakeOutRear, and Print. Here are specifications for all the operations except the constructor. You must implement the operations so they meet these specs.

// deque operations:

   int Length() const;
   // Determines the length of a deque.
   // Precondition: None.
   // Postcondition: Returns the number of items
   // that are currently in the deque.

   bool IsEmpty() const;
   // Determines whether a deque is empty.
   // Precondition: None.
   // Postcondition: Returns true if the deque is empty;
   // otherwise returns false.
 
   void AddInFront(dequeItemType NewItem, bool& Success);
   // Adds an item to the front of a deque.
   // Precondition: NewItem is the item to be added.
   // Postcondition: If insertion was successful, NewItem
   // is at the front of the deque and Success is true; 
   // otherwise Success is false.

   void AddInRear(dequeItemType NewItem, bool& Success);
   // Adds an item to the rear of a deque.
   // Precondition: NewItem is the item to be added.
   // Postcondition: If insertion was successful, NewItem
   // is at the rear of the deque and Success is true; 
   // otherwise Success is false.

   void TakeOutFront(dequeItemType& DequeFront, bool& Success);
   // Retrieves and removes the front of a deque.
   // Precondition: None.
   // Postcondition: If the deque was not empty, DequeFront
   // contains the item that was at the front of the Deque,
   // the item is removed, and Success is true. However, if the
   // deque was empty, deletion is impossible, DequeFront is 
   // unchanged, and Success is false.

   void TakeOutRear(dequeItemType& DequeRear, bool& Success);
   // Retrieves and removes the rear of a deque.
   // Precondition: None.
   // Postcondition: If the deque was not empty, DequeRear
   // contains the item that was at the rear of the Deque,
   // the item is removed, and Success is true. However, if the
   // deque was empty, deletion is impossible, DequeRear is 
   // unchanged, and Success is false.

   void Print() const;
   // Prints the contents of the Deque in front-to-back order.
   // (Prints to standard output.)
   // Does nothing if the Deque is empty.
   // Precondition: None
   // Postcondition: Deque is unchanged.

You must write each deque operation so that it uses the function statement given for it above, and so it follows the other specifications given in the header.


THE DRIVER:

In addition to implementing the deque class, you must write a simple driver program that takes commands to perform operations on a deque and show the results.

The job of the driver is to execute a simple loop. In each pass through the loop, the program reads a one-line command from standard input, interprets the meaning of the command, and then executes the actions required by the command. The loop must be written so that when the program receives the stop command it prints a "goodbye" message and then exits.


DRIVER INPUT AND OUTPUT:

This program reads all input from standard input, and writes all all output to standard output.

The documents sample.in and sample.out contain samples of the type of input the driver must process and the corresponding output that the program produces.

The input consists of very simple commands that tell the driver to perform operations on the deque. There is a stop command that tells the program when to quit.


COMPILING:

Assuming that your class implementation file is named dequeCls.cpp and the file containing your driver is named driver.cpp, you should compile the program like this:

g++ dequeCls.cpp driver.cpp

dequeCls.cpp and driver.cpp should include your dequeCls.h file.


WHAT TO TURN IN:

You will be sending me two e-mail messages. Please follow these rules: Here is a list of the things you have to turn in:
  1. Send me a copy of your completed driver.cpp file before midnight on the first due date. Send it by e-mail with the subject line: CS2500,prog4.drv.

  2. Send the following items to me by e-mail before midnight on the second due date:

    Include all the source files (*.h files and *.cpp files) that are required to compile the program into an executable image -- everything I need so that I can compile and test your program. Combine all the source files and the script into one shell archive file and e-mail me the archive file with the subject line: CS2500,prog4.fin.

Note that there are no spaces in the subject lines given. It is important that you do not insert any spaces. My e-mail address is: john@ishi.csustan.edu.


DUE DATES:

For the due dates, see the class schedule.