I’m implementing a steque in Java, which is meant to combine the properties of both a stack and a queue. My implementation aims to support operations like push, pop, enqueue and dequeue.
Here’s my current implementation, but it’s exhibiting unexpected behavior.
import java.util.LinkedList;
public class Steque<T> {
private LinkedList<T> list;
public Steque() {
list = new LinkedList<>();
}
public void enqueue(T item) {
list.addLast(item);
}
public T dequeue() {
return list.removeFirst();
}
public void push(T item) {
list.addFirst(item);
}
public T pop() {
return list.removeFirst();
}
public boolean isEmpty() {
return list.isEmpty();
}
public int size() {
return list.size();
}
}
I would appreciate any insights into where I’ve gone wrong and how to correct these methods to properly implement the steque functionality.
New contributor
Ashini Ayodhya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
5