Presentation is loading. Please wait.

Presentation is loading. Please wait.

5/4/2015ITK 2751 Queue a FIFO (First In First Out) efbh front rear poll, remove offer peek, element capacity size.

Similar presentations


Presentation on theme: "5/4/2015ITK 2751 Queue a FIFO (First In First Out) efbh front rear poll, remove offer peek, element capacity size."— Presentation transcript:

1 5/4/2015ITK 2751 Queue a FIFO (First In First Out) efbh front rear poll, remove offer peek, element capacity size

2 5/4/2015ITK 2752 /*********************************************************/ /* API for Queue */ /*********************************************************/ public interface ITKQueue { 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();// throws NoSuchElementException if empty } ITKQueue interface

3 5/4/2015ITK 2753 ITKQueue interface 235 public interface ITKQueuek { boolean offer(T item); T remove(); T poll(); T peek(); T element(); } 2 3 5 front rear

4 5/4/2015ITK 2754 Using array to implement queue front rear front rear front rear (rear+1)%capacity (front+1)%capacity

5 5/4/2015ITK 2755 Little Trouble front rear front rear (rear+1)%capacity (front+1)%capacity front rear

6 5/4/2015ITK 2756 import java.util.NoSuchElementException; public class Queue implements ITKQueue { private T[] queue; private int front, rear, size, capacity; public Queue(int capacity) { // this is the constructor queue = (T[]) new Object[capacity]; front = rear = size = 0; // if size=0 then front=rear; this.capacity = capacity; }.... } Queue class

7 5/4/2015ITK 2757 public class Queue implements ITKQueue {..... // return false if failed public boolean offer(T item) { if (size==capacity)return false; if (size==0) queue[rear] = item; else { rear = (rear+1)%capacity; queue[rear] = item; } size++; return true; }... } Methods implementation

8 5/4/2015ITK 2758 public class Queue implements ITKQueue {..... // throw NoSuchElementException if empty public T remove() { if (size==0) throw new NoSuchElementException(); size--; T item = queue[front]; if (size!=0) front = (front+1)%capacity; return item; }... } remove implementation

9 5/4/2015ITK 2759 public class Queue implements ITKQueue {..... // return null if empty public T poll() { if (size==0) return null; size--; T item = queue[front]; if (size!=0) front = (front+1)%capacity; return item; }... } poll implementation

10 5/4/2015ITK 27510 public class Queue implements ITKQueue {..... // return null if empty public T peek() { if (size==0) return null; return queue[front]; } // throws NoSuchElementException if empty public T element() { if (size==0) throw new NoSuchElementException();; return queue[front]; }... } peek, element implementation


Download ppt "5/4/2015ITK 2751 Queue a FIFO (First In First Out) efbh front rear poll, remove offer peek, element capacity size."

Similar presentations


Ads by Google