Rogue Wave banner
Previous fileTop of documentContentsIndexNext file

10.3 The queue Data Abstraction

As a data abstraction, a queue is traditionally defined as any object that implements the following operations given in Table 16:

Table 16 -- Queue operations

Function Implemented operation
empty()
Returns true if the collection is empty
size()
Returns number of elements in collection
front()
Returns (but does not remove) the element at the front of the queue
back()
Returns the element at the end of the queue
push(newElement)
Pushes a new element on to the end of the queue
pop()
Removes (but does not return) the element at the front of the queue

Note that the operations of accessing and of removing the front elements are performed separately.

10.3.1 Include Files

Programs that use the queue data abstraction should include the file queue:

10.3.2 Declaration and Initialization of queue

A declaration for a queue must specify the element type, and can also specify the container that will hold the values. For a queue the default container is a deque, but a list can also be used. The list version is generally smaller, while the deque version may be slightly faster. The following are sample declarations for a queue:

The last example creates a queue of a user-defined type named Customer. As with the stack container, all objects stored in a queue must understand the operators < and ==.

Because the queue does not implement an iterator, none of the generic algorithms described in Part IV apply to queues.

10.3.3 Example Program: Bank Teller Simulation


NOTE: The complete version of the bank teller simulation program is in teller.cpp.

Queues are often found in businesses, such as supermarkets or banks. Suppose you are the manager of a bank, and you need to determine how many tellers to have working during certain hours. You decide to create a computer simulation, basing your simulation on certain observed behavior. For example, you note that during peak hours there is a ninety percent chance that a customer will arrive every minute.

We create a simulation by first defining objects to represent both customers and tellers. For customers, the information we want to know is the average amount of time they spend waiting in line. Thus, customer objects simply maintain two integer data fields: the time they arrive in line, and the time they spend at the counter. The latter is a value randomly selected between 2 and 8. (See Section 2.2.5 for a discussion of the randomInteger() function.)

Because objects can only be stored in Standard C++ Library containers if they can be compared for equality and ordering, it is necessary to define the operators < and == for customers. Customers can also tell us when they are done with their transactions.

Tellers are either busy servicing customers, or they are free. Thus, each teller value holds two data fields: a customer, and a boolean flag. Tellers define a member function to answer whether they are free or not, as well as a member function that is invoked when they start servicing a customer.

The main program, then, is a large loop cycling once each simulated minute. The probability is 0.9 that each minute a new customer is entered into the queue of waiting customers. Each teller is polled, and if any are free they take the next customer from the queue. Counts are maintained of the number of customers serviced and the total time they spent in queue. From these two values we can determine, following the simulation, the average time a customer spent waiting in the line.

By executing the program several times, using various values for the number of tellers, the manager can determine the smallest number of tellers that can service the customers while maintaining the average waiting time at an acceptable level.



Previous fileTop of documentContentsIndexNext file
©Copyright 1998, Rogue Wave Software, Inc.
Send mail to report errors or comment on the documentation.
OEM Release, June 1998