Queue Implementation In Java

Chapter: Data Structures Last Updated: 02-03-2018 01:07:08 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

[ 1000 14 12 44 ]

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

Similar Programs Chapter Last Updated
Linked List Implementation In Java Data Structures 09-03-2018
Stack Implementation In Java Data Structures 08-03-2018
Binary Search Tree Java Data Structures 11-02-2018
Insertion Sort In Java Data Structures 10-02-2018
Java Stack Example Data Structures 16-10-2017
Java String Array Sort Data Structures 31-03-2017
Java Hashmap Add Key And Value Data Structures 25-03-2017
Java Binary Tree Vertical Sum Data Structures 11-11-2016
Java Binary Tree Boundary Traversal Data Structures 11-11-2016
Java Binary Tree Lowest Common Ancestor (LCA) Data Structures 11-11-2016
Java Binary Tree Maximum Element Data Structures 11-11-2016
Java Three Dimensional Array Data Structures 28-10-2016
Java Infix Expression To Postfix Expression Data Structures 25-10-2016
Java Linked List Add Element First And Last Data Structures 24-10-2016
Java Maximum Element From Vector Data Structures 24-10-2016
Java Binary Search On Vector Data Structures 24-10-2016
Java Get Elements Of LinkedList Data Structures 23-10-2016
Java Linked List Update Element Data Structures 23-10-2016
Java Delete Elements From LinkedList Data Structures 23-10-2016
Java Double Ended Queue Data Structures 09-10-2016
Java Dynamic Queue Using Array Data Structures 07-10-2016
Java Queue Array Based Implementation Data Structures 07-10-2016
Java Sort Short Array Data Structures 25-09-2016
Java Sort Long Array Data Structures 25-09-2016
Java Sort Int Array Data Structures 19-09-2016
Java Sort Float Array Data Structures 19-09-2016
Java Sort Double Array Data Structures 19-09-2016
Java Sort Char Array Data Structures 19-09-2016
Java Sort Byte Array Data Structures 19-09-2016
Java Clear LinkedList Elements Data Structures 09-09-2016

1 2 3