















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
















4
Data Structures and Algorithms in Java 5
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().
Array-based Queue in detail
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); }
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; } }
public interface Queue { public int size(); public boolean isEmpty(); public Object front() throws EmptyQueueException; public void enqueue(Object o); public Object dequeue() throws EmptyQueueException; }
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.
19
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++; }