You are on page 1of 9

Queue

PREPARED BY: ABDUL JALIL NIAZAI


Introduction
Queue is also an abstract data type or a linear
data structure, in which the first element is
inserted from one end called REAR(also called
tail), and the deletion of existing element takes
place from the other end called as FRONT(also
called head).
This makes queue as FIFO data structure,
which means that element inserted first will also
be removed first.
The process to add an element into queue is
called Enqueue and the process of removal of
an element from queue is called Dequeue.

PREPARED BY: ABDUL JALIL NIAZAI


Applications Of Queue
There are various queues quietly doing their job in your computer’s (or
the network’s) operating system.
There is printer queue where print jobs wait for the printer to be
available.
A queue also stores keystroke data as you type at type at the keyboard.
Handling of interrupts in real-time systems. The interrupts are handled in
the same order as they arrive, First come first served.

PREPARED BY: ABDUL JALIL NIAZAI


Queue Implementation
Queue can be implemented using an Array, or Linked List. The easiest
way of implementing a queue is by using an Array. To avoid moving
objects once they are placed in Q, we define two variables front and last,
which has the following meanings:
front points to the first element and last points to the last element.
Initially front=0, and last = -1, which indicate that the Q is empty.

PREPARED BY: ABDUL JALIL NIAZAI


class Queue {
private int maxSize;
private long[] queArray;
private int front;
private int last;
private int nItems;
public Queue(int s) {
maxSize = s;
queArray = new long[maxSize];
front = 0;
last= -1;
nItems = 0;
} PREPARED BY: ABDUL JALIL NIAZAI
public void insert(long j) {
if(last == maxSize - 1)
last = -1;
queArray[++last] = j;
nItems++;
}
public long remove() {
long temp = queArray[first++];
if(first == maxSize)
first = 0;
nItems--;
return temp;
}
PREPARED BY: ABDUL JALIL NIAZAI
public long peekFront() {
return queArray[front];
}
public boolean isEmpty() {
return (nItems ==0);
}
public boolean isFull() {
return (nItems==maxSize);
}
public int Size() {
return nItems;
}
PREPARED BY: ABDUL JALIL NIAZAI
public static void main(String[] args) {
Queue theQueue = new Queue(5);
theQueue.insert(10);
theQueue.insert(20);
theQueue.insert(30);
theQueue.insert(40);
theQueue.remove();
theQueue.remove();
theQueue.remove();
theQueue.insert(50);
theQueue.insert(60);
theQueue.insert(70);
theQueue.insert(80);

PREPARED BY: ABDUL JALIL NIAZAI


while( !theQueue.isEmpty() )
{
long n = theQueue.remove();
System.out.print(n);
System.out.print(" ");
}
System.out.println(" ");
}
}

PREPARED BY: ABDUL JALIL NIAZAI

You might also like