Skip to content

Commit ed7b3c8

Browse files
committed
Add Queue using ArrayList
1 parent 096c511 commit ed7b3c8

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package org.alda.structure.queue.array;
2+
3+
import org.alda.structure.queue.IQueue;
4+
5+
import java.util.ArrayList;
6+
7+
/**
8+
* @author bcExpt1123
9+
*
10+
* Implementation of a generic Queue using an ArrayList.
11+
* @param <T>
12+
*/
13+
public class QueueArray<T> implements IQueue<T> {
14+
/**
15+
* Internal storage for the queue elements.
16+
*/
17+
public ArrayList<T> items;
18+
19+
public QueueArray() {
20+
items = new ArrayList<>();
21+
}
22+
23+
/**
24+
* {@inheritDoc}
25+
* @return
26+
*/
27+
public boolean isEmpty() {
28+
return items.isEmpty();
29+
}
30+
31+
/**
32+
* {@inheritDoc}
33+
* @param item the item to be added.
34+
*/
35+
public void enqueue(T item) {
36+
items.add(item);
37+
}
38+
39+
/**
40+
* {@inheritDoc}
41+
* @return
42+
*/
43+
public T dequeue() {
44+
if (items.isEmpty()) {
45+
return null;
46+
}
47+
return items.removeLast();
48+
}
49+
50+
/**
51+
* {@inheritDoc}
52+
* @return
53+
*/
54+
public T front() {
55+
if (items.isEmpty()) {
56+
return null;
57+
}
58+
return items.getFirst();
59+
}
60+
61+
/**
62+
* {@inheritDoc}
63+
* @return
64+
*/
65+
public T rear(){
66+
if (items.isEmpty()) {
67+
return null;
68+
}
69+
return items.getLast();
70+
}
71+
}

0 commit comments

Comments
 (0)