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 {
private LinkedList list;
public Steque() {
list = new LinkedList<>();
}
public void enqueue(T item) {
list.addLast(item); // Incorrect: should add to the front
}
public T dequeue() {
return list.removeFirst(); // Incorrect: should remove from the front
}
public void push(T item) {
list.addFirst(item); // Incorrect: should add to the back
}
public T pop() {
return list.removeFirst(); // Incorrect: should remove from the front
}
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.
Ashini Ayodhya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.