CS210- Lecture 5 Jun 9, 2005 Agenda Queues

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Queues and Linked Lists
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,
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
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?
Queues.
Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.
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.
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.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
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.
STACKS AND QUEUES 1. Outline 2  Stacks  Queues.
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)
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.
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
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.
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.
Stacks and Queues. What is a Stack?  Stack is a data structure in which data is added and removed at only one end called the top  Examples of stacks.
Stacks and Queues MR. Mohammad Alqahtani.
© 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,
Elementary Data Structures
Review Array Array Elements Accessing array elements
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.
Data Structure By Amee Trivedi.
Queues Rem Collier Room A1.02
Sequences 8/1/2018 4:38 AM Linked Lists Linked Lists.
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Sequences 8/2/ :16 AM Linked Lists Linked Lists.
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Stacks and Queues.
Queues Queues Queues.
Queues Mohammad Asad Abbasi Lecture 5
CMSC 341 Lecture 5 Stacks, 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.
Queues.
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.
Linked Lists.
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
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
Queues Jyh-Shing Roger Jang (張智星)
Copyright © Aiman Hanna All rights reserved
CS210- Lecture 6 Jun 13, 2005 Announcements
Stacks, Queues, and Deques
Stacks and Linked Lists
Data Structures & Programming
Presentation transcript:

CS210- Lecture 5 Jun 9, 2005 Agenda Queues Array Based Implementation of Queues Applications of Queues Linked Lists 2/24/2019 CS210-Summer 2005, Lecture 5

Queues Queue is a container of objects that are inserted and removed according to the first-in first-out (FIFO) principle. Objects can be inserted into a queue at any time, but only the objects that has been in the queue the longest can be removed. Objects enter the queue at the rear and are removed from the front. 2/24/2019 CS210-Summer 2005, Lecture 5

Queue ADT Queue ADT supports following methods: enqueue(o): Insert object o at the rear of the queue. Input: Object o Output: None dequeue(): Remove and return from the queue the object at the front. Error occurs if the queue is empty. Input: None Output: Front object 2/24/2019 CS210-Summer 2005, Lecture 5

Queue ADT size(): Return the number of objects in the queue Input: none Output: Integer (total number of objects) isEmpty(): Return a boolean indicating if the queue is empty. Input: None Output: boolean (true if queue is empty, false otherwise) 2/24/2019 CS210-Summer 2005, Lecture 5

Queue ADT front(): Return but do not remove, the front object in the queue. Error occurs if the queue is empty. Input: None Output: Front object Exceptions Attempting the execution of dequeue or front on an empty queue throws an EmptyQueueException. 2/24/2019 CS210-Summer 2005, Lecture 5

Example Operation Output Q enqueue(5) – (5) enqueue(3) – (5, 3) dequeue() 5 (3) enqueue(7) – (3, 7) dequeue() 3 (7) front() 7 (7) dequeue() 7 () dequeue() “error” () isEmpty() true () enqueue(9) – (9) enqueue(7) – (9, 7) size() 2 (9, 7) enqueue(3) – (9, 7, 3) enqueue(5) – (9, 7, 3, 5) dequeue() 9 (7, 3, 5) 2/24/2019 CS210-Summer 2005, Lecture 5

Applications of Queues Waiting lists: Stores, Theaters etc. Access to Shared resources (printer). Multiprogramming 2/24/2019 CS210-Summer 2005, Lecture 5

Queue Interface Java interface corresponding to our Queue ADT public interface Queue { public int size(); public boolean isEmpty(); public Object front() throws EmptyQueueException; public void enqueue(Object o); public Object dequeue() throws EmptyQueueException; } Java interface corresponding to our Queue ADT Requires the definition of class EmptyQueueException No corresponding built-in Java class 2/24/2019 CS210-Summer 2005, Lecture 5

A Simple Array based implementation We must decide how we are going to keep track of the rare and front of the queue. One possibility is to adapt the approach we used in stack. Q[0] be the front. Not efficient as need to move all elements forward one array cell each time we perform a dequeue operation. Dequeue operation will take O(n) time. 2/24/2019 CS210-Summer 2005, Lecture 5

A Simple Array based implementation Two variables keep track of the front and rear f index of the front element r index immediately past the rear element Array location r is kept empty. Initially f = r = 0 (initially empty) When we remove an element from front of the queue, we increment f to index the next cell. 2/24/2019 CS210-Summer 2005, Lecture 5

A Simple Array based implementation When we add an element, we store it in cell Q[r] and increment r to index the next available cell in Q. This allows us to implement all methods in O(1) time. There is still a problem with this approach. 2/24/2019 CS210-Summer 2005, Lecture 5

A Simple Array based implementation Q f, r Q 4 enqueue(4) f r Q dequeue(4) f, r Q 4 enqueue(4) f r If we do enqueue(4) and dequeue(4) 7 times. We would have f = r = 7. Next time we will get ArrayOutOfBoundsException. 2/24/2019 CS210-Summer 2005, Lecture 5

