-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircularQueue.java
More file actions
99 lines (71 loc) · 1.95 KB
/
circularQueue.java
File metadata and controls
99 lines (71 loc) · 1.95 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
public class circularQueue {
protected int [] data ;
private static final int DIFAULT_SIZE=10;
protected int end = 0 ;
protected int frout = 0 ;
private int size = 0 ;
public circularQueue (){
this(DIFAULT_SIZE);
}
public circularQueue (int size ){
this .data = new int[size];
}
public boolean isEmpty (){
return size == 0 ;
}
public boolean isFull (){
return size == data.length;
}
public boolean Insert (int item){
if (isFull()){
return false;
}
data[end++] = item;
end = end % data.length;
size++;
return true ;
}
public int remove () throws Exception{
if (isEmpty()){
throw new Exception("This is empty Queue");
}
int removed = data[frout];
frout = frout % data.length ;
size--;
return removed;
}
public int front () throws Exception{
if (isEmpty()){
throw new Exception("This is empty Queue");
}
return data[frout];
}
public void Display (){
if (isEmpty()){
System.out.println("is empty ");
return ;
}
int i = frout;
do {
System.out.print(data[i] + " -> ");
i++;
i %= data.length;
} while (i != end);
System.out.println("End ");
}
public class DynamicQueue extends circularQueue{
@Override
public boolean Insert (int item){
if (isFull()){
int[] temp = new int [data.length * 2];
for (int i = 0; i < temp.length; i++) {
temp [i]= data[(frout+1) * data.length];
}
frout = 0 ;
end = data.length;
data = temp ;
}
return super.Insert(item);
}
}
}