- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneric_Queue_using_Array_List.java
More file actions
Latest commit
60 lines (51 loc) · 1.61 KB
/
Copy pathGeneric_Queue_using_Array_List.java
File metadata and controls
60 lines (51 loc) · 1.61 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
//Example
importjava.util.Scanner;
importjava.util.ArrayList;
classQueue<T> {
privateArrayList<T> mArr;
publicQueue() {
mArr = newArrayList<>();
}
publicvoidpush(Tval) {
mArr.add(val);
}
publicTpop() {
if (mArr.size() < 1) returnnull;
returnmArr.remove(0);
}
publicStringtoString() {
Stringres = "";
for (inti = 0; i < mArr.size(); ++i) {
res += mArr.get(i).toString() + (i+1 == mArr.size() ? "" : " ");
}
returnres;
}
}
publicclassMain {
privatestatic <T> voidprint(Queue<T> q) {
System.out.println("Queue insert done");
System.out.println("Queue is: " + q);
System.out.println("Queue is deleted: " + q.pop());
System.out.println("Queue is deleted: " + q.pop());
System.out.println("Queue is: " + q);
}
publicstaticvoidmain(String[] args) {
Scannersc = newScanner(System.in);
Queue<Integer> qi = newQueue<>();
for (inti = 0; i < 6; ++i) qi.push(sc.nextInt());
System.out.println("Queue Contents: " + qi);
qi.push(sc.nextInt());
print(qi);
sc.nextLine();
Queue<String> qs = newQueue<>();
for (inti = 0; i < 6; ++i) qs.push(sc.next());
System.out.println("Queue is: " + qs);
qs.push(sc.next());
print(qs);
Queue<Double> qd = newQueue<>();
for (inti = 0; i < 6; ++i) qd.push(sc.nextDouble());
System.out.println("Queue is: " + qd);
qd.push(sc.nextDouble());
print(qd);
}
}