-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMyQueue.java
More file actions
80 lines (68 loc) · 1.86 KB
/
MyQueue.java
File metadata and controls
80 lines (68 loc) · 1.86 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
80
import java.util.ArrayList;
/**
* Implementation of a Queue using an ArrayList
* Created by Devang on 29-Dec-16.
*/
public class MyQueue<E> {
private ArrayList<E> myList = new ArrayList<>();
public MyQueue() {
}
public void enqueue(E item) {
myList.add(item);
}
public E dequeue() {
if (myList.size() == 0)
return null;
return myList.remove(0);
}
public int size() {
return myList.size();
}
public boolean isEmpty() {
return myList.isEmpty();
}
public E front() {
if (myList.size() == 0)
return null;
return myList.get(0);
}
@Override
public String toString() {
return myList.toString();
}
public static void main(String[] args) {
//Integer
MyQueue<Integer> q1 = new MyQueue<>();
System.out.println("Dequeue: " + q1.dequeue());
System.out.println("IsEmpty: " + q1.isEmpty());
q1.enqueue(2);
q1.enqueue(3);
q1.enqueue(5);
q1.enqueue(7);
q1.enqueue(11);
q1.enqueue(13);
System.out.println(q1);
System.out.println("Size: " + q1.size());
System.out.println("Dequeue: " + q1.dequeue());
System.out.println(q1);
System.out.println("IsEmpty: " + q1.isEmpty());
System.out.println("Front: " + q1.front());
System.out.println();
//String
MyQueue<String> q2 = new MyQueue<>();
q2.enqueue("Virat Kohli");
q2.enqueue("Yuvraj Singh");
q2.enqueue("Mahendra Singh Dhoni");
q2.enqueue("Ravindra Jadeja");
q2.enqueue("Rohit Sharma");
q2.enqueue("Umesh Yadav");
System.out.println(q2);
System.out.println("Dequeue: " + q2.dequeue());
System.out.println("Dequeue: " + q2.dequeue());
System.out.println(q2);
System.out.println("Size: " + q2.size());
System.out.println("IsEmpty: " + q2.isEmpty());
System.out.println("Front: " + q2.front());
System.out.println();
}
}