140 likes | 182 Views
FIFO ( F ir st I n F irst O ut). Queue. peek, element. capacity. poll, remove. offer. a. b. h. e. f. size. front. rear. Java Doc java.util Interface Queue<E>. /*********************************************************/
E N D
FIFO (First In First Out) Queue peek, element capacity poll, remove offer a b h e f size front rear Java Doc java.util Interface Queue<E>
/*********************************************************/ /* API for Queue */ /*********************************************************/ publicinterface Queue<T> { boolean offer(T item); // return false if failed T remove(); // throw NoSuchElementException if empty T poll(); // return null if empty T peek(); // return null if empty T element();// throw NoSuchElementException if empty int size(); // return the number of element in the queue int capacity(); // return the capacity of the queue // -1 if unlimited } Queue interface IT 179
Queue interface 2 3 5 size=3 capacity=5 publicinterface Queuek<T> { boolean offer(T item); T remove(); T poll(); T peek(); T element(); int size(); int capacity(); } front 2 3 5 rear IT 179
Queue interface 2 3 5 size=3 publicinterface Queuek<T> { boolean offer(T item); T remove(); T poll(); T peek(); T element(); int size(); int capacity(); } 5 rear 3 2 front IT 179
offer: rear++ poll: front++ Using arrays to implement queues offer offer offer poll poll front front front front rear rear front rear front rear rear rear IT 179
offer: rear= rear+1)%capacity poll: front=(front+1)%capacity Using arrays to implement queues poll offer poll offer offer offer rear front front front front front front rear rear rear rear rear rear IT 179
Using linked lists to implement queues Data structures front rear 1 2 3 4 Boolean offer(T item); T remove(); T poll(); T peek(); T element() int size(); int capacity(); Operations IT 179
package myUtil; import java.util.NoSuchElementException; /* This LQueue uses a linked list to implement interface Queue. */ publicclass LQueue<T> implements Queue<T> { /* This is an inner class for internal nodes */ privateclass Node<E> { private E data; private Node<E> next; private Node(E data, Node<E> next) { this.data = data; this.next = next; } } private Node<T> front, rear; privateintsize, capacity; /* Default constructor initializes an empty queue. */ public LQueue() { front = rear = null; size=0; capacity=-1; // -1 : no limit } /* Set capacity. */ public LQueue(int n) { front = rear = null; size=0; capacity=n; } Queue using single linked lists IT 179
publicboolean offer(T item) { if (size == capacity) returnfalse; if (rear == null) front = rear = new Node<T>(item, null); else rear = rear.next = new Node<T>(item, null); size++; returntrue; } Implementation using linked lists public T peek() { if (front==null) returnnull; returnfront.data; } public T element() { if (front==null) thrownew NoSuchElementException(); returnfront.data; } IT 179
public T remove() { if (front==null) thrownew NoSuchElementException(); T item = front.data; if (front == rear) front = rear = null; else front = front.next; size--; return item; } Implementation using linked lists public T poll() { if (front==null) returnnull; T item = front.data; if (front == rear) front = rear = null; else front = front.next; size--; return item; } IT 179
package myUtil; import java.util.NoSuchElementException; /* This AQueue uses a linked list to implement interface Queue. */ publicclass AQueue<T> implements Queue<T> { private Object[] queue; privateintfront, rear, size, capacity; public AQueue() { front = rear = size = 0; capacity = -1; // -1: unlimited queue = new Object[10]; } public AQueue(int capacity) { queue = new Object[capacity]; this.capacity = capacity; front = rear = size = 0; } Queue using single arrays IT 179
/** • * A private method that doubles the size of the array, queue, • * if its size is too small for a new item. • */ • privatevoid doubleCapacity(){ • Object[] newQ = new Object[queue.length*2]; • for (int i=0; i<size; i++) { • newQ[i]=queue[front]; • front = (front+1)%size; • } • front = 0; • rear = size-1; • queue = newQ; • } Queue using single arrays IT 179
publicboolean offer(T item) { • if (size==queue.length && capacity==-1) doubleCapacity(); • if (size==queue.length) returnfalse; • if (size != 0) • rear = (rear+1)%queue.length; • queue[rear] = item; • size++; • returntrue; • } • public T remove() { • if (size==0) thrownew NoSuchElementException(); • size--; • T item =(T) queue[front]; • if (size != 0) front = (front+1)%queue.length; • return item; • } Queue using single arrays IT 179
Deque<E> Double Ended Queue offerFirst addFirst pollLast removeLast peekLast, getLast peekFirst, getFirst capacity a b h e f size offerLast addLast front rear pollFirst, removeFirst Java Doc java.util Interface Deque<E> IT 179