-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue Problem
More file actions
61 lines (54 loc) · 1.34 KB
/
Queue Problem
File metadata and controls
61 lines (54 loc) · 1.34 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
package Queues;
public class ArrayImplementationQueue {
public static class queueA {
int f = -1;
int r = -1;
int size = 0;
int arr[] = new int[100];
public void add(int val) {
if (r == arr.length - 1) {
System.out.println("Queue is full");
return;
}
if (f == -1) {
f = r = 0;
arr[0] = val;
} else {
arr[r + 1] = val;
r++;
}
size++;
}
public int remove() {
if (size == 0) {
System.out.println("queue is empty");
return -1;
}
int x = arr[f];
f++;
size--;
return x;
}
public int peek() {
return arr[f];
}
public void display() {
if (size == 0) {
System.out.println("queue is empty");
} else {
for (int i = f; i <= r; i++) {
System.out.print(arr[i] + " ");
}
}
System.out.println();
}
}
public static void main(String[] args) {
queueA q = new queueA();
q.display();
q.add(1);
q.add(2);
q.add(3);
q.display();
}
}