Code Listings Chapter 13

Listing 13-1
Listing 13-A

 

Listing 13-1  A  C++ interface for queues

/** @file QueueInterface.h */
#ifndef _QUEUE_INTERFACE
#define _QUEUE_INTERFACE
template < class ItemType > class QueueInterface
{
public:
/** Sees whether this queue is empty.
@return True if the queue is empty, or false if not. */
virtual bool isEmpty ()const = 0;

/** Adds a new entry to the back of this queue.
@post If the operation was successful, newEntry is at the
back of the queue.
@param newEntry The object to be added as a new entry.
@return True if the addition is successful or false if not. */
virtual bool enqueue (const ItemType & newEntry) = 0;

/** Removes the front of this queue.
@post If the operation was successful, the front of the queue
has been removed.
@return True if the removal is successful or false if not. */
virtual bool dequeue () = 0;

/** Returns the front of this queue.
@pre The queue is not empty.
@post The front of the queue has been returned, and the
queue is unchanged.
@return The front of the queue. */
virtual ItemType peekFront () const = 0;
}; // end QueueInterface
#endif

 

 

Listing 13-A  Final Pseudocode for Event-Driven Simulation

// Performs the simulation.
   simulate ():void 
     Create an empty queue bankQueue to represent the bank line
     Create an empty priority queue eventListPQueue for the event list
     
	tellerAvailable = true

// Create and add arrival events to event list
  while (data file is not empty)
  {
    	Get next arrival time a and transaction time t from file 
	newArrivalEvent = a new arrival event containing a and t 
	eventListPQueue.add (newArrivalEvent)
  }

// Event loop
while (eventListPQueue is not empty)
  	{
    newEvent = eventListPQueue.peek ()
	// Get current time
      currentTime = time of newEvent 
	if (newEvent is an arrival event)
      	processArrival (newEvent, eventListPQueue, bankQueue)
      else
      	processDeparture (newEvent, eventListPQueue, bankQueue)
      }

	// Processes an arrival event.
    processArrival (arrivalEvent: Event, eventListPQueue:PriorityQueue,
    bankQueue:Queue)

	// Remove this event from the event list
	eventListPQueue.remove ()
	customer = customer referenced in arrivalEvent 
	if (bankQueue.isEmpty () && tellerAvailable)
	{
	  departureTime = currentTime + transaction time in arrivalEvent 
	  newDepartureEvent =
	  a new departure event with departureTime 
	  eventListPQueue.add (newDepartureEvent) tellerAvailable = false
	}
      else

	bankQueue.enqueue (customer)

	// Processes a departure event .
      + processDeparture (departureEvent: Event, eventListPQueue:PriorityQueue,
      	bankQueue:Queue)

	// Remove this event from the event list
	eventListPQueue.remove ()

	if (!bankQueue.isEmpty ())
	{
	// Customer at front of line begins transaction
	  customer = bankQueue.peek ()
	  bankQueue.dequeue ()
	  departureTime = currentTime + transaction time in customer 
	  newDepartureEvent = a new departure event with departureTime 
	  eventListPQueue.add (newDepartureEvent)
	}
      else
	  tellerAvailable = true