|
| 1 | +/** |
| 2 | + * Data-Structures-And-Algorithms-in-Java |
| 3 | + * ArrayBasedQueue.java |
| 4 | + */ |
| 5 | +package com.deepak.data.structures.Queue; |
| 6 | + |
| 7 | +import java.util.NoSuchElementException; |
| 8 | + |
| 9 | +/** |
| 10 | + * Implementation of array based queue |
| 11 | + * @author Deepak |
| 12 | + */ |
| 13 | +public class ArrayBasedQueue { |
| 14 | + |
| 15 | + /** |
| 16 | + * Method to test queue implementation |
| 17 | + * @param args |
| 18 | + */ |
| 19 | + public static void main(String[] args) { |
| 20 | + System.out.println("Creating a new Array based queue."); |
| 21 | + ArrayBasedQueue queue = new ArrayBasedQueue(10); |
| 22 | + System.out.println("Size of Queue => " + queue.size()); |
| 23 | + System.out.println("Is Queue Empty => " + queue.isEmpty()); |
| 24 | + System.out.println("Inserting two items in Queue."); |
| 25 | + queue.enqueue(10); |
| 26 | + queue.enqueue(20); |
| 27 | + System.out.println("After Insertion, Size of Queue => " + queue.size()); |
| 28 | + System.out.println("After Insertion, Is Queue Empty => " + queue.isEmpty()); |
| 29 | + System.out.println("Removing an item from Queue."); |
| 30 | + queue.dequeue(); |
| 31 | + System.out.println("After Removal, Size of Queue => " + queue.size()); |
| 32 | + System.out.println("Top element on Queue => " + queue.peek()); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Array of objects |
| 37 | + */ |
| 38 | + private Object[] array; |
| 39 | + |
| 40 | + /** |
| 41 | + * Size of queue |
| 42 | + */ |
| 43 | + int size = 0; |
| 44 | + |
| 45 | + /** |
| 46 | + * Since it is a queue, we need track of both head and tail |
| 47 | + * Initial values to be 0 |
| 48 | + */ |
| 49 | + int head = 0; |
| 50 | + int tail = 0; |
| 51 | + |
| 52 | + /** |
| 53 | + * Constructor to create new array based queue |
| 54 | + * @param capacity |
| 55 | + */ |
| 56 | + public ArrayBasedQueue(int capacity) { |
| 57 | + array = new Object[capacity]; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Method to push a new item in queue |
| 62 | + * We will deal only with tail while adding new item |
| 63 | + * @param item |
| 64 | + */ |
| 65 | + public void enqueue(Object item) { |
| 66 | + if (size == array.length) { |
| 67 | + throw new IllegalStateException("Cannot add to full queue"); |
| 68 | + } |
| 69 | + array[tail] = item; |
| 70 | + tail = (tail + 1) % array.length; |
| 71 | + size++; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Method to pop a item from queue |
| 76 | + * We will deal only with head while deleting a element |
| 77 | + * @return |
| 78 | + */ |
| 79 | + public Object dequeue() { |
| 80 | + if (size == 0) { |
| 81 | + throw new NoSuchElementException("Cannot remove from empty queue"); |
| 82 | + } |
| 83 | + Object item = array[head]; |
| 84 | + array[head] = null; |
| 85 | + head = (head + 1) % array.length; |
| 86 | + size--; |
| 87 | + return item; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Method to check the top item in queue |
| 92 | + * @return |
| 93 | + */ |
| 94 | + public Object peek() { |
| 95 | + if (size == 0) { |
| 96 | + throw new NoSuchElementException("Cannot peek from empty queue"); |
| 97 | + } |
| 98 | + return array[size - 1]; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Method to check size of queue |
| 103 | + * @return |
| 104 | + */ |
| 105 | + public int size() { |
| 106 | + return size; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Method to check if queue is empty |
| 111 | + * @return |
| 112 | + */ |
| 113 | + public boolean isEmpty() { |
| 114 | + return size == 0; |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments