Csc 2720 Data structure Instructor: Zhuojun Duan

Slides:



Advertisements
Similar presentations
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.
Advertisements

Topic 9 The Queue ADT.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
COMPSCI 105 SS 2015 Principles of Computer Science Queues.
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Circular Arrays Neat trick: use a circular array to insert and remove items from a queue in constant time The idea of a circular array is that the end.
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
Cmpt-225 Queues. A queue is a data structure that only allows items to be inserted at the end and removed from the front Queues are FIFO (First In First.
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Queue, Deque, and Priority Queue Implementations Chapter 11 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Queues by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data Structures.
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
The Abstract Data Type Queue A queue New items enter at the back, or rear, of the queue Items leave from the front of the queue First-in, first-out (FIFO)
LECTURE 27: DEQUES CSC 212 – Data Structures. Roses are red and violets are blue Implement push, pop, & top And you’re a Stack too! Stack & ADT Memory.
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
© 2011 Pearson Addison-Wesley. All rights reserved 8 B-1 Chapter 8 (continued) Queues.
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
Chapter 7 A Queues. © 2004 Pearson Addison-Wesley. All rights reserved7 A-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear,
Queue Section 3 6/12/ The Abstract Data Type Queue A queue is a list from which items are deleted from one end (front) and into which items are.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
The Queue ADT.
Queues Chapter 8 (continued)
Review Array Array Elements Accessing array elements
Chapter 7 Queues.
Data Abstraction & Problem Solving with C++
Week 4 - Monday CS221.
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.
ADT description Implementations
Double-Ended Queues Chapter 5.
COSC160: Data Structures: Lists and Queues
Marcus Biel, Software Craftsman
CC 215 Data Structures Queue ADT
Queues.
Chapter 15 Lists Objectives
CENG 213 Data Structure Queue 7/2/2018.
Queue.
Stacks and Queues.
Queues Queues Queues.
The Abstract Data Type Queue
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Priority Queue.
Queues, Deques and Priority Queues
Queues.
Queues, Deques and Priority Queues
Queue.
Stacks and Queues.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Revised based on textbook author’s notes.
CSC 143 Queues [Chapter 7].
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Figure 7.1 Some queue operations. Figure 7.1 Some queue operations.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Chapter 8 Queues © 2006 Pearson Addison-Wesley. All rights reserved.
Copyright © Aiman Hanna All rights reserved
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Queues Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks and Queues.
Queues.
Queues, Deques, and Priority Queues
Queue, Deque, and Priority Queue Implementations
Presentation transcript:

Csc 2720 Data structure Instructor: Zhuojun Duan Queue Csc 2720 Data structure Instructor: Zhuojun Duan

Outline Introduction of Queue Implementation Applications Queue in JAVA Library

Preliminary

Conceptual View of a Queue Queue: a collection whose elements are added at one end (the rear or tail of the queue) and removed from the other end (the front or head of the queue) A queue is a FIFO (first in, first out) data structure Applications in computer science: Printer queue (e.g. printer in MC 235) Keyboard input buffer GUI event queue (click on buttons, menu items) To encode messages (more on this later) …

Queue operations boolean isEmpty(); // Determines whether a queue is empty boolean enqueue(newItem); // Adds newItem at the back of a queue. boolean dequeue(); // Retrieves and removes the front of a queue. boolean dequeueAll(); // Removes all items from a queue object peek(); //Retrieves the front of a queue.

Conceptual View of a Queue Adding an element Front of queue New element is added to the rear of the queue Rear of queue

Conceptual View of a Queue Adding an element Front of queue Rear of queue

Conceptual View of a Queue Removing an element Front of queue Element is removed from the front of the queue Rear of queue

Conceptual View of a Queue Removing an element New front element of queue Element is removed from the front of the queue Rear of queue

Recognizing Palindromes A palindrome A string of characters that reads the same from left to right as its does from right to left Example: Anna, Civic, Kayak, Level, Madam To recognize a palindrome, a queue can be used in conjunction with a stack A stack can be used to reverse the order of occurrences A queue can be used to preserve the order of occurrences

Recognizing Palindromes A recognition algorithm for palindromes As you traverse the character string from left to right, insert each character into both a queue and a stack Compare the characters at the front of the queue and the top of the stack Figure: The results of inserting a string into both a queue and a stack

Outline Introduction of Queue Implementation Applications Queue in JAVA Library

Implementations of Queue A queue can have either An array-based implementation Array-based Circular array- based A reference-based implementation (linked list )

Array Implementation of a Queue First Approach First Approach: Use an array in which index 0 represents one end of the queue (the front) : front is fixed Integer value rear represents the last element in the array Discussion: What is the challenge with this approach?

An Array Implementation of a Queue First Approach front rear 1 2 3 4 … aq 1 2 3 4 front A queue aq containing four elements 3 rear

Adding an Element in queue First Approach front rear rear 1 2 3 4 … aq 1 2 3 4 5 front Add new element for queue aq 5 4 3 rear Element is added at the array location given by the index of rear+1, and then rear is incremented.

Removing an Element from queue First Approach front rear 1 2 3 4 … aq 1 2 3 4 5 front Step 1: Element is removed from array location 0 4 3 rear Step 2: remaining elements are shifted forward one position in the array, and then rear is decremented.

Removing an Element from queue First Approach front rear 1 2 3 4 … aq 2 3 4 5 front Step 1: element is removed from array location 0 3 4 3 rear Step 2: remaining elements are shifted forward one position in the array, and then rear is decremented.

Array Implementation of a Queue First Approach How to initialize the value of front and rear: Set front=0, rear=-1; How to decide the queue is empty? if rear < front How to decide the queue is full? if rear==MAX_QUEUE-1

Array Implementation of a Queue Second Approach (circular array ) Drawback of first approach: Shifting cost Circular Arrays can be used to implement the queue Use a circular array to insert and remove items from a queue without shifting The idea of a circular array is that the end of the array “wraps around” to the start of the array 1 3 2 4 5 6 7

Array Implementation of a Queue Second Approach (circular array ) //Java Code Queue q = new Queue(); q.enqueue(6); front = count = 1 6 1 2 3 4 5 rear = 5 insert item at (rear + 1 ) % queue.length=6%6=0

Array Implementation of a Queue Second Approach (circular array ) //Java Code Queue q = new Queue(); q.enqueue(6); q.enqueue(4); q.enqueue(7); q.enqueue(3); q.enqueue(8); front = count = 1 5 2 4 3 6 4 7 3 8 1 2 3 4 5 rear = 4 3 2 1

Array Implementation of a Queue Second Approach (circular array ) //Java Code Queue q = new Queue(); q.enqueue(6); q.enqueue(4); q.enqueue(7); q.enqueue(3); q.enqueue(8); q.dequeue(); //front = 1 q.dequeue(); //front = 2 q.enqueue(9); front = 1 2 count = 5 3 4 4 6 4 7 3 8 9 1 2 3 4 5 rear = 5 4 make front = (0 + 1) % 6 = 1 make front = (1 + 1) % 6 = 2

Array Implementation of a Queue Second Approach (circular array ) //Java Code Queue q = new Queue(); q.enqueue(6); q.enqueue(4); q.enqueue(7); q.enqueue(3); q.enqueue(8); q.dequeue(); //front = 1 q.dequeue(); //front = 2 q.enqueue(9); q.enqueue(5); front = 2 count = 5 4 5 7 3 8 9 1 2 3 4 5 rear = 5 insert at (rear + 1) % 6 = (5 + 1) % 6 = 0

Array Implementation of a Queue Second Approach (circular array ) Illustration by a circle A circular array eliminates the problem of rightward drift

Array Implementation of a Queue Second Approach (circular array ) Illustration by a circle

Array Implementation of a Queue Second Approach (circular array ) Question: how to distinguish between queue-full and queue-empty conditions ? front and back cannot be used to distinguish between queue-full and queue-empty conditions Count can be used to do it.

Array Implementation of a Queue Second Approach (circular array ) front and back cannot be used to distinguish between queue-full and queue-empty conditions Figure: front passes back when the queue becomes empty

Array Implementation of a Queue Second Approach (circular array ) front and back cannot be used to distinguish between queue-full and queue-empty conditions Figure: back catches up to front when the queue becomes full

Array Implementation of a Queue Second Approach (circular array ) Notes for circular array based queue: To detect queue-full and queue-empty conditions Keep a count of the queue items To initialize the queue, set front to 0 rear(back) to MAX_QUEUE – 1 count to 0 Inserting into a queue rear = (rear+1) % MAX_QUEUE; items[rear] = newItem; ++count;

Array Implementation of a Queue Second Approach (circular array ) Notes for circular array based queue: Deleting from a queue front = (front+1) % MAX_QUEUE; --count;

Implementations of Queue A queue can have either An array-based implementation Array-based Circular array- based A reference-based implementation (linked list )

Queue: Linked List Implementation Removing items from the front of the queue is straightforward But we need to insert items at the back of the queue in constant time So cannot traverse through the list By using an additional reference (and a little administration) we can keep track of the node at the back of the queue

List Queue Example singly linked list //Java Code Queue q = new Queue(); q.enqueue(6); 6 front rear

List Queue Example 6 front 4 rear //Java Code Queue q = new Queue(); singly linked list //Java Code Queue q = new Queue(); q.enqueue(6); q.enqueue(4); 6 front 4 rear

List Queue Example 6 front 4 rear 7 //Java Code Queue q = new Queue(); singly linked list //Java Code Queue q = new Queue(); q.enqueue(6); q.enqueue(4); q.enqueue(7); 6 front 4 rear 7

List Queue Example 6 front 4 rear 7 3 //Java Code singly linked list //Java Code Queue q = new Queue(); q.enqueue(6); q.enqueue(4); q.enqueue(7); q.enqueue(3); 6 front 4 rear 7 3

List Queue Example 6 front 4 rear 7 3 //Java Code singly linked list //Java Code Queue q = new Queue(); q.enqueue(6); q.enqueue(4); q.enqueue(7); q.enqueue(3); q.dequeue(); 6 front 4 rear 7 3

Queue: Circular Linked List Implementation Possible implementations of a queue A circular linked list with one external reference A reference to the back Figure: A reference-based implementation of a queue: b) a circular linear linked list with one external reference

