-
Notifications
You must be signed in to change notification settings - Fork 35
/
EfficientArrayImplementation.java
78 lines (62 loc) · 1.16 KB
/
EfficientArrayImplementation.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
package deque;
class Deque {
int arr[];
int size, cap, front;
Deque(int c) {
arr = new int[c];
cap = c;
front = 0;
size = 0;
}
boolean isFull() {
return (size == cap);
}
boolean isEmpty() {
return size == 0;
}
void insertFront(int x) {
if(isFull()) return;
front = (front+cap-1)%cap;
arr[front] = x;
size++;
}
void deleteFront() {
if(isEmpty()) return;
front = (front+1)%cap;
size--;
}
void insertRear(int x) {
if(isFull()) return;
int newRear = (front+size)%cap;
arr[newRear] = x;
size++;
}
int deleteRear() {
if(isEmpty()) return -1;
else return arr[--size];
}
int getFront() {
if(isEmpty()) return -1;
else return arr[front];
}
int getRear() {
if(isEmpty()) return -1;
else return arr[(front+size-1)%cap];
}
}
public class EfficientArrayImplementation {
public static void main(String[] args) {
Deque dq = new Deque(6);
dq.insertFront(10);
dq.insertFront(78);
dq.insertFront(89);
dq.insertFront(45);
dq.insertFront(7);
// dq.deleteFront();
dq.insertFront(89);
System.out.println(dq.getFront());
// while(!dq.isEmpty()) {
// System.out.println(dq.getRear());
// }
}
}