-
Notifications
You must be signed in to change notification settings - Fork 25
/
Queue.java
70 lines (69 loc) · 1.64 KB
/
Queue.java
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
package Data_Structures;
import java.util.*;
class queue
{
private static int queue[];
private static int front,rear,size;
queue(int a)
{
queue = new int [a];
front = -1;
rear = -1;
}
private static void enqueue(int n)
{
if(front==size-1)
{
System.out.println("OverFlow Condition");
}
else
{
if(front==-1&&rear==-1)
{
front = 0;
rear = 0;
queue[front] = n;
}
else
{
queue[++rear] = n;
}
}
}
private static void deque()
{
if(front==-1)
{
System.out.println("UnderFlow Condition");
}
else
{
queue[front] = queue[++front];
}
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the queue");
size = sc.nextInt();
queue q = new queue(size);
enqueue(5);
enqueue(6);
deque();
enqueue(8);
enqueue(9);
deque();
enqueue(2);
deque();
deque();
enqueue(10);
System.out.println("Elements present in the queue: ");
for(int i=front;i<=rear;i++)
{
System.out.println(queue[i]);
}
System.out.println("Front is present in " + front + " position" + " which has element " + queue[front]);
System.out.println("Rear is present in " + rear + " position" + " which has element " + queue[rear]);
sc.close();
}
}