Mastery 1: CPS 108, Fall 1996

Due: October 7 (there will be a group project started during this time)

Mastery exams are solo projects

* testing deque programs

The program was used to test aspects of the deque implementation. A template file was used to generate instantiations of the deque class.

Templated Deques

You are to implement a templated class for double-ended queues, also called deques. You must implement the following operations for your deque class (as member functions).

In addition, you should implement a destructor, assignment operator, and copy constructor.

Implementation Details

You cannot use any classes in your implementation, the deque must be implemented using only built-in types (e.g., no vector class, no list class). You should feel free to borrow/steal code from the vector and list classes (on acpub see ~ola/courses/lib on cs machines see /usr/project/courses/cps008/lib.)

Perhaps the easiest way to meet the requirements for O(1) append and prepend and expected O(1) access is to use a directory array of pointers to nodes. Each node contains an array of (say 20) elements. Initially the middle node of the directory array is the only non-NULL pointer. To prepend you can add a new node before the middle directory node. A node is added after the middle directory node when the 20-element middle node is full. You'll only need to grow the directory array when all its nodes are allocated.

Inherited Interface

Your class must be implemented by inheriting from the abstract base class ADeque declared below and accessible from ~ola/cps108/deque/adeque.h

#ifndef _ADEQUE_H #define _ADEQUE_H // abstract base class for implementing double-ended queues // (dequeues) // template <class Type> class ADeque { public: virtual ~ADeque(){}; // mutator functions virtual void Append(const Type & elt) = 0; // add element to end virtual void Prepend(const Type & elt) = 0; // add element to front virtual void Poplast() = 0; // pop last element virtual void Popfirst() = 0; // pop first element virtual Type & operator [](int index) = 0; // returns index-th entry virtual void Insert(int k, const Type & elt) = 0; // insert elt at index k // accessor functions virtual const Type & operator[](int index) const = 0; // const version virtual int Size() const = 0; // returns #elts }; #endif

You may find it easier to implement a non-templated version of this class first (e.g., for ints or strings) and then change the implementation to be templated.


Submit as shown below. You must submit .h and .cc files for your dequeue class. You must inherit from ADeque to facilitate our test programs. You should also submit your own test program along with an explanation (in your README) of what your test program does and how well you think it tests the correctness of your implementation. In your README you should also include a profile analysis (obtained using gprof) of a program that stresses the performance of your dequeue class. You should submit this test program too. Note that there will be two test programs: one to test performance and one to test correctness of the deque class functions. Your makefile should be able to make both.
  submit108 mastery dequeue.h README Makefile ...


This assignment is worth 50 points awarded as follows.
Behavior Points
Works Correctly (performance irrelevant) 10
Meets Performance Specs 10
Style/Coding standards 10
Test Programs 10
README, Writeup 10

For extra credit, your deqeue class should handle it's own memory allocation, e.g., nodes that are deleted should be kept on a per-class (e.g., static) freelist and these nodes should be used when allocating new nodes. This can earn an aditional 10 points.