Assignments | Course Home | Documentation | Lab Hours/Tutoring | Schedule | Submit

Saint Louis University

Computer Science 2100
Data Structures

Michael Goldwasser

Fall 2015

Dept. of Math & Computer Science


Lab Assignment 06

Topic: Overflowing Bookshelf
Source Code: bookshelf.cpp
Live Archive Ref#: 3391

Pre-lab Due:

Thursday, 22 October 2015, 10:00am
Submission Deadline: Friday, 23 October 2015, 11:59pm

Techniques:

Use of a std::list and iterators.

More Information:

General information about labs, testing, judging utility

Collaboration Policy

The pre-lab requirement must be completed and submitted individually.

The remainder of the lab activity should be completed working in pairs. One person should submit the result, making sure that both partners' names are clearly identified in that submission.

Please make sure you adhere to the policies on academic integrity in this regard.


Pre-Lab Requirement

Read the complete problem description and then determine what the expected output should be if given the following input:

Prelab input: Prelab output:
100
A 1 20
A 2 20
A 3 30
A 4 30
R 2
A 5 1
A 6 19
E
7
A 49 1
A 48 1
R 43
A 47 1
A 46 1
A 45 1
A 44 1
A 43 1
A 42 6
R 42
A 49 1
A 48 1
A 47 1
E
-1


Overflowing Bookshelf

Agnes C. Mulligan is a fanatical bibliophile – she is constantly buying new books, and trying to find space for those books. In particular, she has a shelf for her “to be read” books, where she puts her newest books. When she decides to read one of these books, she removes it from the shelf, making space for more books. Sometimes, however, she buys a new book and puts it on the shelf, but because of limited space, this pushes one or more books off the shelf at the other end. She always adds books on the left side of the shelf, making books fall off the right side. Of course, she can remove a book from any location on the shelf when she wants to read one.

Your task will be to write a simulator that will keep track of books added and removed from a shelf. At the end of the simulation, display the books remaining on the shelf, in order from left to right. Books in each simulation will be identified by a unique, positive integer, 0 < I ≤ 100. There are three types of events in the simulation:

Input: The input file will contain data for one or more simulations. The end of the input is signalled by a line containing -1. Each simulation will begin with the integer width of the shelf, s, 5 ≤ s ≤ 100, followed by a series of add and remove events. An add event is a single line beginning with an upper case 'A' followed by the book ID, followed by the integer width of the book, w, 0 < ws. A remove event is a single line beginning with an upper case 'R' followed by the the book ID. Finally, the end event is a line containing only a single upper case 'E'. Each number in an event is preceded by a single blank.

Output: For each simulation case, print a single line containing a label (as shown in the output sample), followed by the list of IDs of books remaining on the shelf, in order from left to right.

Example input: Example output:
10
R 3
A 6 5
A 42 3
A 3 5
A 16 2
A 15 1
R 16
E
7
A 49 6
A 48 2
R 48
E
5
A 1 1
A 2 1
A 3 1
R 2
A 4 1
A 5 1
R 5
R 4
A 6 1
A 7 4
E
-1
PROBLEM 1: 15 3
PROBLEM 2:
PROBLEM 3: 7 6



Judge's Data

You can run the automated judge's tests on turing to test the correctness of your program (although you must still formally submit the source code via the course website). To use the judging utility, your source code must be named bookshelf.cpp. If you are working on your own machine (or if you just want to examine the judge's inputs and expected outputs), we provide them here.


Submit

One member of the team should submit the file bookshelf.cpp through the course submission page. Please make sure that the names of both members of the team are included in comments at the beginning of the source code.


Hints

This was a more difficult programming contest problem than the ones we have seen in our earlier labs. By the end of a five-hour contest, less than 50% of the teams had successfully solved it, and the average solving time for those who finished it was over 3 hours into the contest. That said, it is a rather straightforward problem with proper use of data structures.

The basic simulation sounds most like a queue, with new books placed on one end of the shelf and old books falling off the other end of the shelf. However, the rules also allow Agnes to remove a book from an arbitrary position. So for this reason, we are going to use a list rather than a queue. We will store an iterator for each book that is inserted, marking its position to facilitate a later removal. For each trial of the simulation, we recommend that you declare the following variables:

Notes of warning:


Michael Goldwasser
CSCI 2100, Fall 2015
Last modified: Wednesday, 21 October 2015
Assignments | Course Home | Documentation | Lab Hours/Tutoring | Schedule | Submit