public class GenericStackApp { public static void main(String args[]) { GenericStack<String> q1 = new GenericStack<>(); q1.push("Apples"); q1.push("Oranges"); q1.push("Bananas"); for (int i=0; i < q1.size(); i++) { System.out.println("Push: " + q1.push()); } System.out.println("The stack contains " + q1.size() + " items"); System.out.println(); System.out.println("Peek: " + q1.peek()); System.out.println("The stack contains " + q1.size() + " items"); System.out.println(); while (q1.size() > 0) { System.out.println("Pop: " + q1.pop()); } System.out.println("The stack contains " + q1.size() + " items"); } }import java.util.*; public class GenericStack<E> { private LinkedList<E> list = new LinkedList<>(); public void push(E item) { list.addLast(item); } public E push() { return list.element(); } public E peek() { return list.getLast(); } public E pop() { return list.removeLast(); } public int size() { return list.size(); } }
My output was like this:
Push: Apples
Push: Apples
Push: Apples
The stack contains 3 items
Peek: Bananas
The stack contains 3 items
Pop: Bananas
Pop: Oranges
Pop: Apples
The stack contains 0 items
I need it to be:
Push: Apples
Push: Oranges
Push: Bananas
I don't know what to do next.