-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue.java
81 lines (67 loc) · 2.18 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.util.Random;
public class Queue<T> extends DLinkedList<T> {
void Push(T data) {
Append(data);
}
T Pop() {
if (head == null) {
return null;
} else {
T ret = head.data;
Node temp = head;
if (temp.next == null) {
head = tail = null;
} else {
head = temp.next;
head.previous = null;
temp.next = null;
}
return ret;
}
}
T Peek() {
if (head == null) {
return null;
} else {
return head.data;
}
}
// IsEmpty() defined in parent class can be reused.
int GetLength() {
return Count();
}
public static void main(String args[])
{
Random rand = new Random();
int count = 10;
Queue<Integer> queue = new Queue<Integer>();
for (int x = 0; x < count; x++)
{
int rnumber = rand.nextInt(100) + 1;
queue.Push(rnumber);
System.out.print(rnumber+"\t");
}
System.out.println();
System.out.println("\t1) Output queue from tail to head is:");
queue.Output();
int a = rand.nextInt(100) + 1;
System.out.println("\t2) Push element: " + a);
queue.Push(a);
System.out.println("\t3) After push " + a + ", the queue is (from tail to head): ");
queue.Output();
int b = rand.nextInt(100) + 1;
System.out.println("\t4) Push element " + b);
queue.Push(b);
System.out.println("\t5) After push " + b + ", the queue is (from tail to head): ");
queue.Output();
System.out.println("\t6) queue size is: " + queue.GetLength());
System.out.println("\t7) Pop ");
int c = queue.Pop();
System.out.println("\t8) After Pop " + c + ", the queue is (from tail to head): ");
queue.Output();
System.out.println("\t9) After Pop queue size is: " + queue.GetLength());
int d = queue.Peek();
System.out.println("\t10) Peek the queue we got " + d);
System.out.println("\t11) queue is Empty now?" + queue.IsEmpty());
}
}