Queue in Java and some of its common methods

dreamecho100

dreamecho100

Mazen Mohamed
created at: ,  updated at: tags: programming, queue, java, data-structure
Queue in Java and some of its common methods

What is a Queue

A Queue is a linear structure that follows a particular order in which the operations are performed.

The order is First In First Out (FIFO).

A good example of a queue is any queue of consumers for a resource where the consumer that came first is served first.

Here we will implement the queue in Java with some of its common methods like (enqueue, dequeue, peak, and print).

Queue vs Stack

The difference between stacks and queues is in removing. In a stack, we remove the item the most recently added.

In a queue, we remove the item the least recently added.

Stack implementation in Java

let's start with a class [ "MyQueue" ] and add the basic properties and methods that we will need.

  • [ "front" ]: An integer that holds the value of the first index of the queue.
  • [ "rear" ]: An integer that holds the value of the last index of the queue.
  • [ "maxSize" ]: A variable we will use to Know the maximum size allowed for the queue.
  • [ "numberOfElements" ]: A variable that we will use to keep track of the number of elements in the queue.
  • [ "list" ]: An array where we will store the values of the queue.
  • [ "isFull" ]: A method that will return true if the queue is empty.
  • [ "isEmpty" ]: A method that will return true if the queue is full.

When we call the [ "MyQueue" ] class:

  • It will take an integer that will determine the [ "maxSize" ] of the queue.
  • [ "front" ]: will be 0;
  • [ "rear" ]: will be -1 since there is no element yet;
  • we will initialize the size of the [ "list" ] array by the [ "maxSize" ]
  • And lastly, the [ "numberOfElements" ] will be 0.

Add an element to Stack in Java

  • First, We will check if the last item in the queue is at the end of the queue (at the last allowed index), we will change the [ "rear" ] to point at the first of the array, else we increase both [ "rear" ] and [ "numberOfElements" ] by one.
  • Then that we will place the item to the index value of [ "rear" ].
  • Finally, we will return the index of the item in the queue.

Remove an element to Stack in Java

In A Queue when removing an item we remove the first item to come in.

  • First, We will check if the queue is empty, and if true we exit or you can throw an exception.
  • Then that we will store the item in the index [ "front" ] in [ "removedItem" ] to return it.
  • After that, we will increase the value of [ "front" ] and decrease the value of [ "numberOfElements" ].
  • We also will check if the [ "front" ] is at the last index of the queue and assign it to 0 (at the first of the queue) if true.
  • Finally, we will return the [ "removedItem" ].

Looking at the first element of the queue (Peak)

  • First, We will check if the queue is empty, and if true we exit or you can throw an exception.
  • Then we will return the first element in the queue.

Printing all the elements of the queue

I have added two variations and I advise you to test both of them and see the difference 😁.

Printing all the elements of the queue V1

Printing all the elements of the queue V2

full code for queue implementation in Java