-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.java
More file actions
70 lines (52 loc) · 1.23 KB
/
Queue.java
File metadata and controls
70 lines (52 loc) · 1.23 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
60
61
62
63
64
65
66
67
68
69
70
public class Queue {
int[] arrs;
int front;
int rear;
int size;
public Queue(){
arrs = new int[5];
front = 0;
rear = -1;
size = 0;
}
// insert element
public void enqueue(int data ){
if( isfull()){
throw new RuntimeException("Queue is full");
}
rear= (rear+1)%5;
arrs[rear] = data;
size++;
}
// delete element
public int dequeue(){
if (isempty()) {
throw new RuntimeException("Queue is empty.");
}
int data = arrs[front];
front = (front+1)%5;
return data;
}
// to check queue is full
public boolean isfull()
{
return size == 5;
}
// to check queue is empty
public boolean isempty(){
return size == 0;
}
// to print peek element
public int peek(){
if (isempty()) {
throw new RuntimeException("Queue is empty.");
}
return arrs[front];
}
public void display(){
for(int i=front; i < size; i++){
System.out.print(arrs[i] +" ");
}
System.out.println();
}
}