H-SC COMS 262 - Lecture 30 Notes - Queues

Unformatted text preview:

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 2TopicsQueuesQueue ADTQueue implementation01/14/19 Queues 3QueuesA 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 QueuesImplement 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 InheritancePrivate 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 ConstructorsConstruct an empty queue.Queue();Queue(const Queue& q);Construct a copy of the specified queue.01/14/19 Queues 7InspectorsGet 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 8MutatorsEnqueue 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 9FacilitatorsRead 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 FunctionsDetermine whether the queue has a valid structure.void Validate() const;01/14/19 Queues 11Non-Member FunctionsRead 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 ImplementationChoose an appropriate List class as a base class. Good choices CircArrayList LinkedListwTail DoublyLinkedList CircLinkedList01/14/19 Queues 13Queue ImplementationBad choices ArrayList LinkedList Use private inheritance to enforce the Queue structure on the List.01/14/19 Queues 14Lecture 29 – Apr 3, 2002Implementation of queuesApplicationsEvaluating infix expressionsSimulating waiting lines01/14/19 Queues 15Implementation of Queue Member FunctionsExamplearrayqueue.hlinkedqueue.hQueueTest.cpp01/14/19 Queues 16Queue Application: Infix Expression EvaluationAn 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 NotationParentheses are often needed to indicate order of operation. Example: (3 + 4) * (5 + 6)Operators follow a precedence hierarchy.Example: 3 + 4 * 5 – 6 / 7Operators have left or right associativity.Example: 100 – 50 – 10 – 5 – 101/14/19 Queues 18Queue Application: Infix Expression EvaluationBegin 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 EvaluationIf 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 EvaluationIf 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 EvaluationAfter 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 LinesSpecify 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 LinesMaintain 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

H-SC COMS 262 - Lecture 30 Notes - Queues

Download Lecture 30 Notes - Queues
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 30 Notes - Queues and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 30 Notes - Queues 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?