10/28/2015it 1791 queue a fifo (first in first out) efbh front rear poll, remove offer add peek,...
Post on 03-Jan-2016
215 Views
Preview:
TRANSCRIPT
04/20/23 IT 179 1
Queue
a
FIFO (First In First Out)
e fb h
front rear
poll,remove
offeradd
peek,element
capacity
size
Java Doc
java.utilInterface Queue<E>
04/20/23 IT 179 2
/*********************************************************//* API for Queue *//*********************************************************/
public interface Queue<T> {boolean offer(T item); // return false if failedT remove(); // throw NoSuchElementException if emptyT poll(); // return null if emptyT peek(); // return null if emptyT element();// throw NoSuchElementException if emptyint size(); // return the number of element in the queueint capacity(); // return the capacity of the queue
// -1 if unlimited}
Queue interface
04/20/23 IT 179 3
Queue interface
2 3 5
public interface Queuek<T> { boolean offer(T item); T remove(); T poll(); T peek(); T element(); int size(); int capacity();}
2
3
5
front
rear
size=3capacity=5
04/20/23 IT 179 4
Queue interface
2 3 5
2
3
5
front
rear
public interface Queuek<T> { boolean offer(T item); T remove(); T poll(); T peek(); T element(); int size(); int capacity();}
size=3
04/20/23 IT 179 5
Using arrays to implement queues
frontrear rear
offer: rear++poll: front++
offer
front
rear
front
offer
rear
offer
rear
front
front
poll
rearfront
poll
04/20/23 IT 1796
Using arrays to implement queues
front
rear
rear
offer
front
poll
rear
front
poll
offer
rear
offer
rear
front front
offer
rear
front
rear
offer: rear= rear+1)%capacity poll: front=(front+1)%capacity
04/20/23 IT 179 7
1 2 3 4
front rear
Data structures
Operations
Using linked lists to implement queues
Boolean offer(T item); T remove(); T poll(); T peek(); T element()int size();int capacity();
package myUtil;import java.util.NoSuchElementException;
/* This LQueue uses a linked list to implement interface Queue. */public class LQueue<T> implements Queue<T> {
/* This is an inner class for internal nodes */private class 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;private int size, 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; }
04/20/23IT 179 8
Queue using single linked lists
04/20/23 IT 179 9
Implementation using linked lists
public boolean offer(T item) { if (size == capacity) return false; if (rear == null)
front = rear = new Node<T>(item, null); else
rear = rear.next = new Node<T>(item, null); size++; return true; }
public T peek() { if (front==null) return null; return front.data;}
public T element() { if (front==null) throw new NoSuchElementException(); return front.data;}
04/20/23 IT 179 10
Implementation using linked lists
public T remove() { if (front==null) throw new NoSuchElementException(); T item = front.data; if (front == rear)
front = rear = null; else
front = front.next; size--; return item;}
public T poll() { if (front==null) return null; T item = front.data; if (front == rear) front = rear = null; else
front = front.next; size--; return item;}
package myUtil;import java.util.NoSuchElementException;
/* This AQueue uses a linked list to implement interface Queue. */public class AQueue<T> implements Queue<T> {
04/20/23 IT 179 11
Queue using single arrays
private Object[] queue;private int front, 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;
}
04/20/23 IT 179 12
Queue using single arrays
/** * A private method that doubles the size of the array, queue, * if its size is too small for a new item. */private void 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;
}
04/20/23 IT 179 13
Queue using single arrays
public boolean offer(T item) {if (size==queue.length && capacity==-1) doubleCapacity();if (size==queue.length) return false;if (size != 0)
rear = (rear+1)%queue.length;queue[rear] = item;size++;return true;
}
public T remove() {if (size==0) throw new NoSuchElementException();size--;T item =(T) queue[front];if (size != 0) front = (front+1)%queue.length;return item;
}
04/20/23 IT 179 14
Double Ended Queue
a e fb h
front rearpollFirst,removeFirst
offerLastaddLast
peekFirst,getFirst
size
Java Doc
java.utilInterface Deque<E>
Deque<E>
peekLast,getLast
pollLastremoveLast
offerFirstaddFirst
capacity
top related