Lab 6: STL List

Topics: Introduction to Lists

Group work: Group work is allowed for the labs, but each person must do their own coding and each person must turn in an assignment. Copying other peoples' code / code files is not allowed. Copied assignments will receive 0% for everybody involved.

Assignments must build: Assignments that don't build will automatically just be given a flat 50% score. I may still give feedback on what went wrong, and 50% is better than 0%, so turn in something - but ideally, make sure it builds.


 

About

Another kind of data structure we have access to as part of the Standard Template Library is the list. Similarly to a vector, it handles resizing as needed, though list and vector aren't exactly the same.

The list class is built on top of a Linked List structure, as opposed to a Dynamic Array structure. You will be learning about Linked Lists and how they work soon.

From the outside, our big difference is being able to add new items to the front and the back of the list, and also that we must use an iterator to step through each element of the list (we can't just use the subscript operator like we can with a vector).

You can find the list documentation, function descriptions, and sample code here: http://www.cplusplus.com/reference/list/list/


 

Setting up the project

In your Student Repository, open the CS250-Lab06-STLList project. It will have the following files:

  • main.cpp
  • functions.hpp
  • functions.cpp

This program runs a "donut shop simulator" using a list as the line (or queue) at the donut shop. Each cycle of the program, ten minutes pass, and one of the following events can occur:

  • A new customer can enter the BACK of the line
  • A new customer with a special coupon can enter the FRONT of the line
  • The customer at the FRONT of the line can order their donut and leave
  • The customer at the BACK of the line can get tired of waiting and leave

And there are some additional functions that act as helpers - getting the front and back customers and displaying the list of who is in line.

You will utilize the STL list documentation to help implement each function.

You will implement the functions:

  • void CustomerEntersBackOfLine( list<Customer>& line, Customer customer )
  • void CustomerEntersFrontOfLine( list<Customer>& line, Customer customer )
  • void CustomerOrders( list<Customer>& line )
  • void CustomerRagequits( list<Customer>& line )
  • Customer GetLineFront( list<Customer>& line )
  • Customer GetLineBack( list<Customer>& line )
  • string GetLine( list<Customer>& line )

 

void CustomerEntersBackOfLine( list<Customer>& line, Customer customer )

Add the customer to the back of the line of customers. Use the .push_back() function.


 

void CustomerEntersFrontOfLine( list<Customer>& line, Customer customer )

Add the customer to the front of the line of customers. Use the .push_front() function.


 

void CustomerOrders( list<Customer>& line )

Remove the customer at the front of the line. Use the .pop_front() function.

p>

 

void CustomerRagequits( list<Customer>& line )

Remove the customer at the back of the line. Use the .pop_back() function.


 

Customer GetLineFront( list<Customer>& line )

Get the customer at the front of the line. Use the .front() function.


 

Customer GetLineBack( list<Customer>& line )

Get the customer at the back of the line. Use the .back() function.


 

string GetLine( list<Customer>& line )

Create a string that lists all customers in line.

With an STL list, we must use something called iterators to go through everything. There is a chapter on iterators later on, so for now just use this code:

string GetLine( list<Customer>& line )
{
    string text = "";
    for ( list<Customer>::iterator it = line.begin(); it != line.end(); it++ )
    {
        text += it->name + " ";
    }
    return text;
}

 

Running the program

When you run the program, it will run the simulation and log all the events to the output file, donut-sim.html. This file will be in your project directory.


 

Turn in

Turn in your functions.cpp file - no other files are necessary.