-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicQueue.java
More file actions
161 lines (105 loc) · 4.22 KB
/
DynamicQueue.java
File metadata and controls
161 lines (105 loc) · 4.22 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
public class DynamicQueue {
private int rear , front , size;
private static int CAPACITY = 16;
private int[] queueRep;
//empty constructor intialization process
public DynamicQueue(){
rear = 0 ; front = 0 ; size = 0;
queueRep = new int[CAPACITY];
}
//parameterize constructor
public DynamicQueue(int cap) throws NullPointerException{
if(cap <= 0){
throw new NullPointerException("please give a valid capacity for a queue");
}
CAPACITY = cap;
queueRep = new int[cap];
}
public int size(){
return size;
}
public boolean isEmpty(){
return (size == 0);
}
public int front(){
return queueRep[front];
}
public void enqueue(int data)throws NullPointerException , IllegalStateException{
if(size == CAPACITY){
// System.out.println("Size and capacity are equal");
expand();
}
// System.out.println("Size and capacity not equal" + rear);
size++;
queueRep[rear] = data;
rear = (rear + 1) % CAPACITY;
}
public int dequeue(){
int data = queueRep[front];
size--;
queueRep[front] = Integer.MIN_VALUE;
front = (front+1)% CAPACITY;
return data;
}
public void expand(){
int length = size;
int newQueue[] = new int[length<<1];
System.out.println("rear at : " + rear + " front at : " + front);
// if front and rear pointer are pointing at the exact same location
if(rear == 0){
for(int i = 0 ; i < queueRep.length ; i++){
newQueue[i] = queueRep[i];
}
}
else{
int j = 0, temp = front;
//System.out.println("temp value : " + temp + " and rear " + rear);
//we go only till one element before rear pointer , because there might be a issue of rear and front both pointer pointing at same location other than 0
while(temp != rear - 1){
newQueue[j] = queueRep[temp%CAPACITY];
j++;
temp = (temp+1) % CAPACITY;
}
newQueue[j] = queueRep[temp];
}
//resetting rear , size , capacity , queueRep and front
queueRep = newQueue;
front = 0;
rear = size;
CAPACITY *= 2;
}
public String toString(){
String result = "[";
for(int i = 0 ; i < size ; i++){
result += Integer.toString(queueRep[(front + i) %CAPACITY]);
if(i < size - 1) result += ", ";
}
result += "]";
return result;
}
public static void main(String args[]) {
DynamicQueue dq = new DynamicQueue(5);
dq.enqueue(1);System.out.println(dq.toString());
dq.enqueue(2);System.out.println(dq.toString());
dq.enqueue(3);System.out.println(dq.toString());
dq.enqueue(4);System.out.println(dq.toString());
dq.enqueue(5);System.out.println(dq.toString());
System.out.println("removed element : "+dq.dequeue());
System.out.println(dq.toString());
dq.enqueue(6);System.out.println(dq.toString());
dq.enqueue(7);System.out.println(dq.toString());
dq.enqueue(8);System.out.println(dq.toString());
dq.enqueue(9);System.out.println(dq.toString());
dq.enqueue(10);
System.out.println(dq.toString());
dq.enqueue(11);
System.out.println("removed element : "+dq.dequeue());
System.out.println(dq.toString());
System.out.println("removed element : "+dq.dequeue());
System.out.println(dq.toString());
System.out.println("removed element : "+dq.dequeue());
System.out.println(dq.toString());
System.out.println("removed element : "+dq.dequeue());
System.out.println(dq.toString());
}
}