In Previous post, Queue Data Structure We talked and learned about Queue data structure.
In this post, we will learn about Queue Implementation using an array in java?
Below is the complete source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
package com.kkjavatutorials.queue; /** * Queue Implementation using circular array * @author KK JavaTutorials */ public class Queue { private int maxSize; private int front; private int rear; private int currentSize; private int queueArray[]; public Queue(int maxSize) { super(); this.maxSize = maxSize; this.front = 0; this.rear = -1; this.currentSize =0; this.queueArray = new int[maxSize]; } /** * Method to EnQueue or insert an Item in Queue(rear) * @param data Item to be inserted in Queue */ public void enQueue(int data) { //Checking if queue is full before inserting new Item if(isQueueFull()) { throw new RuntimeException("Queue is Full!!"); } //We have to wrap the queue in case maxSize has reached if(rear == maxSize-1) { rear =-1; } queueArray[++rear] = data; System.out.println(data+" is added in Queue"); currentSize++; } /** * Method to DeQueue or Remove an Item From Queue * @return return DeQueued or Removed Item from queue(front) */ public int deQueue() { //Checking if queue is Empty before Removing new item if(isQueueEmpty()) { throw new RuntimeException("Queue is Empty!!"); } int result = queueArray[front++]; //We have to wrap the queue in case maxSize has reached if(front == maxSize) { front = 0; } currentSize--; return result; } private boolean isQueueEmpty() { return currentSize == 0; } private boolean isQueueFull() { return maxSize == currentSize; } public int size() { return currentSize; } } |
Client program which uses our custom Queue:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.kkjavatutorials.client; import com.kkjavatutorials.queue.Queue; /** * Client program which uses our custom Queue * @author KK JavaTutorials * */ public class ClientTest { public static void main(String[] args) { Queue queue = new Queue(5); queue.enQueue(10); queue.enQueue(20); queue.enQueue(30); queue.enQueue(40); queue.enQueue(50); queue.deQueue(); queue.enQueue(60); System.out.println(queue.size()); } } |
Output of the above program:
10 is added in Queue
20 is added in Queue
30 is added in Queue
40 is added in Queue
50 is added in Queue
60 is added in Queue
5
You May Also Like:
Which data type would you choose for storing currency values like Trading Price in Java?
How would you print a given currency value for Indian Locale (INR Currency)?
Which classes in java are immutable ?
How would you round a double value to certain decimal Precision and Scale ?
What is BlockingQueue? How can we implement Producer and Consumer problem using Blocking Queue?
How to get number of available processors in Java ?
That’s all about Queue Implementation using an array in java?
If you have any feedback or suggestion please feel free to drop in below comment box.