Please help Implement StackInterface using a ListInterface object as the underly
ID: 3841655 • Letter: P
Question
Please help
Implement StackInterface using a ListInterface object as the underlying data structure.
The class and some methods are implemented below.
For this question, implement the three missing methods: pop, push, and peek.
For full credit, do not add any new instance data variables and only implement the three methods specified below.
public class StackAsList<T> implements StackInterface<T> {
private ListInterface<T> list;
public StackAsList () {
list = new AList<T>();
}
public void push(T newEntry) {
// IMPLEMENT THIS METHOD
}
public T pop() {
// IMPLEMENT THIS METHOD
}
public T peek() {
// IMPLEMENT THIS METHOD
}
public boolean isEmpty() {
return list.isEmpty();
}
public void clear() {
list.clear();
}
}
Explanation / Answer
public class StackAsList<T> implements StackInterface<T> {
private ListInterface<T> list;
public StackAsList () {
list = new AList<T>();
}
public void push(T newEntry) {
list.add(newEntry); // this will add at the top
}
public T pop() {
return (list.remove(list.size()-1)); //This will remove from the top
}
public T peek() {
return (list.get(list.size()-1)); // This will give the element at the top
}
public boolean isEmpty() {
return list.isEmpty();
}
public void clear() {
list.clear();
}
}