-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue_arraylist.java
More file actions
95 lines (77 loc) · 1.51 KB
/
queue_arraylist.java
File metadata and controls
95 lines (77 loc) · 1.51 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
public class QueueUsingArray {
private int data[];
private int front; // index at which front element is stored
private int rear; // index at which rear element is stored
private int size;
public QueueUsingArray() {
data = new int[10];
front = -1;
rear = -1;
size = 0;
}
public QueueUsingArray( int capacity) {
data = new int[capacity];
front = -1;
rear = -1;
size = 0;
}
int size(){
return size;
}
boolean isEmpty(){
return size == 0;
}
int front() throws QueueEmptyException{
if(size == 0){
throw new QueueEmptyException();
}
return data[front];
}
void enqueue(int element) throws QueueFullException{
if(size == data.length){
throw new QueueFullException();
}
if(size == 0){
front = 0;
}
size++;
rear = (rear + 1) % data.length;
// rear++;
// if(rear == data.length){
// rear = 0;
// }
data[rear] = element;
}
private void doubleCapacity() {
int temp[] = data;
data = new int[ 2* temp.length];
int index = 0;
for(int i = front ; i < temp.length; i++){
data[index] = temp[i];
index++;
}
for(int i = 0; i <= front - 1;i++){
data[index] = temp[i];
index++;
}
front = 0;
rear = temp.length - 1;
}
int dequeue() throws QueueEmptyException{
if(size == 0){
throw new QueueEmptyException();
}
int temp = data[front];
front = (front + 1) % data.length;
// front++;
// if(front == data.length){
// front = 0;
// }
size--;
if(size == 0){
front = -1;
rear = -1;
}
return temp;
}
}