Docsity

Java Data Structures and Algorithms: Queues and Priority Queues, Slides of Data Structures and Algorithms

Toggle sidebar
bg1
Data Structures and Algorithms in Java 1/21
2. Stack and Queue
Part 2: Queue
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17

Partial preview of the text

Download Java Data Structures and Algorithms: Queues and Priority Queues and more Slides Data Structures and Algorithms in PDF only on Docsity!

2. Stack and Queue

Part 2: Queue

Queues

Objectives

  • (^) Queues
  • (^) Priority Queues
  • (^) Queue interface in java.util

Operations on a queue

4

  • (^) The following operations are needed to properly manage a queue: - (^) clear() — Clear the queue - (^) isEmpty() — Check to see if the queue is empty - (^) enqueue(el) — Put the element el at the end of the queue - (^) dequeue() — Take the first element from the queue - (^) front() — Return the first element in the queue without removing it
  • (^) Exceptions
    • (^) Attempting the execution of dequeue or front on an empty queue throws an EmptyQueueException sometimes the peek() is named instead of front().

Data Structures and Algorithms in Java 5

Queues (continued)

Figure 4-8 A series of operations executed on a queue

01/16/24 Data Structures and Algorithms in Java 7 / R6-9 (page252). What values are returned during the following sequence of queue operations, if executed on an initially empty queue? enqueue(5), enqueue(3), dequeue(), enqueue(2), enqueue(8), dequeue(), dequeue(), enqueue(9), enqueue(1), dequeue(), enqueue(7), enqueue(6), dequeue(), dequeue(), enqueue(4), dequeue(), dequeue().

Applications of Queues

  • (^) Direct applications
    • (^) Waiting lists
    • (^) Access to shared resources (e.g., printer)
    • (^) Multiprogramming
  • (^) Indirect applications
    • (^) Auxiliary data structure for algorithms
    • (^) Component of other data structures Queue is used when things don’t have to be processed immediatly, but have to be processed in First In First Out order. This property of Queue makes it also useful in the following applications:

Array-based Queue - 1

  • (^) Use an array of size N in a circular fashion
  • (^) Two variables keep track of the first and last f index of the front element l index of the last element Q 0 1 2 f l normal configuration Q 0 1 2 l f wrapped-around configuration Array-based Queue

Array-based Queue - 2

Array-based Queue in detail

Array implementation of a queue - 2

13 void enqueue(Object x) { if(isFull() && !grow()) return; if(last == max-1 || last == -1) { a[0] = x; last=0; if(first==-1) first = 0; } else a[++last] = x; } Object front() throws Exception { if(isEmpty()) throw new Exception(); return(a[first]); } public Object dequeue() throws Exception { if(isEmpty()) throw new Exception(); Object x = a[first]; if(first == last) // only one element {first = last = -1;} else if(first==max-1) //first đứng ở cuối mảng first = 0; else //first bình thường first++; return(x); }

Linked list implementation of a queue

14 class Node { public Object info; public Node next; public Node(Object x, Node p) { info=x; next=p; } public Node(Object x) { this(x,null); } }; class MyQueue { protected Node head,tail; public MyQueue() { head = tail = null; } public boolean isEmpty() { return(head==null);} Object front() throws Exception { if(isEmpty()) throw new Exception(); return(head.info); } public Object dequeue() throws Exception { if(isEmpty()) throw new Exception(); Object x = head.info; head=head.next; if(head==null) tail=null; return(x); } void enqueue(Object x) { if(isEmpty()) head = tail = new Node(x); else { tail.next = new Node(x); tail = tail.next; } }

Queue Interface in Java - 1

  • (^) Java interface
corresponding to our
Queue ADT
  • (^) Requires the
definition of class
EmptyQueueException
  • (^) No corresponding
built-in Java class

public interface Queue { public int size(); public boolean isEmpty(); public Object front() throws EmptyQueueException; public void enqueue(Object o); public Object dequeue() throws EmptyQueueException; }

Double-Ended Queues (Deque) - 1

17 We next consider a queue-like data structure that supports insertion and deletion at both the front and the back of the queue. Such a structure is called a doubleended queue , or deque , which is usually pronounced “deck” to avoid confusion with the dequeue method of the regular queue ADT, which is pronounced like the abbreviation “D.Q.” The deque abstract data type is more general than both the stack and the queue ADTs. The extra generality can be useful in some applications. For example, we described a restaurant using a queue to maintain a waitlist. Occasionally, the first person might be removed from the queue only to find that a table was not available; typically, the restaurant will reinsert the person at the first position in the queue. It may also be that a customer at the end of the queue may grow impatient and leave the restaurant.

Priority Queues

19

  • (^) A priority queue can be assigned to enable a

particular process, or event, to be executed

out of sequence without affecting overall

system operation

  • (^) FIFO rule is broken in these situations
  • (^) In priority queues, elements are dequeued

according to their priority and their current

queue position

Array implementation of a priority queue - 1

20 class PriorityQueue { protected float [] a; int top, max; public PriorityQueue() { this(50); } public PriorityQueue(int max1) { max = max1; a = new float[max]; top = -1; } protected boolean grow() { int max1 = max + max/2; float [] a1 = new float[max1]; if(a1 == null) return(false); for(int i =0; i<=top; i++) a1[i] = a[i]; a = a1; return(true); } public boolean isEmpty() { return(top==-1);} public boolean isFull() { return(top==max-1);} public void clear() { top=-1;} public void enqueue(float x) { if(isFull() && !grow()) return; if(top==-1) { a[0] = x; top = 0; return; } int i = top; while(i>=0 && x<a[i]) { a[i+1] = a[i]; i--; } a[i+1] = x; top++; }