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