# Application for Bank Teller Position - PowerPoint

Document Sample

```					                                      Queues Everywhere!

M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues   1
Queues

Queue:
– Linear, homogeneous structure
– Has First-In-First-Out (FIFO) behavior;
• Items added at one end and removed from the other end
– Middle elements are logically inaccessible
– Important in simulation & analyzing the behavior of
complex systems
Enqueue                                                                            equeue

M. Malaty - OCC - CS?CIS - CS200 - Data Structures     Chapter 7: Queues                           2
Queue Applications

 Real-World Applications
– Check out at a bookstore
– Bank / ATM
– Call an airline
– Cashier lines in any store
 Computer Science Applications
– Print lines of a document
– Printer shared between computers
– Convert digit strings to decimal
– Recognizing palindromes
– Shared resource usage (CPU, memory access, …)
M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues   3

Specification:
– Elements
• Homogeneous
• Linear
– Structure:
• Elements added to rear & removed from front
– Items:
• Item type provided by the user
• Maximum number of items provided either by the user or
by the capacity of available memory

M. Malaty - OCC - CS?CIS - CS200 - Data Structures      Chapter 7: Queues   4

 Specification:
– Operations
• Create an empty queue
• Create a copy of a queue
• Destroy a queue
• Check if a queue is empty / Full
• Enqueue (Enq, Enque, Add, Insert) new element to the back (rear)
• Dequeue (Deq, Deque, Remove, Serve, Delete) an element from the
front
• Retrieve an element from a queue
– Like the stack, if we need to modify an item in the queue, we
must remove it, change its contents, and then add it back to
the queue
M. Malaty - OCC - CS?CIS - CS200 - Data Structures      Chapter 7: Queues          5

Implementation:
– Pointer-based (Linear or Circular)
– Array-based (Linear or Circular)

M. Malaty - OCC - CS?CIS - CS200 - Data Structures      Chapter 7: Queues   6
UML Diagram for Queue Class

+Queue()
+~Queue()
+isEmpty():boolean {query}
+enqueue(in newItem:QueueItemType)
throw QueueException
+dequeue()
throw QueueException
+dequeue(out queueFront:QueueItemType)
throw QueueException
+peek(out queueFront:QueueItemType)
throw QueueException

M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues   7

 Pseudocode
+Queue()
// Creates an empty queue
// Precondition: None
// Postcondition: A queue is created and is empty
+~Queue()
// Destroys a queue
// Precondition: Queue exists
// Postcondition: Queue elements deleted and queue destroyed
+isEmpty(): boolean {query}
// Determines whether a queue is empty
// Precondition: Queue is created
// Postcondition: Returns true is the queue is empty; false otherwise
+enqueue( int newItem) throw QueueException
// Inserts (Enqueue) newItem at the back of a queue.
// Throws an exception if insertion is not successful.
// Precondition: Queue exists and is not full
// Postcondition: If success, NewItem is attached to the rear of the
//                queue; otherwise queue is unchanged
M. Malaty - OCC - CS?CIS - CS200 - Data Structures      Chapter 7: Queues   8

 Pseudocode
+dequeue( ) throw QueueException
// Removes (Dequeue) the front of a queue; i.e. the item that was
// added the earliest. Throws an exception if deletion isn’t successful.
// Precondition:Queue exists and is not empty
// Postcondition:If success, front element is removed from the queue;
//           otherwise, queue is unchanged
+dequeue ( int& queueFront) throw QueuException
// Retrieves into queueFront and removes the front of a queue.
// Throws an exception if deletion is not successful
// Precondition:Queue exists and is not empty
// Postcondition:If success, front element is removed from the queue,
//          QueuFront holds its value; otherwise,
//          QueueFront is undefined and queue is unchanged
+peek(int& queueFront) throw QueueException
// Retrieves into queueFront the front of a queue. Queue is
// unchanged. Throws an exception if retrieval is not successful.
// Precondition: Queue exists and is not empty
// Postcondition:Queue is unchanged and QueueFront contains
//      a copy of the contents of the element at the front of the queue
M. Malaty - OCC - CS?CIS - CS200 - Data Structures      Chapter 7: Queues    9
Queue Applications

A queue can retain characters in the order in
which they are typed
Queue aQueue;
while (not end of line) {
aQueue.enqueue(ch)
} //end while
Once the characters are in a queue, the system
can process them as necessary

M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues   10
Queue Applications

 Converting Digit Strings to Decimal
// Convert digits in aQueue into decimal integer n
// Get first digit, ignoring any leading blanks
do {       aQueue.dequeue( ch)
} until ( ch != blank)
// Assertion: ch contains first digit
// Compute n from digits in queue
n = 0;
done = false;
do {      n = 10 * n + integer that ch represents
if (! aQueue.isEmpty( ) )
aQueue.dequeue(ch)
else
done = true
} until ( done || ch != digit)
// Assertion: n is result
M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues   11

 Possible implementations:
– Pointer-based
– Array-based
•        Linear
•        Circular
•        Linear
•        Circular
– STL
 All implementations will use the following exception
#include <exceptopn>
#include <string>
using namespaces std;
class QueueException: public logic_error
{ public:
QueueException(const & message =“ ”) : logic_error (message.c_str( ) )
{}
}; // end QueueException
M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues                12

 Pointer-Based Implementation
– More straightforward than array-based
– Possible options:
– Two external pointer (Front & Back)
– One pointer will be enough (Back)
– Both the copy constructor & destructor must be written, because of the
dynamic allocation
– Insertion
• Inserting a new node at the Back needs 3 pointer changes
Change Next pointer in the new node
Change the Next pointer in the Back node
Change the External pointer
• Special case:
– If the queue is empty
– Deletion
• Delete from the Front
• Only one pointer change is needed
• Special case:
– If the queue contains one item only
M. Malaty - OCC - CS?CIS - CS200 - Data Structures         Chapter 7: Queues         13

 Pointer-Based Implementation: Option 1
– Linear linked list with 2 external pointers
– Insertion to an empty queue                                                                            frontPtr
newPtr
frontPtr = backPtr = newPtr
20
– Insertion to a non-empty queue
Inserte
newPtr -> next = NULL                                                       d
element                   backPtr
backPtr -> next = newPtr
backPtr = newPtr
– Deletion
tempPtr = frontPtr
backPtr
frontPtr = frontPtr -> next    frontPtr   Deleted                                       Inserte
element                                          d
tempPtr -> next = NULL                                                                  element

delete tempPtr
20                       15             26               84

M. Malaty - OCC - CS?CIS - CS200 - Data Structures        Chapter 7: Queues                                            14

 Pointer-Based Implementation
– Circular linear linked list with one external pointer
– Insertion
NewPtr                 BackPtr
• Into an empty queue
NewPtr -> Next = NewPtr
BackPtr = NewPtr
• Into a non-empty queue                                                              30
NewPtr -> Next = BackPtr-> Next
BackPtr -> Next = NewPtr                                      Inserte
d
BackPtr = NewPtr                                              element

BackPtr
Inserte
d
element
20                         15        26                       84               30

NewPtr

M. Malaty - OCC - CS?CIS - CS200 - Data Structures        Chapter 7: Queues                                         15

 Pointer-Based Implementation
– Deletion
• form a one-node (one item) queue                                                     BackPtr
FrontPtr = BackPtr -> Next
If (Frontptr = BackPtr)
BackPtr = NULL                                                   30
delete FrontPtr
• form anon-empty, more than one item queue                                          Deleted
element
FrontPtr = BackPtr -> Next
BackPtr -> Next = FrontPtr -> Next
delete FrontPtr
FrontPtr                                 BackPtr

Deleted
element

20                   15                 26    84         30

M. Malaty - OCC - CS?CIS - CS200 - Data Structures              Chapter 7: Queues                            16

Pointer-Based Implementation
– Implementation file QueueP.cpp

M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues   17

Example:
#include “QueueP.h”
Int main ( )
{
Queue aQueue;

aQueue.enqueue ( 15 );
...
}

M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues   18

 Array-Based Implementation
– If statically allocated arrays are used, the compiler-
generated destructor & copy constructor are
sufficient
– If dynamically allocated arrays are used, we must
provide the (deep) copy constructor & destructor
– Pre- & Post-conditions are the same for pointer-
based implementation

M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues   19

 Array-Based Implementation
– 1. Linear Array implementation
• front & back are indexes in the array
• Initial condition: front =0 & back = -1
 Initial state
0        -1

Front       Back    0          1       2    3        4
MAX_QUEUE -1
0     4      20         45     51       76       84

front     back     0           1     2    3        4
MAX_QUEUE -1

M. Malaty - OCC - CS?CIS - CS200 - Data Structures              Chapter 7: Queues                                         20

 Array-Based Implementation
– back will point to the last added element
– Initial values:
• front = 0
• back = -1
– Insertion
• Increment back
• Insert item in item [ back ]
– Deletion
• Increment front
– Queue Empty Condition
• back < front
M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues   21

1. Linear Array implementation
– Problem: Rightward-Drifting:
• After a sequence of additions & removals, items will drift
towards the end of the array

Rightward
drifting
47    49                        16      60 20
0         1        47     48   49
front   back

MAX_QUEUE -1

M. Malaty - OCC - CS?CIS - CS200 - Data Structures           Chapter 7: Queues                    22

 Rightward drifting solutions
– Shift array elements after each deletion
•  Shifting dominates the cost of the implementation
– Use a circular array: When Front or Back reach the end of the array, wrap
them around to the beginning of the array
• Problem:
– Front & Back can't be used to distinguish between queue-full & queue-empty
conditions
• Solution:
– Use a counter
– Count == 0 means empty queue
– Count == MAX_QUEUE means full queue
– Another solution is to let front points to the array element preceding the front
element
• front = back => Queue is empty
– Additional convention: Reserve the array position pointed to by the front to contain
no elements
• Initially         front = back = MAX_QUEUE –1
M. Malaty - OCC - CS?CIS - CS200 - Data Structures      Chapter 7: Queues                             23

 Circular Array Implementation
const int MAX_QUEUE = maximum-size-of-queue;
typedef desired-type-of-queue-item QueuItemType;
QueueItemType items [ MAX_QUEUE ];                         MAX_QUEUE -1
int     front;
int     back;                                                                       Front
int     count                                                         20
– Insertion                                                                            45
back = ( back + 1 ) % MAX_QUEUE;
51
items[back] = newItem;
76
++count;
– Deletion                                                                                  Back
front = ( front + 1 ) % MAX_QUEUE;
- - count;
M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues                                    24

 Circular Arrays Implementation
– Initial condition:
• count = 0, front = 0, back = MAX_QUEUE – 1
– The Wrap-around effect is obtained by using modulo
arithmetic (%-operator)
– Insertion
• Increment back, using modulo arithmetic
• Insert item
• Increment count
– Deletion
• Increment front using modulo arithmetic
• Decrement count
– Overhead of maintaining a counter or flag

M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues   25

 Circular Array Implementation
– Solution
• Use IsFull flag
–  Same overhead as using the count
• Use extra array location
– Same space requirement
– More time-efficient
– Queue is Full:
• front == (back + 1) % (MAX_QUEUE + 1)
– Queue is Empty:
• front == back
– If the elements of the queue are complex data structure, the memory
wasted may be significant

M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues                         26

Circular Array Implementation

(a) front passes back when the queue
becomes empty;

(b) back catches up to front when the
queue becomes full

M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues   27

Circular Array Implementation
– Implementation file QueueA.cpp

M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues   28

– Use an instance of List class to represent the queue
– Pre- & Post-conditions are the same as before
– The implementation is much simpler

M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues   29

– If the item in position 1 of a list aList represents the
front of the queue, the following implementations can
be used
• dequeue()
aList.remove(1)
• getFront(queueFront)
aList.retrieve(1, queueFront)
– If the item at the end of the list represents the back of
the queue, the following implementations can be
used
• enqueue(newItem)
aList.insert(getLength() + 1, newItem)

M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues   30

– Implementation fiile QueueL.cpp

M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues   31
Comparing Implementations

 Fixed size versus dynamic size
– A statically allocated array
• Prevents the enqueue operation from adding an item to the queue if the
array is full
– A resizable array or a reference-based implementation
• Does not impose this restriction on the enqueue operation
 Pointer-based implementations
• More efficient
• Simpler to write

M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues              32
Comparing Implementations

Pointer-Based
– More complicated than ADT List
– Most flexible No size restrictions
Array-Based
– No overhead of pointer manipulation
– Prevents adding elements if the array is full
– Simpler to write
– Not as efficient as using a linked list directly
M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues   33

 Lists
– Operations are defined in terms of position of data items
– No restrictions on the position. All positions can be accessed
– Operations:
• create:
– Creates an empty ADT of the List type
• isEmpty:
– Determines whether an item exists in the ADT
• insert:
– Inserts a new item in any given position
• remove:
– Deletes an item from a given position
• retrieve:
– Retrieves the item in the specified position

M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues   34

 Stacks
– Operations are defined in terms of position of data items
– Position is restricted to the Top of the stack. Only one end
position can be accessed
– Operations:
• create:
– Creates an empty ADT of the Stack type
• isEmpty:
– Determines whether an item exists in the ADT
• push:
– Inserts a new item into the Top position
• pop:
– Deletes an item from the Top position
• peek:
– Retrieves the item from the Top position
M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues   35

 Queues
– Operations are defined in terms of position of data items
– Position is restricted to the front & back of the queue. Only the
end positions can be accessed
– Operations:
• create:
– Creates an empty ADT of the Queue type
• isEmpty:
– Determines whether an item exists in the ADT
• enqueue:
– Inserts a new item in the Back position
• dequeue:
– Deletes an item from the Front position
• peek:
– Retrieves the item from the Front position

M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues   36

Stacks and queues are very similar
– Operations of stacks and queues can be paired off
as
•    createStack and createQueue
•    Stack isEmpty and queue isEmpty
•    push and enqueue
•    pop and dequeue
•    Stack getTop and queue getFront

M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues   37
Application: Simulation

 Simulation
– A technique for modeling the behavior of both natural and human-made
systems. The behavior of a system Model should behave similar to real
system
– Goal
• Generate statistics that summarize the performance of an existing system by
constructing a mathematical or a physical model capturing the relevant
• Factors affecting the model are changed and the effect on the behavior of the
system is watched and measured
• Predict the performance of a proposed system
– Example
•    A simulation of the behavior of a bank
•    Supermarket
 Real-word systems are called “Queuing Systems”
 Theory used in simulation is called “Queuing Theory”
– Wikipedia
M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues                      38
Simulation

 Goal of simulation:
– Generate statistics summarizing or predicting the performance of systems,
that could be used for improvement
 What should be known to simulate a queuing system?
–    Number and types of events and how they affect the system
–    Number of servers
–    Distribution of arrival time of events
–    Expected service time
 Example: Bank system
–    Server: Teller
–    Objects being served: Customers
–    What should be observed: Average waiting time
–    Events: Arrivals & departures of customers
–    Service time: Time needed for each customer

M. Malaty - OCC - CS?CIS - CS200 - Data Structures      Chapter 7: Queues      39
Simulation

 Example: Bank system

Figure 7.14 A bank line at time a) 0; b) 12 c) 20; d) 38

M. Malaty - OCC - CS?CIS - CS200 - Data Structures                Chapter 7: Queues                   40
Simulation

 From the software point of view, simulation is a big loop
that executes a set of rules
 Example: Rules for a bank system:
– R1:
• If a customer arrives, he/she gets in line
– R2:
• If the teller is free, and if there is anyone waiting, the first customer in
line leaves the line and advances to the teller window
• Service time should be known
– R3:
• If a customer is at the teller’s window, the time remaining for that
customer to be serviced is decremented
– R4:
• If there are customers in line, an additional time that they have
remained in the queue is recorded
M. Malaty - OCC - CS?CIS - CS200 - Data Structures      Chapter 7: Queues                      41
Simulation

 Definitions:
– Event-Driven Simulation:
• Simulation that uses events to evaluate the performance
• Simulated time is advanced to time of the next event
• Events are generated by a mathematical model that is
based on statistics and probability
– Time-Driven Simulation:
• Simulation depending on using random numbers to
simulate the time of events' occurrences
• Simulated time is advanced by a single time unit
• The time of an event is determined randomly and
compared with a simulated clock
M. Malaty - OCC - CS?CIS - CS200 - Data Structures      Chapter 7: Queues   42
Simulation

Note:
– Techniques for generating events to reflect the real
world require a good deal of mathematics and will
not be discussed in this class
Simulated time:
– The time from the beginning (Time = 0) to the end of
measurement
External event:
– Events that the system can't control
Internal events:
– Events that can be calculated by the system
M. Malaty - OCC - CS?CIS - CS200 - Data Structures      Chapter 7: Queues   43
Application: Bank Simulation

Ms. Simpson, President of First City Bank of
Springfield, has heard her customers complain about
how long they have to wait for service. Because she
fears that they may move their account to another
bank, she is considering whether to hire a second
teller.

Ms. Simpson needs an approximation of the average
time that customer has to wait for service from the
only teller available.

How can she get this information?
M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues   44
Application: Bank Simulation

– How many teller does the bank employ?
– How often customers arrive?
– What is the average time does a customer have to
wait before receiving service?
– How many employers should be hired to improve the
performance of the bank?

M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues   45
Application: Bank Simulation

Events:
– Customer arrivals
• External events
– Customer departures when completing transaction
• Internal events
Statistics needed:
– Average time a customer waits for service
= Total waiting time for all customers / Number of customers

M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues   46
Application: Bank Simulation

Assumptions:
– The list of all arrival events, is already
available for use in a file in the form of pairs
(Arrival time, Transaction time)
– The events are given in
• Ascending order by arrival time
– Departure events are not included in the file
since it could be calculated

M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues   47
Application: Bank Simulation

Event list:
– Keeps track of arrival and departure events
that will occur but have not occurred yet
– Contains at most one arrival event and one
departure event
– Departure time:
Departure time = arrival time + transaction time
– Waiting time:
• Time elapsed between arrival & start of
transaction
M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues   48
Application: Bank Simulation

Required operations:
Required data structures:
– A queue representing customers in line:
• will contain arrival time & duration of transaction
– A list representing the event list

M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues   49
Application: Bank Simulation

 The new customer always enters the queue, even if the
queue is empty
 When a customer is ready for service, the following
operations take place:
– Remove the customer from the queue
– Delete the arrival event for the new customer from the event
list
– Insert the departure event into the event list
 The place that an event is inserted in the event list
depends on the relative time of that event

M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues   50
Application: Bank Simulation

 Example:
– If the file contains the following:
Arrival time                                 Transaction duration
20                                                 5
22                                                 4
23                                                 2
30                                                 3
– The result of simulation will be as follows:
Time                                 Event
20                                  Cust1 enters bank & begins transaction
22                                  Cust2 enters bank & stands at line end
23                                  Cust3 enters bank & stands at line end
25                                  Cust1 departs & Cust2 begins transaction
29                                  Cust2 departs & Cust3 begins transaction
30                                  Cust4 enters bank & stands at line end
31                                  Cust3 departs & Cust4 begins transaction
34                                  Cust4 departs
M. Malaty - OCC - CS?CIS - CS200 - Data Structures            Chapter 7: Queues            51
Application: Bank Simulation

 Pseudocode (First Draft)
– Increments the time by increments of 1
//Initialize
currentTime = 0
Initialize the line to “no customers”
while (currentTime <= time of final event)
{ if (an arrival event occurs at time currentTime)
process the arrival event
if (a departure event occurs at time currentTime)
process the departure event
// when both arrival & departure occur at the same time,
// arbitrarily process the arrival event first
++currentTime
} // end while
M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues   52
Application: Bank Simulation

 Pseudocode (Second Draft)
– Considers only times of relevant events (arrival & departure)
//Initialize the line to “no customers”
while (event remain to be processed)
{ currentTime = time of next event
if (event is an arrival event )
process the arrival event
else
process the departure event
// when both arrival & departure events occur at the same time,
// arbitrarily process the arrival event first
} // end while

M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues          53
Application: Bank Simulation

 Observations
– Each arrival event generates exactly one departure event
– We cannot generate a departure event for a given arrival
event independent of other events.
– If we read the whole file of events, we will need to save the
calculations that the simulation performs
• => It is better to store one arrival and one departure event at a time
and let the simulation perform the calculation step-by-step
– The event list will contain at most one event of each kind

M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues                   54
Application: Bank Simulation

Observations
– For arrival events
• Keep the earliest unprocessed arrival event
• When the event is processed, replace it with the next
unprocessed event
– For departure events
• The next departure event depends on the customer
currently served
• Evaluate time of next departure from
– Time service begins
– Length of transaction
• As soon as a customer begins service, we place the
corresponding departure event corresponding to this
customer in the event list
M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues   55
Application: Bank Simulation

Example:
– Event list structure

A   Arrival time           Transaction time
Arrival event

D                 Departure time
Departure event

M. Malaty - OCC - CS?CIS - CS200 - Data Structures        Chapter 7: Queues                        56
Application: Bank Simulation

Event list
– Considers only times of relevant events
(arrival & departure)
– We need to maintain an event list that
contains future events
– A file will contain all the events and they will
be brought to the event list as needed

M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues   57
Application: Bank Simulation

 Possible event list configurations
– Initially:
• One arrival event A read from the input file
– Event list: A
– Generally:
• Two events (Arrival A & Departure D)
– Event list:             A D (next event is arrival)
or                    D A (next event is departure)
– Special cases:
• If Arrival event is first and, after it is processed, the file is empty (eof):
– Event list: D (input has been exhausted)
• If Departure event is first and teller line is empty:
– Event list: A (departure leaves teller line empty)
 Events are inserted in the event list the beginning or at the end
depending on their arrival times
M. Malaty - OCC - CS?CIS - CS200 - Data Structures             Chapter 7: Queues             58
Application: Bank Simulation

 Algorithm for arrival events
// Update the event list
Delete the arrival event for customer C from the event list
if ( new customer C begins transaction immediately)
Insert a departure event for customer C into the event list
(time of event = current time + transaction length)
If (not at the end of the input file)
Read a new arrival event & add it to the event list
(time of event = time specified in file)

M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues   59
Application: Bank Simulation

Algorithm for departure events
// Update the line
Delete customer at front of queue
If (the queue is not empty)
Current front customer begins transaction
// Update the event list
Delete the departure event from the event list
If (the queue is not empty)
Insert into the event list the departure event for the customer now
at the front of the queue
(time of event = current time + transaction length)

M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues           60
Application: Bank Simulation

Final simulation algorithm
+simulate ( )
// perform the simulation
Create an empty queue bankQueue to represent the bank line
Create an empty event list eventList
Get the first arrival event from the input file & place it in eventList
while ( eventList is not empty)
{
newEvent = first event in eventList
if (newEvent is arrival event)
processArrival(newEvent, arrivalFile, eventList,
bankQueue)
else
processDeparture(newEvent, eventList, bankQueue)
} // end while
M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues             61
Application: Bank Simulation

 Final simulation algorithm
+processArrival( in arrivalEvent: Event,           in arrivalFile: File,
inout anEventList: EventList, inout bankQueue: Queue)
// Processes an arrival event
atFront = bankQueue.isEmpty() // present queue status
// Update bankQueue by inserting the customer, as described in arrivalEvent,
// into the queue
bankQueue.enqueue ( arrivalEvent)
// Update the event list
Delete arrivalEvenet from anEventList
If (atFront)
{ // The line was empty, new customer at the front of
// the line begins transaction immediately
Insert into anEventList a departure event corresponding to the new customer
with currentime = currentTime + transaction length
} // end if
If (not at end of input file)
{ Get the next arrival event from arrivalFile
Add the event – with time as specified in input file – to anEventList
} // end if
M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues                     62
Application: Bank Simulation

 Final simulation algorithm
+processDeparture( in departureEvent: Event,
inout anEventList: EventList, inout bankQueue: Queue)
// Processes an departure event
// Update the line by deleting the front customer
bankQueue.dequeue ( )
// Update the event list
Delete departureEvent from anEventList

If (! bamkQueue.isEmpty)
{ // Customer at front of line begins transaction
Insert into the event list the departure event corresponding to
the customer now at the front of the line & has
currentTime = currentTime + transaction length
} // end if

M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues                63
Application: Bank Simulation

+createEventList() // Create an empty event list
+destroyEventList() // Destroys an event list
+isEmpty(): boolean {query}
// Determines whether an event list is empty
+insert(in anEvent: Event)
// Inserts anEvent into an event list so that events are ordered
// by time. If an arrival event & departure event have the same
// time, the arrival event precedes the departure event
+delete()        // Deletes the first event from an event list
+retrieve( out anEvent: Event)
// Sets anEvent to the first event in an event list

M. Malaty - OCC - CS?CIS - CS200 - Data Structures   Chapter 7: Queues    64
Summary

The definition of the queue operations gives the
ADT queue first-in, first-out (FIFO) behavior
A pointer-based implementation of a queue uses
either
– A linear linked list with a head reference and a tail
reference
A circular array eliminates the problem of
rightward drift in an array-based implementation
M. Malaty - OCC - CS?CIS - CS200 - Data Structures     Chapter 7: Queues   65
Summary

 Distinguishing between the queue-full and queue-empty
conditions in a circular array
– Count the number of items in the queue
– Use a isFull flag
– Leave one array location empty
 Simulations
– In a time-driven simulation
• Time is advanced by a single time unit
– In an event-driven simulation
• Time is advanced to the time of the next event
– To implement an event-driven simulation, you maintain an
event list that contains events that have not yet occurred

M. Malaty - OCC - CS?CIS - CS200 - Data Structures     Chapter 7: Queues   66

```
DOCUMENT INFO
Shared By:
Categories:
Stats:
 views: 72 posted: 12/16/2010 language: English pages: 66
Description: Application for Bank Teller Position document sample