Repository navigation
Expand file tree
/
Copy pathDynamic_Queue.java
More file actions
127 lines (93 loc) · 2.4 KB
/
Copy pathDynamic_Queue.java
File metadata and controls
127 lines (93 loc) · 2.4 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
/*
author: kunal-kushwaha
date: 29-03-2019
*/
class Queue_Circluar {
protected int[] data;
private static int DEFAULT_SIZE = 10;
protected int end = 0;
protected int front = 0;
private int size = 0;
public Queue_Circluar(){
this(DEFAULT_SIZE); // will run if no value is passed
}
public Queue_Circluar(int size){
data = new int[size];
}
public boolean isFull(){
return size == data.length;
}
public boolean isEmpty(){
return size == 0;
}
public boolean insert(int item){
if(isFull()){
return false;
}
data[end++] = item;
end = end % data.length; // restart from index 0
size++;
return true;
}
public int remove() throws Exception{
if(isEmpty()){
throw new Exception("Removing from empty Queue");
}
int temp = data[front++];
front = front % data.length;
size--;
return temp;
}
public int front() throws Exception{
if(isEmpty()){
throw new Exception("Checking front from empty queue");
}
return data[0];
}
public void display(){
for (int i = front; i < end ; i++) {
System.out.print(data[i] + " ");
}
System.out.println("END");
}
}
class Queue extends Queue_Circluar{
public Queue(){
super();
}
public Queue(int size){
super(size);
}
@Override
public boolean insert(int item) {
if(isFull()){
System.out.println("RESIZING");
int[] temp = new int[data.length * 2];
for (int i = 0; i < data.length; i++) {
temp[i] = data[(front + i) % data.length];
}
front = 0;
end = data.length;
data = temp;
}
return super.insert(item);
}
}
public class Dynamic_Queue {
public static void main(String[] args) throws Exception{
Queue queue = new Queue();
for (int i = 0; i < 50; i++) {
queue.insert(i);
}
System.out.println("Queue contains : ");
queue.display();
}
}
/*
OUTPUT :
RESIZING
RESIZING
RESIZING
Queue contains :
0 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 END
*/