Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.

Slides:



Advertisements
Similar presentations
Queues and Linked Lists
Advertisements

1 Array-based Implementation An array Q of maximum size N Need to keep track the front and rear of the queue: f: index of the front object r: index immediately.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Queue & List Data Structures & Algorithm Abstract Data Types (ADTs) ADT is a mathematically specified entity that defines a set of its instances,
Queue ADT Operations Enqueue, Dequeue, Peek, isEmpty Implementation: Circular Array (Weiss), Linked List, Circular Array (text) Application: Level-Order.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
Lab 1: 1. Download all my programs in your computer under the same folder. 2. The tree shown in the following figure represents an expression: (((( 3 +
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only 
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
Stacks, Queues, and Deques
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Queues.
CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test.
Implementing and Using Stacks
Circular queue. Array-based Queue Use an array of size N in a circular fashion Three variables keep track of the front, rear, and size f index of the.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Stacks. week 2a2 Outline and Reading The Stack ADT (§4.1) Applications of Stacks Array-based implementation (§4.1.2) Growable array-based stack Think.
Stack and Queue.
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
Queues by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data Structures.
LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Stacks And Queues Chapter 18.
Lecture 3 Queues Queues1. queue: – Retrieves elements in the order they were added. – First-In, First-Out ("FIFO") – Elements are stored in order of insertion.
Queues CSCI 3333 Data Structures. Acknowledgement  Dr. Bun Yue  Mr. Charles Moen  Dr. Wei Ding  Ms. Krishani Abeysekera  Dr. Michael Goodrich  Dr.
Question of the Day  Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
CE 221 Data Structures and Algorithms
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
Parasol Lab, Dept. CSE, Texas A&M University
Welcome to CSCE 221 – Data Structures and Algorithms
Queue. The Queue ADT Insertions and deletions follow the first-in first-out scheme Insertions are at the rear of the queue and removals are at the front.
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
CS 221 Analysis of Algorithms Data Structures. Portions of the following slides are from  Goodrich and Tamassia, Algorithm Design: Foundations, Analysis.
Click to edit Master text styles Stacks Data Structure.
Circular Queues Maitrayee Mukerji. Queues First In – First Out (FIFO) The first element to be inserted is the first one to be retrieved Insertion at one.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
Queues1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,
Queues 5/11/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Using Queues: Coded Messages
Queues Queues Queues.
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/9/2018 6:32 PM Queues.
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Circular queue.
Queue.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
Queues 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Recall What is a Data Structure Very Fundamental Data Structures
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues Jyh-Shing Roger Jang (張智星)
Copyright © Aiman Hanna All rights reserved
CE 221 Data Structures and Algorithms
Stacks and Linked Lists
Presentation transcript:

Queues

… frontrear dequeueenqueue

Message queues in an operating system There are times that programs need to communicate with each other. Unix operating system provides message queue as one of the mechanisms to facilitate communication. Program #1 Program #2 front rear Send a message to the queue Take a message from the queue

The Queue Abstract Data Type

This table shows a series of queue operations and their effects. The queue is empty initially

A Queue Interface in Java

A Simple Array-Based Implementation To implement a queue with an array, we need: 1.An array of the size N 2.An index f for the front element 3.An index r for next empty slot or cell Q: … 012 r N - 1 f

As objects are enqueued and dequeued, the queue moves along in the array. For example: fr After enqueue: f r After dequeue: f r

When we increment f or r, we compute the result as (f + 1) mod N or (r + 1) mod N. In Java, the modulo operator is % (remainder operator). For example, if r = N - 1, then (r + 1) = N, therefore, (r + 1) mod N = 0 The value of r wraps around from N to 0. Q: … 0 12 rf N - 1

Initially, we assign r = f = 0, indicating that the queue is empty. During the process, we may meet a situation where r = f = i (0 < i < N), which also indicates the queue is empty. Now we assume that we enqueue N objects into Q without dequeueing any of them: Q: … 012 r f N - 1 Then, we have r = f = 0. But in this case, we have a full queue. In general, when r = f = i (0 < i < N), it is possible that we have a full queue.

Problem: r = f may indicate that the queue is empty or that the queue is full. Solution: when |r – f| = N – 1, report that the queue is full.

Algorithms: size(): return the number (N - f + r) mod N 012N-1N+r-1 If r  f, then r - f  0 N + (r - f )  N (N - f + r) mod N = - f + r = r - f If r 0 N - f + r = N – (f - r ) < N (N - f + r) mod N = N - f + r

Algorithms: isEmpty( ): return the result of the evaluation of the relationship f = r f r

Algorithms: front( ): if the queue is empty throw a QueueEmptyException else return element Q[f] Q: … 012 r N - 1 f

enqueue(o): if queue size is N - 1 throw a QueueFullException else store the object to Q[r] assign (r + 1) mod N to r Q: … 012 r N - 1 f (r + 1) mod N

dequeue( ): if queue size is empty throw a QueueEmptyException else save the element Q[f] to a variable temp make the element Q[f] a null object assign (f + 1) mod N to f Q: … 012 r N - 1 f (f + 1) mod N

Memory Allocation in Java

Data Structure Exercises 4.1