-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue.java
60 lines (60 loc) · 1.39 KB
/
Queue.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
package interview;
import java.util.Arrays;
public class Queue<T extends Comparable<T>> extends Stack implements DSUtility {
private final Object queue[];
private int FRONT,REAR;
private int SIZE;
public Queue(int N){
if(N < 0)
throw new RuntimeException("SIZE CANNOT BE NEGATIVE");
queue=new Object[N];
FRONT=-1;
REAR=-1;
}
public Queue(){
queue=new Object[10];
FRONT=-1;
REAR=-1;
SIZE=10;
}
public void insert(T e){
if(REAR==SIZE-1){
System.out.println("SOORRY");
}else{
super.pushSupportingStack(e);
queue[++REAR]=e;
}
}
public T delete(){
if(FRONT==-1)
FRONT=0;
return (T)queue[FRONT++];
}
public void print(){
if(FRONT==-1)
FRONT=0;
int HEAD=FRONT;
for(;HEAD<=REAR;HEAD++)
System.out.print(" "+queue[HEAD]);
}
@Override
public boolean isFull(){
return (REAR==SIZE-1);
}
@Override
public boolean isEmpty(){
return (REAR==-1);
}
@Override
public Object min() {
return super.min();
}
@Override
public Object max() {
return super.max();
}
@Override
public void sort() {
Arrays.sort(queue);
}
}