Queue – Java2Blog https://java2blog.com A blog on Java, Python and C++ programming languages Tue, 13 Apr 2021 15:19:09 +0000 en-US hourly 1 https://wordpress.org/?v=6.2.9 https://java2blog.com/wp-content/webpc-passthru.php?src=https://java2blog.com/wp-content/uploads/2022/09/cropped-ICON_LOGO_TRANSPARENT-32x32.png&nocache=1 Queue – Java2Blog https://java2blog.com 32 32 Implement Queue using Linked List in java https://java2blog.com/implement-queue-using-linked-list-in-java/?utm_source=rss&utm_medium=rss&utm_campaign=implement-queue-using-linked-list-in-java https://java2blog.com/implement-queue-using-linked-list-in-java/#comments Wed, 12 Oct 2016 15:19:00 +0000 http://www.java2blog.com/?p=73

If you want to practice data structure and algorithm programs, you can go through 100+ java coding interview questions.

In this post , we will see how to implement Queue using Linked List in java.
Queue is abstract data type which demonstrates First in first out (FIFO) behaviour. We will implement same behaviour using Array.
Although java provides implementation for  all abstract data types such as Stack , Queue and LinkedList but it is always good idea to understand basic data structures and implement them yourself.
Please note that LinkedList implementation of Queue is dynamic in nature.
There are two most important operations of Queue:
enqueue : It is operation when we insert element into the queue.
dequeue : It is operation when we remove element from the queue. Read more at

Java Program to implement Queue using Linked List:

package org.arpit.java2blog;
public class QueueUsingLinkedListMain
{
 private Node front, rear; 
 private int currentSize; // number of items

 //class to define linked node
 private class Node
 { 
 int data;
 Node next;
 }

 //Zero argument constructor
 public QueueUsingLinkedListMain()
 {
 front = null;
 rear = null;
 currentSize = 0;
 }

 public boolean isEmpty()
 {
 return (currentSize == 0);
 }

 //Remove item from the beginning of the list.
 public int dequeue()
 {
 int data = front.data;
 front = front.next;
 if (isEmpty()) 
 {
 rear = null;
 }
 currentSize--;
 System.out.println(data+ " removed from the queue");
 return data;
 }

 //Add data to the end of the list.
 public void enqueue(int data)
 {
 Node oldRear = rear;
 rear = new Node();
 rear.data = data;
 rear.next = null;
 if (isEmpty()) 
 {
 front = rear;
 }
 else 
 {
 oldRear.next = rear;
 }
 currentSize++;
 System.out.println(data+ " added to the queue");
 }
 public static void main(String a[]){

 QueueUsingLinkedListMain queue = new QueueUsingLinkedListMain();
 queue.enqueue(6);
 queue.dequeue();
 queue.enqueue(3);
 queue.enqueue(99);
 queue.enqueue(56);
 queue.dequeue();
 queue.enqueue(43);
 queue.dequeue();
 queue.enqueue(89);
 queue.enqueue(77);
 queue.dequeue();
 queue.enqueue(32);
 queue.enqueue(232);
 }
}
When you run above program, you will get below output:
6 added to the queue
6 removed from the queue
3 added to the queue
99 added to the queue
56 added to the queue
3 removed from the queue
43 added to the queue
99 removed from the queue
89 added to the queue
77 added to the queue
56 removed from the queue
32 added to the queue
232 added to the queue

]]>
https://java2blog.com/implement-queue-using-linked-list-in-java/feed/ 1
Queue implementation in java https://java2blog.com/implement-queue-using-array-in-java/?utm_source=rss&utm_medium=rss&utm_campaign=implement-queue-using-array-in-java https://java2blog.com/implement-queue-using-array-in-java/#comments Wed, 12 Oct 2016 15:19:00 +0000 http://www.java2blog.com/?p=75

If you want to practice data structure and algorithm programs, you can go through 100+ java coding interview questions.

In this post , we will see how to implement Queue using Array in java.
Queue is abstract data type which demonstrates First in first out (FIFO) behavior. We will implement same behavior using Array.

Although java provides implementation for  all abstract data types such as Stack, Queue and LinkedList but it is always good idea to understand basic data structures and implement them yourself.

Please note that Array implementation of Queue is not dynamic in nature.

There are two most important operations of Queue:
enQueue:It is operation when we insert element into the queue.
deQueue: It is operation when we remove element from the queue.

Java Program to implement Queue using Array:

package org.arpit.java2blog;

public class QueueUsingArrayMain {

    private int capacity;
    int queueArr[];
    int front;
    int rear;
    int currentSize = 0;

    public QueueUsingArrayMain(int sizeOfQueue) {
        this.capacity = sizeOfQueue;
        front = 0;
        rear = -1;
        queueArr = new int[this.capacity];
    }

    /**
     * this method is used to add element in the queue
     * 
     * @param data
     */
    public void enqueue(int data) {
        if (isFull()) {
            System.out.println("Queue is full!! Can not add more elements");
        } else {
            rear++;
            if (rear == capacity - 1) {
                rear = 0;
            }
            queueArr[rear] = data;
            currentSize++;
            System.out.println(data + " added to the queue");
        }
    }

    /**
     * This method removes an element from the front of the queue
     */
    public void dequeue() {
        if (isEmpty()) {
            System.out.println("Queue is empty!! Can not dequeue element");
        } else {
            front++;
            if (front == capacity - 1) {
                System.out.println(queueArr[front - 1] + " removed from the queue");
                front = 0;
            } else {
                System.out.println(queueArr[front - 1] + " removed from the queue");
            }
            currentSize--;
        }
    }

    /**
     * This method is use to check if element is full or not
     * 
     * @return boolean
     */
    public boolean isFull() {
        if (currentSize == capacity) {
            return true;
        }
        return false;
    }

    /**
     * This method is use to check if element is empty or not
     * 
     * @return
     */
    public boolean isEmpty() {

        if (currentSize == 0) {
            return true;
        }
        return false;
    }

    public static void main(String a[]) {

        QueueUsingArrayMain queue = new QueueUsingArrayMain(5);
        queue.enqueue(6);
        queue.dequeue();
        queue.enqueue(3);
        queue.enqueue(99);
        queue.enqueue(56);
        queue.dequeue();
        queue.enqueue(43);
        queue.dequeue();
        queue.enqueue(89);
        queue.enqueue(77);
        queue.dequeue();
        queue.enqueue(32);
        queue.enqueue(232);
    }
}
When you run above program, you will get below output:

6 added to the queue
6 removed from the queue
3 added to the queue
99 added to the queue
56 added to the queue
3 removed from the queue
43 added to the queue
99 removed from the queue
89 added to the queue
77 added to the queue
56 removed from the queue
32 added to the queue
232 added to the queue

That’s all about Queue implementation in java.

]]>
https://java2blog.com/implement-queue-using-array-in-java/feed/ 7