QueuesTopicsSlide 3Implementation of QueuesPrivate InheritanceQueue ConstructorsInspectorsMutatorsFacilitatorsOther Member FunctionsNon-Member FunctionsQueue ImplementationSlide 13Lecture 29 – Apr 3, 2002Implementation of Queue Member FunctionsQueue Application: Infix Expression EvaluationDisadvantages of Infix NotationSlide 18Slide 19Slide 20Slide 21Queue Application: Waiting LinesSlide 2301/14/19 Queues 1QueuesLecture 30Fri, Apr 2, 200401/14/19 Queues 2TopicsQueuesQueue ADTQueue implementation01/14/19 Queues 3QueuesA queue is a List that operates under the principle “first in, first out” (FIFO). New elements are enqu eued into the queue. Old elements are dequeued from the queue. To enforce the FIFO principle, we enqueue and dequeue at opposite ends.01/14/19 Queues 4Implementation of QueuesImplement a Queue as a subclass of a List class. Use PushFront() and PopBack(), or Use PushBack() and PopFront(). Choose a List class for which enqueuing and dequeuing will be efficient.01/14/19 Queues 5Private InheritancePrivate inheritance prevents violations of the FIFO principle. The Queue class has access to all public and protected List functions. The Queue class user has access only to the queue functions.01/14/19 Queues 6Queue ConstructorsConstruct an empty queue.Queue();Queue(const Queue& q);Construct a copy of the specified queue.01/14/19 Queues 7InspectorsGet a copy of the element at the head of the queue.T Head() const;int Size() const;bool Empty() const;Get the number of elements in the queue.Determine whether the queue is empty.01/14/19 Queues 8MutatorsEnqueue the specified value at the tail of the queue. void Enqueue(const T& value);T Dequeue();void MakeEmpty();Dequeue and return the element at the head of the queue.Make the queue empty.01/14/19 Queues 9FacilitatorsRead a queue from the specified input stream.void Input(istream& in);void Output(ostream& out) const;Write a queue to the specified output stream.01/14/19 Queues 10Other Member FunctionsDetermine whether the queue has a valid structure.void Validate() const;01/14/19 Queues 11Non-Member FunctionsRead a queue from the specified input stream.istream& operator>>(istream& in, Queue& q);ostream& operator<<(ostream& out, const Queue& q);Write a queue to the specified output stream.01/14/19 Queues 12Queue ImplementationChoose an appropriate List class as a base class. Good choices CircArrayList LinkedListwTail DoublyLinkedList CircLinkedList01/14/19 Queues 13Queue ImplementationBad choices ArrayList LinkedList Use private inheritance to enforce the Queue structure on the List.01/14/19 Queues 14Lecture 29 – Apr 3, 2002Implementation of queuesApplicationsEvaluating infix expressionsSimulating waiting lines01/14/19 Queues 15Implementation of Queue Member FunctionsExamplearrayqueue.hlinkedqueue.hQueueTest.cpp01/14/19 Queues 16Queue Application: Infix Expression EvaluationAn infix expression with one (binary) operator is written in the order: left-operand, operator, right-operand.Example: 3 + 401/14/19 Queues 17Disadvantages of Infix NotationParentheses are often needed to indicate order of operation. Example: (3 + 4) * (5 + 6)Operators follow a precedence hierarchy.Example: 3 + 4 * 5 – 6 / 7Operators have left or right associativity.Example: 100 – 50 – 10 – 5 – 101/14/19 Queues 18Queue Application: Infix Expression EvaluationBegin with an empty stack and an empty queue. Process the tokens from left to right according to the following rules. If the token is a number, Enqueue the token. If the token is a left parenthesis, Push the token onto the stack.01/14/19 Queues 19Queue Application: Infix Expression EvaluationIf the token is a right parenthesis, Pop tokens off the stack and enqueue them until A left parenthesis is popped. Discard the right and left parentheses.01/14/19 Queues 20Queue Application: Infix Expression EvaluationIf the token is an operator, Pop tokens off the stack and enqueue them until An operator of lower precedence is on top of the stack, or A left parenthesis is on top of the stack, or The stack is empty. Push the operator onto the stack.01/14/19 Queues 21Queue Application: Infix Expression EvaluationAfter processing the last token Pop all tokens off the stack and enqueue them. The queue now contains the expression in post-fix notation. Process the queue as a post-fix expression. Sample program - InfixEvaluator.exe01/14/19 Queues 22Queue Application: Waiting LinesSpecify the arrival rate (average time between arrivals). Specify the departure rate (average time between departures). New arrivals are enqueued. Departures leave the service window. If the queue is not empty, a customer is dequeued and steps up to the service window.01/14/19 Queues 23Queue Application: Waiting LinesMaintain statistics on Number of arrivals. Number of departures. Average time between arrivals. Average time between departures. Average time spent in the queue. Average queue size. Fraction of the time that the window is idle. Explore the relationship between these statistics and the arrival and departure
View Full Document