forked from hussien89aa/DataStructureAndAlgorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueDArray.java
More file actions
52 lines (48 loc) · 1.01 KB
/
QueueDArray.java
File metadata and controls
52 lines (48 loc) · 1.01 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
package com.ds;
import java.util.Arrays;
public class QueueDArray<T> {
Object[] ArrayQueue;
int Rear;
int Front;
int size;
public QueueDArray(int size) {
this.size=size;
ArrayQueue= new Object[this.size];
Front=-1;
Rear=-1;
}
public Boolean isFull(){
return(Rear==size-1);
}
public Boolean isEmplty(){
return(Front==-1|| Front>Rear);
}
public void Queue(Object newItem){
ensureCapacity(Rear+2);
Rear=Rear+1;
ArrayQueue[Rear]= newItem;
if(Front==-1)
Front=0;
}
public void ensureCapacity( int minCapacity){
int oldCapacity= getSize();
if(minCapacity>oldCapacity){
int newCapacity=oldCapacity*2;
if(newCapacity<minCapacity)
newCapacity=minCapacity;
ArrayQueue=Arrays.copyOf(ArrayQueue, newCapacity);
}
}
public int getSize(){
return ArrayQueue.length;
}
public T DeQueue(){
if(isEmplty()){
System.out.println("queue is empty");
return null;
}
T ObjectOut=(T) ArrayQueue[Front];
Front=Front+1;
return ObjectOut;
}
}