This repository was archived by the owner on Jun 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQueueArray.java
More file actions
79 lines (77 loc) · 1.65 KB
/
QueueArray.java
File metadata and controls
79 lines (77 loc) · 1.65 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
package ASD;
public class QueueArray {
private static final int initSize=1;
Object[] learn;
int[]umur;
int[]tensi;
int front,rear,itemCount;
public QueueArray() {
makeEmpty();
}
public boolean isFull() {
return itemCount==learn.length-1;
}
public boolean isEmpty() {
return itemCount==0;
}
public void makeEmpty() {
learn=new Object[initSize];
umur = new int[initSize];
tensi = new int[initSize];
front=0;
rear=-1;
itemCount=0;
}
public void arrayDoubling() {
Object[] tmp=learn;
int[] tmpp = umur;
int[] tmppp = tensi;
learn=new Object[tmp.length*2];
umur = new int[tmpp.length*2];
tensi = new int[tmppp.length*2];
System.arraycopy(tmp, 0, learn, 0, tmp.length);
System.arraycopy(tmpp, 0, umur, 0, tmpp.length);
System.arraycopy(tmppp, 0, tensi, 0, tmppp.length);
}
public void enqueue(Object x,int usia, int tensi) {
if(!isFull()) {
learn[++rear]=x;
umur[rear]=usia;
this.tensi[rear]=tensi;
itemCount++;
}else {
arrayDoubling();
learn[++rear]=x;
umur[rear]=usia;
this.tensi[rear]=tensi;
itemCount++;
}
}
public Object dequeue() throws Exception{
if(!isEmpty()) {
Object tmp=learn[front];
for(int i=0;i<itemCount;i++) {
learn[i] = learn[i + 1];
umur[i]=umur[i+1];
tensi[i]=tensi[i+1];
}
learn[rear--]=null;
itemCount--;
return tmp;
}else {
throw new Exception("quewe kosong, gada yang keluar");
}
}
public Object peek() throws Exception{
if(!isEmpty()) {
return learn[front];
}else {
throw new Exception("gaada isinya, mau ngintip apa?");
}
}
public void print() {
for(int i=0;i<itemCount;i++) {
System.out.print(learn[i] + " ");
}
}
}