Circular Queue using Array in Java DSA

-- Leave a Comment
Hello here is Circular Queue. Here we create two separate file for the class and the main method. Everything is simple and common code here. I would like to introduce a new style to initialize a instance variables. The code block from line no 7 to line no 10 on CircularQueue.java is a Initialization Block. The benefit of creating any initializing block is that every constructors on the class would use the block to initialize variables. To initialize the static variables we just use static keyword before the block.

Here is CircularQueue.java file.
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
class CircularQueue
{
 int maxSize;
 int rear;
 int front;
 int aQueue[];
 {
  rear = -1;
  front = -1;
 }
 CircularQueue(int maxSize)
 {
  this.maxSize = maxSize;
  this.aQueue = new int[maxSize];
 }
 void enQueue(int item)
 {
  if(((rear+1) % maxSize) == front)
  {
   System.out.println("Queue is Full");
  }
  else
  {
   if (rear == front && front == -1)
   {
    front += 1;
   }
   rear = (rear+1) % maxSize;
   aQueue[rear] = item;
  }
 }
 void deQueue()
 {
  if(rear == front && rear == -1)
  {
   System.out.println("Queue is Empty.");
  }
  else
  {
   int item = aQueue[front];
   if(rear == front)
   {
    rear = -1;
    front = -1;
   }
   else
   {
    front = (front + 1) % maxSize;
   }
   System.out.println(item + " is deQueued from the Queue");
  }
 }
 
 String elementOrElements()
 {
  String send = (rear == front)? (" ") :("s ");
  return send;
 }
  
 void display()
 {
  int tmpfront = front;
  if(rear == front && rear == -1)
                {
                        System.out.println("Queue is Empty.");
                }
                else
  {
   System.out.println("The element"+ elementOrElements() + "on the Queue are:- ");
   for(int i=0; i<maxSize ; i++)
   {
    if(tmpfront != rear)
    {
     System.out.println(aQueue[tmpfront]);
     tmpfront = (tmpfront + 1) % maxSize;
    }
    else
    {
     System.out.println(aQueue[rear]);
     break;
    }
   }
  }
 }
}

And Here is CircularQueueTest.java file.
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
import java.util.Scanner;
public class CircularQueueTest
{
 public static void main(String []args)
 {
  Scanner input = new Scanner(System.in);
  System.out.print("Enter total no of elements to be in the queue:- ");
  int maxSize = input.nextInt();
  CircularQueue queue = new CircularQueue(maxSize);
  int select;
  int term = 0;
  while (term == 0)
  {
   System.out.print("\nOption:\tTo Do:\n1\tTo push element.\n2\tTo POP element.\n3\tTo Display the Queue elements.\n4\tTo Exit the Program.\nEnter your option:- ");
                 select = input.nextInt(); 
   switch(select)
   {
    case 1:
    {
     System.out.print("Enter element to insert in the Queue:- ");
     int ele = input.nextInt();
     queue.enQueue(ele); 
     break;
    }
    case 2:
    {
     queue.deQueue();
     break;
    }
    case 3:
    {
     queue.display();
     break;
    }
    case 4:
    {
     term = 1;
     System.out.println("Thank you!");
     break;
    }
    default:
     System.out.println("Enter a valid options");
   }
  }
 }
}

To fully understand this code you need to figure out what actually (front + 1) % maxSize do. Take your time and do some calculation. It would be better if calculate it and figure out it by yourself.

Hint:- (front + 1) % maxSize and (rear+1) % maxSize will always return 0 or < maxSize.

Hello This is Sagar Devkota. Studying Bachelor of Software Engineering at Pokhara University, NCIT. Open Source Enthusiastic.

0 comments:

Post a Comment