A Simple Array based implementation Solution: Use an array of size N in a circular fashion We let the f and r indices wrap around the end of Q. normal configuration Q 1 2 r f wrapped-around configuration Q 1 2 f r 2/24/2019 CS210-Summer 2005, Lecture 5

A Simple Array based implementation We compute the increment of f and r as (f + 1) mod N (r + 1) mod N Another problem: If we enqueue N objects into Q without dequeuing any of them. We would have f = r, which is the same condition that occurs when the queue is empty. No difference between full queue and empty queue. 2/24/2019 CS210-Summer 2005, Lecture 5

A Simple Array based implementation Solution: Insist that Q can never hold more than N – 1 objects. Size ? (N – f + r) mod N. When list is empty f = r = 0 Size = N mod N = 0 When list is full i.e N – 1 elements f = 0 r = N – 1 Size = (N – 0 + N – 1) mod N = N - 1 2/24/2019 CS210-Summer 2005, Lecture 5

Queue Operations We use the modulo operator (remainder of division) Algorithm size() return (N - f + r) mod N Algorithm isEmpty() return (f = r) Q 1 2 r f Q 1 2 f r 2/24/2019 CS210-Summer 2005, Lecture 5

Queue Operations Operation enqueue throws an exception if the array is full This exception is implementation-dependent Algorithm enqueue(o) if size() = N  1 then throw FullQueueException else Q[r]  o r  (r + 1) mod N Q 1 2 r f Q 1 2 f r 2/24/2019 CS210-Summer 2005, Lecture 5

Queue Operations Operation dequeue throws an exception if the queue is empty This exception is specified in the queue ADT Algorithm dequeue() if isEmpty() then throw EmptyQueueException else o  Q[f] f  (f + 1) mod N return o Q 1 2 r f Q 1 2 f r 2/24/2019 CS210-Summer 2005, Lecture 5

Analyzing the Array Based Queue Implementation Method Time size O(1) isEmpty O(1) front O(1) enqueue O(1) dequeue O(1) 2/24/2019 CS210-Summer 2005, Lecture 5

Drawbacks of Array based implementation Array based implementation must assume a fixed upper bound N on the ultimate size of the stack. An application may actually need more or less queue capacity than this. Still in cases where we have a good estimate on the number of items to be stored in the queue, the array based implementation is hard to beat. 2/24/2019 CS210-Summer 2005, Lecture 5

Round Robin Schedulers We can implement a round robin scheduler using a queue, Q, by repeatedly performing the following steps: e = Q.dequeue() Service element e Q.enqueue(e) The Queue Shared Service 1 . Deque the next element 3 Enqueue the serviced element 2 Service the 2/24/2019 CS210-Summer 2005, Lecture 5

The “Pass the Pillow” game A group of n children sit in a circle passing a pillow around the circle. Children continue passing the pillow until a leader rings a bell, at which point the child holding the pillow must leave the game after handling the pillow to the next child in the circle. The process is then continued until there is only one child remaining, who is declared the winner. If leader always rings the bell after the pillow has been passed k times, then determining the winner for a given list of children in known as “Josephus Problem”. 2/24/2019 CS210-Summer 2005, Lecture 5

Singly Linked Lists A singly linked list is a concrete data structure consisting of a sequence of nodes Each node stores element link to the next node next node elem head  A B C D 2/24/2019 CS210-Summer 2005, Lecture 5

Singly Linked Lists The next reference inside a node can be viewed as a link or pointer to another node. Moving from one node to another by following a next reference is known as link hopping or pointer hopping. The first and last node are usually called head and tail of the list resp. Tail has a null next reference which indicates the termination of the list. 2/24/2019 CS210-Summer 2005, Lecture 5

Singly linked list vs. Arrays Like an array, a singly linked list keeps the elements in a certain linear order, which is determined by the chain of next links between the nodes. Unlike an array, a singly linked list does not have a predetermined fixed size, and uses space proportional to the number of its elements. 2/24/2019 CS210-Summer 2005, Lecture 5

Inserting at the head 2/24/2019 CS210-Summer 2005, Lecture 5

Inserting at the head Allocate a new node Insert new element Have new node point to old head Update head to point to new node 2/24/2019 CS210-Summer 2005, Lecture 5

Removing at the head Update head to point to next node in the list Allow garbage collector to reclaim the former first node 2/24/2019 CS210-Summer 2005, Lecture 5

Inserting at the tail 2/24/2019 CS210-Summer 2005, Lecture 5

Inserting at the tail Allocate a new node Insert new element Have new node point to null Have old last node point to new node Update tail to point to new node 2/24/2019 CS210-Summer 2005, Lecture 5

Removing at tail Removing at the tail of a singly linked list is not efficient! There is no constant-time way to update the tail to point to the previous node. 2/24/2019 CS210-Summer 2005, Lecture 5

Analyzing the Singly Linked List Method Time Inserting at the head O(1) Inserting at the tail O(1) Deleting at the head O(1) Deleting at the tail O(n) 2/24/2019 CS210-Summer 2005, Lecture 5