Wikipedia

Search results

Sunday, October 27, 2013

Queue

QUEUE

What is Queue ?
Whenever I am asked about a queue i am reminded of the boring queue that I use to stand in to pay my bills .And yes its same in programming languages too though here its not boring . If I am first standing in a Queue I would be the first one to pay my bill and get out of the line . This makes the queue a FIFO(First In First Out) operation,the first element added to queue would be the first one to be removed .


(Example of a Queue at a booth)

(FIFO operations done on a queue)
It is just so simple to understand always remember the general idea behind it and you could use in programming.
Now I would put some light on the FIFO operations in programming .
There are two types of operations that can be performed on a queue:-
1. Enqueue - Insertion/Pushing of element 
2. Dequeue - Deletion/Poping of element 

Considering a array the enqueue always done at end and dequeue from the front

I have taken two counters(front & rear) representing front of the queue and rear/end of the queue

ENQUEUE operation on the  queue(Insertion of new element)

In order to insert a element in a queue we need to check if the queue is full or not in order to do so we check if rear pointer has reached the INIT_CAPACITY of our array by
rear<INIT_CAPACITY-1

If condition is satisfied we insert an element in the array by

arr[++rear]=data
else we print "Queue is Full"

DEQUEUE operation from the  queue(Delete a element from queue)

In order to delete a element from a queue we need to check if the queue is empty or not because if we delete a element from a empty queue it would throw an exception that should be handled . 
In order to do so we check if our pointer has reached front=-1 or does the front pointer has a higher value than rear pointer 
i.e.
front==-1 || front>rear
If condition satisfies we handle the exception as queue is empty .
If not we remove the element from the queue by returning the element 
return arr[front++]





 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
 *  Queue using an array
 *  @ Author Karan Deep Singh
 * */
public class Queue {
 private static int INIT_CAPACITY = 5;
 int arr[];
 int front;
 int rear;

 public Queue() {
  
  arr = new int[INIT_CAPACITY];
  front = 0;
  rear = -1;
 }
 // Enqueue/Insert elements in queue by checking if queue is full or not 
 private void enqueue(int data) {
  
   if(rear<INIT_CAPACITY-1)
    arr[++rear] = data;
   else
    System.out.println("Queue is Full");
 }
 //Dequeue/Delete elements from queue by checking if queue is empty or not 
 private int dequeue()throws Exception {
  
  if(front==-1 || front > rear)
   throw new Exception("Queue is Empty");
  return arr[front++];
 }

 public static void main(String[] args) throws Exception {

  Queue queue = new Queue();
  queue.enqueue(10);
  queue.enqueue(12);
  queue.enqueue(13);
  queue.enqueue(14);
  queue.enqueue(15);
  queue.enqueue(16);
  queue.enqueue(17);
  queue.enqueue(18);
  
  System.out.println(queue.dequeue());
  System.out.println(queue.dequeue());
  System.out.println(queue.dequeue());
  System.out.println(queue.dequeue());
  System.out.println(queue.dequeue());
  System.out.println(queue.dequeue());
  

 }
}






No comments: