-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeneric_Queue_using_Array_List.java
60 lines (51 loc) · 1.61 KB
/
Generic_Queue_using_Array_List.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
//Example
import java.util.Scanner;
import java.util.ArrayList;
class Queue<T> {
private ArrayList<T> mArr;
public Queue() {
mArr = new ArrayList<>();
}
public void push(T val) {
mArr.add(val);
}
public T pop() {
if (mArr.size() < 1) return null;
return mArr.remove(0);
}
public String toString() {
String res = "";
for (int i = 0; i < mArr.size(); ++i) {
res += mArr.get(i).toString() + (i+1 == mArr.size() ? "" : " ");
}
return res;
}
}
public class Main {
private static <T> void print(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);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Queue<Integer> qi = new Queue<>();
for (int i = 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 = new Queue<>();
for (int i = 0; i < 6; ++i) qs.push(sc.next());
System.out.println("Queue is: " + qs);
qs.push(sc.next());
print(qs);
Queue<Double> qd = new Queue<>();
for (int i = 0; i < 6; ++i) qd.push(sc.nextDouble());
System.out.println("Queue is: " + qd);
qd.push(sc.nextDouble());
print(qd);
}
}