Queue data structure | Queue DS | Queue

Queue is a data structure which has these followings characteristics:
  1. It has two terminals from where data is insert or get.
  2. The process of inserting the data in the queue data structure is called en-queue. (en-queue = insert)
  3. The process of getting data from the queue data structure is called de-queue. (de-queue = Get)
  4. The FIFO (first in first out) methodology is used in the queue data structure.
  5. For getting data from this data structure from one terminal the data is insert and form another terminal data is get.








FIFO ( First in first out )









Queue in java
Types of Queues
  • thread-safety: if you don't require the queue to be accessed concurrently from multiple threads, then a plain LinkedList can be used as a Queue; the advantage of the other implementations is that they offer efficient thread-safety;
  • blocking or non-blocking: various blocking implementations add extra methods to put and remove items from the queue, blocking until the operation is possible, with an optional time limit;
  • bound or non-bound: sometimes it is useful to put an upper limit on the number of items that can fit in the queue, e.g. to prevent a thread pool from queueing up too many jobs when the machine is busy;
  • other special operations: Java provides an implementation that orders by priority, and another that applies a delay to queued items.
Queue implementations as of Java 6
Blocking?Other criteriaBoundNon-bound
BlockingNoneArrayBlockingQueueLinkedBlockingQueue
Priority-basedPriorityBlockingQueue
DelayedDelayQueue
Non-blockingThread-safeConcurrentLinkedQueue
Non thread-safeLinkedList
Non thread-safe, priority-basedPriorityQueue

Java provides Queue implementations depending on a few key criteria:

As of Java 6, the various queue classes are as follows:

One further type of queue not included above is the SynchronousQueue, which is effectively a zero-length queue (so that a thread adding an item to the queue will block until another thread removes the item).

Blocking queues

In general, the most interesting queue implementations are the various blocking queues, which allow efficient concurrent access and are useful for coordinating objects between threads, particularly in the so-called producer-consumer pattern. In Java, all blocking queue implementations implement the BlockingQueue interface, which we look at next.




Example of queue (by Array)

import java.util.Scanner;

public class queue {
// class variable
private static String []queue;
private static int front=0;
private static int rear;
private static int size=0;

public static void size(int s)
{
queue= new String[s];
size=s;
rear=size-1;

}

public static void enqueu(String data) {
queue[front]=data;
front++;
}

public static String dequeu() {

String data="";

if (rear>=0) {
data=queue[rear];
queue[rear]=null;
rear--;
}

else
;
return data;

}

public static boolean isEmpty() {

boolean dequeued=false;
int howManydequeued=0;
for (int i = 0; i < size; i++) {
if (queue[i]==null)
howManydequeued++;
else
continue;

}

if (front==0 || howManydequeued==size )

return true;

else 
return false;
}

public static void main(String[] args) {


Scanner ip= new Scanner (System.in);
 
System.out.println("Is queue empty: "+isEmpty());
System.out.print("please enter the size of queue:");
int s=ip.nextInt();

size(s);

for (int i = 0; i <s; i++) {
System.out.print("Enqueue:");
String value=ip.next();
enqueu(value);
}

System.out.print("Dequeue: "+dequeu());
System.out.println();


System.out.println("Is queue empty: "+isEmpty());



}


}

Example of queue (by LinkedList)

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class QueueEWithLinkedList {
// the declaration of queue
public static Queue <String> q= new LinkedList <String>();
//method for add the data into the queue
public static void enqueue (String item) {
q.add(item);
}
// method for remove the data from queue
public static void dequeue() {
int last=q.size()-1;
String removedItem=((LinkedList<String>) q).get(last);
q.remove(removedItem);
last--;
}
// method for isEmpty queue
public static boolean isEmpty () {
if(q.isEmpty())
return true;
else
return false;
}
// method for show the elements in the queue
public static void show() {
System.out.println(q);
}
public static void main(String[] args) {
// object for getting input
Scanner ip= new Scanner (System.in);
System.out.println("Check that whether the queue is empty : "+isEmpty());
// for getting the value into the queue from user
while(true) {
System.out.println("Please enter the data in the queue: ");
String data=ip.next();
enqueue(data);
System.out.println("Press y to more entery");
String y=ip.next();
// continue till the user enter "y"
if (y.equalsIgnoreCase("Y"))
continue;
else
break;
}

// current status of queue
System.out.println("Queue status:");
show();
while(true) {
System.out.println("Press \"d\" to dequeue");
String d=ip.next();
// if user enter D then dequeue method calls
if (d.equalsIgnoreCase("D")) {
try {
//for dequeue
dequeue();
System.out.println("After dequeue the queue the current queue:");
// current status
show();
}
catch(NullPointerException e){
System.out.println("Queue is empty");
}
break;
}
}

System.out.println("Check that whether the queue is empty or not: "+isEmpty());

}

}

Example of queue (by LinkedBlockingQueue)
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;

public class Main {

public static void main(String[] args) {

Queue q = new LinkedBlockingQueue();

q.add(1);
q.add(2);
q.add(3);
q.add(4);
q.add(5);
 
System.out.println(q.poll());
System.out.println(q.poll());
System.out.println(q.poll());
System.out.println(q.poll());
System.out.println(q.poll());

}

}

Comments

Popular posts from this blog

Umbrella activities in Software Engineering

Operating System | Best Definition of Opetating System