Please see the programming webpage for details about the programming environment for this course, guidelines for programming style, and details on electronic submission of assignments.
The files you may need for this assignment can be downloaded here.
For this assignment, you will work individually and submit your own work.
Please make sure you adhere to the policies on academic integrity in this regard.
Web browsers commonly allow you to navigate through a "history" of web pages that have previously been visited. The mechanism is somewhat like a stack, in that the most recently visited pages are at the top of the history and revisited when the "back" button is pressed.
However, the history does not necessarily have unbounded capacity. In reality, there may exist a fixed limit on the size of the history. The issue arises as to what should happen when the capacity is exhausted and a new item is pushed onto the stack. One possibility is to throw an exception. But this is not how a Web browser behaves. If it only has room to save 50 pages in its history and yet you visit more pages, it will make room in the history for a new page by throwing away the page that is on the very bottom of the history (i.e., the least recently visited page). The formal Stack interface does not help, as it gives us no way to directly access or remove the object on the bottom of the stack.
In this assignment, we define a new ADT that we call a LeakyStack. The interface for a LeakyStack is very similar to the interface given for a Stack. However in the case when the capacity is exhausted, a call to push will result in the placement of the new page at the expense of the loss of the least recently accessed page. We formalize this interface as an abstract class definition, provided online in file LeakyStack.h.
/** * Interface for a leaky stack: A collection of objects that are inserted * and removed according to the last-in first-out principle, but with * overflow handled by the removal of the least-recently accessed item. */ class LeakyStack { public: /** Returns the number of objects in the stack. * @return number of elements */ int size() const; /** Determines if the stack is currently empty. * @return true if empty, false otherwise. */ bool empty() const; /** Returns a const reference to the top object in the stack. * Throws a runtime_error if the stack is empty. * @return reference to top element */ const string& top() const; /** Inserts an object at the top of the stack. If the stack * is already at capacity, the oldest element will be lost. * @param the new element */ void push(const string& elem); /** Removes the top object from the stack. * Throws a runtime_error if the stack is empty. */ void pop(); };
For this assignment, you will be required to give two different implementations for the LeakyStack interface, as described below. We will provide you with sample initial files for these classes.
One way to implement a LeakyStack is as follows. Use the ArrayStack implementation essentially verbatim from lecture, modifying only the push method accordingly. In the case when pushing onto a "full" stack, use a loop to shift all items down one location in the array. By doing this, you will lose your bottommost item while opening up the topmost location for the newly pushed object.
A second way to implement a LeakyStack is to use an array viewed in a circular manner (in a style similar to our ArrayQueue implementation). You can mark the "top" of the circular stack with an extra integer variable that is an index into your array. By also keeping explicit count of the current size of the stack, you can effectively identify the "bottom" of the stack as well, when needed. With a bit of care, you can more efficiently handle pushes, even those involving overflow.
To make things a bit easier for this assignment,
we have intentionally defined the LeakyStack to be a stack of strings rather than using a generic item type. This means that you do not need to write templated code for this assignment.
our driver will not rely upon the existence of proper "housekeeping" methods (a copy constructor, assignment operator, and destructor). So you do not need to implement them, even though they would be necessary for legitimate versions of these classes.
All such files can be downloaded here.
LeakyStack.h
This file formally defines the abstract class LeakyStack.
LeakyStackA.h
This file should be used to formally define the
LeakyStackA class, which is the first of the two
implementations of the LeakyStack interface.
The initial file that we provide is not complete. Rather it
defines all of the public methods that you will need to
implement, yet with stubs for most of the method bodies. You
will need to modify this file appropriately.
LeakyStackA.cpp
This file should contain the implementations for most of the
methods defined in the LeakyStackA class.
The initial file that we provide is not complete. Rather it
defines so-called stubs for each such method. The
stubs are syntactically correct so that they can be compiled,
but they are in no way semantically correct implementations of
the desired behaviors. You
will need to modify this file appropriately.
LeakyStackB.h
Analog to LeakyStackA.h but to be used for the
second of the two implementations as outlined in this
assignment.
LeakyStackB.cpp
Analog to LeakyStackA.cpp but to be used for the
second of the two implementations as outlined in this
assignment.
WebHistoryDriver.cpp
This file provides a main driver to be used in testing your
program. You will not need to modify this code.
The use of the driver is discussed in the next section.
makefile
This makefile should allow you to rebuild your project by
simply typing 'make' rather than invoking the compiler
directly.
WebHistoryDriver is a text-based, menu-driven program for testing your LeakyStack implementations. The menu allows you to call any combination of LeakyStack methods. You may even call the constructors for either of the two implementations anytime you want to restart with a different capacity.
The interface is fairly simple. Typing 'A' (or 'a') creates a leakystackA. 'B' or 'b' creates a leakystackB. Once your stack is created, you can do a series of pushes, pops, and tops (using the appropriate letter for input), where after a push you are expected to enter some data which will be added to the stack.
By default, the program takes input from the keyboard. However the driver has an optional feature which allows it to read input from a file rather than from a keyboard. The file should have the identical characters which you would use if you were typing them yourself on the keyboard. This feature simply allows you to test and retest your program on a particular input without having to retype the input each time you want to run the program. It is also convenient at times when running with a debugger to have the input read from a file rather than from the keyboard. If the program reaches the end of the file without exiting, then it will revert back to reading input from the keyboard for the remainder of the program.
To use this feature, you must specify the exact filename (including any suffix), as a single argument at runtime, using a syntax such as
./WebHistoryDriver inputfile
Part of developing good software is also knowing how to perform meaningful testing of your software. Our hypothetical motivation for this data structure was that it might be used in the larger context of a web browser. One approach to testing would be to wait until the entire project were completed and then to test the final result. Of course, if the browser history were not operating properly at that point, we would have to wonder whether the problem was due to an errant implementation of the data structure, to an errant use of a well-written data structure, or some other problem. Rather than relying on a test of the entire end product, a useful approach is to make sure that each individual piece works in isolation. This is called unit testing. So for this assignment, you will develop a test plan for evaluating the correctness of the LeakyStack implementations without actually considering the larger application (in fact, we will not actually be writing any such hypothetical web browser). To perform unit testing of this data structure, you will use the provided driver which allows you to individually call any of the public methods supported by the data structure.
There is another issue we wish for you to consider when developing test plans. Often, testing is performed in close coordination with an examination of source code. For example, we may write some code or view some code, and then try to test that particular chunk of code. There is an inherent danger in relying only on such tests. In particular, if the tests are based on the same flow of thoughts as (errant) code, we are more likely to forget to test for a particular combination of events that were not considered by the code. The practice of "black box" testing is to design a set of tests based purely on the formal specifications for a data structure, but without actually seeing the source code.
On this assignment, we will have you develop a black-box unit test plan for LeakyStack implementations. Specifically, you must submit a plain text file named "inputfile" which will be used to test other students' implementations. The goal is to create test input which causes as many as possible of the other students' programs to fail in some way. Because our execution of student tests will be automated, you must make sure that your "inputfile" strictly adheres to the file format which is expected by the text-based driver, WebHistoryDriver, described earlier. Your file may use at most 100 commands. If your input file contains more than the prescribed number of commands, we will simply truncate the end of the file. In order to make sure that your inputfile follows the proper format, we strongly recommend that you run your program on your inputfile to make sure it is accepted by the driver.
Here is a sample input file, appropriately formatted.
Source Code
Please submit all source code files which you have either
modified or added. For this assignment, we expect this to include the
four files LeakyStackA.h,
LeakyStackA.cpp,
LeakyStackB.h, and
LeakyStackB.cpp.
Test Input
Please submit a single file, inputfile, as outlined in the
earlier section on testing.
Readme File
For each assignment, you are to submit a separate file "readme" as
specified in the general programming webpage.
For this assignment, you should include two separate tables, similar
to Table 4.2 on page 174 of the text, summarizing the
worst-case asymptotic running times for your implementations.
One table should discuss LeakyStackA and one should discuss
LeakyStackB.
The assignment is worth 10 points. Eight points will be awarded based on our own evaluation of your assignment and the readme file. One point will be based on how well your test input checks both your and the other students' implementations.
The final point will be added based on an early check-in with the professor; you will be expected to show at least 20 lines of (new) code in by Friday, Sept. 30. This may be done via email, after class, in normal office hours, or at a different time by appointment, but it must be done by the end of the day on Friday.
When dealing with an array in a circular fashion, there are many
possible ways for adjusting indicies as they "wrap around" the ends of
the array. The text chooses to use modular arithmetic (the "%"
operator in C++). Please note that its precedence is equivalent to
multiplication and division, and therefore the expression