Presentation is loading. Please wait.

Presentation is loading. Please wait.

Queue FIFO (First In First Out) Java Doc peek, element offer poll, add

Similar presentations


Presentation on theme: "Queue FIFO (First In First Out) Java Doc peek, element offer poll, add"— Presentation transcript:

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

2 Queue interface /*********************************************************/ /* API for Queue */ public interface 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 } 4/24/2017 IT 179

3 Queue interface front rear 2 3 5 size=3 capacity=5 public interface
Queuek<T> { boolean offer(T item); T remove(); T poll(); T peek(); T element(); int size(); int capacity(); } front 2 3 5 rear 4/24/2017 IT 179

4 Queue interface rear front 2 3 5 size=3 public interface
Queuek<T> { boolean offer(T item); T remove(); T poll(); T peek(); T element(); int size(); int capacity(); } 5 rear 3 2 front 4/24/2017 IT 179

5 Using arrays to implement queues
offer: rear++ poll: front++ offer offer offer poll poll front front front front rear rear rear front front rear rear rear 4/24/2017 IT 179

6 Using arrays to implement queues
offer: rear= rear+1)%capacity poll: front=(front+1)%capacity poll offer poll offer offer offer rear front front front front front front rear rear rear rear rear rear 4/24/2017 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 4/24/2017 IT 179

8 Queue using single linked lists
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; IT 179 4/24/2017

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; } 4/24/2017 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; } 4/24/2017 IT 179

11 Queue using single arrays
package myUtil; import java.util.NoSuchElementException; /* This AQueue uses a linked list to implement interface Queue. */ public class AQueue<T> implements Queue<T> { 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; IT 179 4/24/2017

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; IT 179 4/24/2017

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; IT 179 4/24/2017

14 Double Ended Queue Deque<E> Java Doc offerFirst addFirst
pollLast removeLast peekFirst, getFirst peekLast, getLast capacity a b h e f size offerLast addLast front rear pollFirst, removeFirst Java Doc java.util Interface Deque<E> 4/24/2017 IT 179


Download ppt "Queue FIFO (First In First Out) Java Doc peek, element offer poll, add"

Similar presentations


Ads by Google