-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.java
More file actions
59 lines (54 loc) · 1.27 KB
/
Queue.java
File metadata and controls
59 lines (54 loc) · 1.27 KB
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
import java.util.Arrays;
public class Queue {
public int ar[];
private int DEFAULT_SIZE=5;
int front=0,end=0;
public Queue()
{
this.ar=new int[DEFAULT_SIZE];
}
public boolean isFull ()
{
return end==ar.length;}
public boolean enqueue(int element) throws Exception
{
if(isFull())
throw new Exception("bhar gayi hai be");
ar[end++]=element;
return true;
}
public boolean isEmpty()
{
return end==0;
}
public int dequeue() throws Exception
{
if(isEmpty()) {
throw new Exception("khali hai be");
} int temp=ar[0];
for (int i = 1; i <end ; i++) {
ar[i-1]=ar[i];
}
end--;
return temp;
}
public void reverse_queue() throws Exception{
if (end==0)
return;
int temp=dequeue();
reverse_queue();
enqueue(temp);
}
public void display(){
System.out.println(Arrays.toString(ar));
}
public static void main(String[] args) throws Exception {
Queue q=new Queue();
for (int i = 0; i <q.ar.length ; i++) {
q.enqueue(i+1);
}
q.display();
q.reverse_queue();
q.display();
}
}