1 / 14

Queue

FIFO ( F ir st I n F irst O ut). Queue. peek, element. capacity. offer add. poll, remove. a. b. h. e. f. size. front. rear. Java Doc java.util Interface Queue<E>. /*********************************************************/

kerryn
Télécharger la présentation

Queue

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. FIFO (First In First Out) Queue peek, element capacity offer add poll, remove a b h e f size front rear Java Doc java.util Interface Queue<E> IT 179

  2. /*********************************************************/ /* 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 of unlimited } Queue interface IT 179

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. /** • * 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

  13. 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

  14. 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

More Related