Queue: Circular Linked List Implementation Figure : Inserting an item into a nonempty queue

Queue: Circular Linked List Implementation Figure : Inserting an item into an empty queue: a) before insertion; b) after insertion

Queue: Circular Linked List Implementation Figure : Deleting an item from a queue of more than one item

Queue: Linked List Implementation How to decide the queue is empty if the queue is implemented by linked list? Singly linked list implementation : rear==null; Circular Linked List Implementation lastNode==null;

Outline Introduction of Queue Implementation Applications Queue in JAVA Library

Applications

Application Simulation A technique for modeling the behavior of both natural and human-made systems Goal Generate statistics that summarize the performance of an existing system Predict the performance of a proposed system Example A simulation of the behavior of a bank

Application: Simulation Figure : A blank line at at time a) 0; b) 12

Application: Simulation Figure: A blank line at at time c) 20; d) 38

Outline Introduction of Queue Implementation Applications Queue in JAVA Library

Interface Queue in Java Java has a queue interface called Queue Derived from interface Collection

Interface Queue in Java Java has a queue interface called Queue Derived from interface Collection Known Implementing Classes: AbstractQueue, ArrayBlockingQueue, ArrayDeque, ConcurrentLinkedDeque, C oncurrentLinkedQueue, DelayQueue, LinkedBlockingDeque, LinkedBlockingQ ueue, LinkedList, LinkedTransferQueue, PriorityBlockingQueue, PriorityQueue,  SynchronousQueue Please reference: http://docs.oracle.com/javase/7/docs/api/java/util/Queue.html

Interface Deque in Java Deque = double-ended queue (pronounced “deck”) Allows us to insert and delete from either end Useful methods: addFirst, addLast, peekFirst, peekLast, getFirst, getLast, removeFirst, removeLast,… Thus, may function as both a stack and a queue Known Implementing Classes: ArrayDeque, ConcurrentLinkedDeque, LinkedBlockingDeque, LinkedList Please reference: http://docs.oracle.com/javase/7/docs/api/java/util/Deque.html

Comparing Implementations All of the implementations of the ADT queue mentioned are ultimately either Array based Reference based Fixed size versus dynamic size A statically allocated array Prevents the enqueue operation from adding an item to the queue if the array is full A resizable array or a reference-based implementation Does not impose this restriction on the enqueue operation

A Summary of Position-Oriented ADTs List Stack Queue Stacks and queues Only the end positions can be accessed Lists All positions can be accessed