Download presentation
Presentation is loading. Please wait.
Published byChastity Carter Modified over 6 years ago
1
Csc 2720 Data structure Instructor: Zhuojun Duan
Queue Csc Data structure Instructor: Zhuojun Duan
2
Outline Introduction of Queue Implementation Applications
Queue in JAVA Library
3
Preliminary
4
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) …
5
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.
6
Conceptual View of a Queue
Adding an element Front of queue New element is added to the rear of the queue Rear of queue
7
Conceptual View of a Queue
Adding an element Front of queue Rear of queue
8
Conceptual View of a Queue
Removing an element Front of queue Element is removed from the front of the queue Rear of queue
9
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
10
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
11
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
12
Outline Introduction of Queue Implementation Applications
Queue in JAVA Library
13
Implementations of Queue
A queue can have either An array-based implementation Array-based Circular array- based A reference-based implementation (linked list )
14
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?
15
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
16
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.
17
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.
18
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.
19
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
20
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
21
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
22
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
23
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
24
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
25
Array Implementation of a Queue
Second Approach (circular array ) Illustration by a circle A circular array eliminates the problem of rightward drift
26
Array Implementation of a Queue
Second Approach (circular array ) Illustration by a circle
27
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.
28
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
29
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
30
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;
31
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;
32
Implementations of Queue
A queue can have either An array-based implementation Array-based Circular array- based A reference-based implementation (linked list )
33
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
34
List Queue Example singly linked list
//Java Code Queue q = new Queue(); q.enqueue(6); 6 front rear
35
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
36
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
37
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
38
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
39
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
40
Queue: Circular Linked List Implementation
Figure : Inserting an item into a nonempty queue
41
Queue: Circular Linked List Implementation
Figure : Inserting an item into an empty queue: a) before insertion; b) after insertion
42
Queue: Circular Linked List Implementation
Figure : Deleting an item from a queue of more than one item
43
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;
44
Outline Introduction of Queue Implementation Applications
Queue in JAVA Library
45
Applications
46
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
47
Application: Simulation
Figure : A blank line at at time a) 0; b) 12
48
Application: Simulation
Figure: A blank line at at time c) 20; d) 38
49
Outline Introduction of Queue Implementation Applications
Queue in JAVA Library
50
Interface Queue in Java
Java has a queue interface called Queue Derived from interface Collection
51
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:
52
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:
53
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
54
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.