-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArray_Implementation.java
81 lines (67 loc) · 1.81 KB
/
Array_Implementation.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
71
72
73
74
75
76
77
78
79
80
81
package Queue;
public class Array_Implementation {
public static class Queue{
static int size;
static int[] arr;
static int rear;
public Queue(int n){
arr = new int[n];
size = n;
rear = -1;
}
// Empty function
public static boolean isEmpty(){
return rear == -1;
}
// add at end
public static void add(int data){
if (rear == size-1){
System.out.println("Full Queue!");
return;
}
rear = rear + 1;
arr[rear] = data;
}
// remove from front
public static int remove(){
if (isEmpty()){
return -1;
}
int front = arr[0];
for (int i=0; i<rear; i++){
arr[i] = arr[i+1];
}
rear = rear-1;
return front;
}
// peek the first element
public static int peek(){
if (isEmpty()) return -1;
return arr[0];
}
// display
public static void display(){
if (isEmpty()) {
System.out.println("Empty Queue");
return;
}
System.out.print("front -> ");
for (int i=0; i<=rear; i++){
System.out.print(arr[i] + " -> ");
}
System.out.print("End");
}
}
public static void main(String[] args) {
int n = 5;
Queue que = new Queue(n);
que.display();
que.add(1);
que.add(2);
que.add(3);
que.add(4);
que.add(5);
que.add(6); // full queue
que.display();
}
}