Presentation is loading. Please wait.

Presentation is loading. Please wait.

Queues Lecture 30 Fri, Apr 2, 2004 5/3/2019 Queues.

Similar presentations


Presentation on theme: "Queues Lecture 30 Fri, Apr 2, 2004 5/3/2019 Queues."— Presentation transcript:

1 Queues Lecture 30 Fri, Apr 2, 2004 5/3/2019 Queues

2 Topics Queues Queue ADT Queue implementation 5/3/2019 Queues

3 Queues A queue is a List that operates under the principle “first in, first out” (FIFO). New elements are enqueued into the queue. Old elements are dequeued from the queue. To enforce the FIFO principle, we enqueue and dequeue at opposite ends. 5/3/2019 Queues

4 Implementation 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. 5/3/2019 Queues

5 Private 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. 5/3/2019 Queues

6 Queue Constructors Construct an empty queue.
Construct a copy of the specified queue. Queue(const Queue& q); 5/3/2019 Queues

7 Inspectors Get a copy of the element at the head of the queue.
T Head() const; Get the number of elements in the queue. int Size() const; Determine whether the queue is empty. bool Empty() const; 5/3/2019 Queues

8 Mutators Enqueue the specified value at the tail of the queue.
void Enqueue(const T& value); Dequeue and return the element at the head of the queue. T Dequeue(); Make the queue empty. void MakeEmpty(); 5/3/2019 Queues

9 Facilitators Read a queue from the specified input stream.
void Input(istream& in); Write a queue to the specified output stream. void Output(ostream& out) const; 5/3/2019 Queues

10 Other Member Functions
Determine whether the queue has a valid structure. void Validate() const; 5/3/2019 Queues

11 Non-Member Functions Read a queue from the specified input stream.
istream& operator>>(istream& in, Queue& q); Write a queue to the specified output stream. ostream& operator<<(ostream& out, const Queue& q); 5/3/2019 Queues

12 Queue Implementation Choose an appropriate List class as a base class.
Good choices CircArrayList LinkedListwTail DoublyLinkedList CircLinkedList 5/3/2019 Queues

13 Queue Implementation Bad choices ArrayList LinkedList Use private inheritance to enforce the Queue structure on the List. 5/3/2019 Queues

14 Implementation of Queue Member Functions
Example arrayqueue.h linkedqueue.h QueueTest.cpp 5/3/2019 Queues

15 Queue Applications Lecture 31 Mon, Apr 5, 2004 5/3/2019 Queues

16 Topics Evaluating infix expressions Simulating waiting lines 5/3/2019
Queues

17 Queue Application: Infix Expression Evaluation
An infix expression with one (binary) operator is written in the order: left-operand, operator, right-operand. Example: 5/3/2019 Queues

18 Disadvantages of Infix Notation
Parentheses are often needed to indicate order of operation. Example: (3 + 4) * (5 + 6) Operators follow a precedence hierarchy. Example: * 5 – 6 / 7 Operators have left or right associativity. Example: 100 – 50 – 10 – 5 – 1 5/3/2019 Queues

19 Queue 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. 5/3/2019 Queues

20 Queue 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. 5/3/2019 Queues

21 Queue 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. 5/3/2019 Queues

22 Queue 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.exe 5/3/2019 Queues

23 Queue 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. 5/3/2019 Queues

24 Queue 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 rates. 5/3/2019 Queues


Download ppt "Queues Lecture 30 Fri, Apr 2, 2004 5/3/2019 Queues."

Similar presentations


Ads by Google