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

Slides:



Advertisements
Similar presentations
§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
Advertisements

Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Sample PMT online… Browse 1120/sumII05/PMT/2004_1/ 1120/sumII05/PMT/2004_1/
Prefix, Postfix, Infix Notation
Arithmetic Expressions Infix form –operand operator operand 2+3 or a+b –Need precedence rules –May use parentheses 4*(3+5) or a*(b+c)
E.G.M. Petrakislists, stacks, queues1 Stacks Stack: restricted variant of list –Elements may by inserted or deleted from only one end  LIFO lists –Top:
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
 Balancing Symbols 3. Applications
CS 240Chapter 6 - StacksPage 21 Chapter 6 Stacks The stack abstract data type is essentially a list using the LIFO (last-in-first-out) policy for adding.
Chapter 6: Stacks STACK APPLICATIONS STACK IMPLEMENTATIONS CS
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
Arithmetic Expressions
Doubly-Linked Lists Same basic functions operate on list Each node has a forward and backward link: What advantages does a doubly-linked list offer? 88.
Stacks & Queues Infix Calculator CSC 172 SPRING 2002 LECTURE 5.
Infix to postfix conversion Process the tokens from a vector infixVect of tokens (strings) of an infix expression one by one When the token is an operand.
Infix, Postfix, Prefix.
1 CSCD 326 Data Structures I Infix Expressions. 2 Infix Expressions Binary operators appear between operands: W - X / Y - Z Order of evaluation is determined.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Objectives of these slides:
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Stack Applications.
Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Lists Lecture 16 Fri, Mar 3, Topics Lists List ADT Attributes Constructors Destructor Inspectors Mutators Facilitators Operators Other List Functions.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
Lecture Objectives  To understand how Java implements a stack  To learn how to implement a stack using an underlying array or linked list  Implement.
Reverse Polish Notation Written by J.J. Shepherd.
Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
ADT Stack & Queue - Case Studies TCP1201: 2013/2014.
CSC 172 DATA STRUCTURES. A TALE OF TWO STRUCTURES.
Mark Redekopp David Kempe
Stacks Access is allowed only at one point of the structure, normally termed the top of the stack access to the most recently added item only Operations.
Queues Chapter 8 (continued)
CC 215 Data Structures Queue ADT
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
Homework 4 questions???.
Stacks Chapter 7 introduces the stack data type.
Objectives In this lesson, you will learn to: Define stacks
CSC 172 DATA STRUCTURES.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stacks Stack: restricted variant of list
PART II STACK APPLICATIONS
Binary Trees Lecture 36 Wed, Apr 21, /21/2018 Binary Trees.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks, Queues, and Deques
Stacks and Queues 1.
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Lecture 15 Section 6.1 Mon, Feb 26, 2007
Stack.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
CSCS-200 Data Structure and Algorithms
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
5.3 Implementing a Stack Chapter 5 – The Stack.
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Disadvantages 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 – 1 5/3/2019 Queues

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

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

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

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

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

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