Queue Implementation In Java
Chapter:
Data Structures
Last Updated:
22-09-2018 08:45:26 UTC
Program:
/* ............... START ............... */
public class Queue {
private int maxSize; // initializes the set number of slots
private long[] queArray; // slots to main the data
private int front; // this will be the index position for the element in the
// front
private int rear; // going to be the index position for the element at the
// back of the line
private int nItems; // counter to maintain the number of items in our queue
public Queue(int size) {
this.maxSize = size;
this.queArray = new long[size];
front = 0; // index position of the first slot of the array
rear = -1; // there is no item in the array yet to be considered as the
// last item.
nItems = 0; // we don't have elements in the array yet
}
public void insert(long j) {
if (rear == maxSize - 1) {
rear = -1;
}
rear++;
queArray[rear] = j;
nItems++;
}
public long remove() { // remove item from the front of the queue
long temp = queArray[front];
front++;
if (front == maxSize) {
front = 0; // we can set front back to the 0th index so that we can
// utilize the entire array again
}
nItems--;
return temp;
}
public long peekFront() {
return queArray[front];
}
public boolean isEmpty() {
return (nItems == 0);
}
public boolean isFull() {
return (nItems == maxSize);
}
public void view() {
System.out.print("[ ");
for (int i = front; i < queArray.length; i++) {
System.out.print(queArray[i] + " ");
}
System.out.print("]");
}
public static void main(String[] args) {
Queue myQueue = new Queue(5);
myQueue.insert(100);
myQueue.insert(1000);
myQueue.insert(14);
myQueue.insert(12);
myQueue.insert(44);
myQueue.remove();
myQueue.view();
}
}
/* ............... END ............... */
Output
Notes:
-
Queue is data structure in java which follows First-In-First-Out (FIFO) data structure. This data structure we can assume as ticket counter the person comes at the front will get the ticket at the first and others in order.
- To add an element in queue we can call it as enqueue and to remove the entries from queue we call it as dequeue.
- Queue myQueue = new Queue(5); - Will create queue with size five and we will set front zero and rear to -1 to make the queue as empty. When creating the queue we set the number of items to zero (nItems = 0 ).
- When inserting an item to queue or during enqueue, first we will increment rear by one and place the item and also increment the nitem value.
- While removing an item from queue or dequeue, first item in front is stored in temp variable and front is increment by one and return the value at the temp variable index.
- As like in stack, peek operation is will return the value at the front of queue without removing the value.
- IsEmpty will check whether queue is empty or not (By checking nitem = 0 ).
- IsFull will check whether queue is full nor not, for that in program we check that nitem = maxSize.
- View() function will print the values in the queue in the order.
Tags
Queue Implementation, Java Queue, Data Structures, simple queue implementation in java, queue implementation in java using array, java queue example