Question
public class MyStack { private java.util.ArrayList list = new java.util.ArrayList (); //TODO1: Implement the method: return the top element on the stack. public E peek()
public class MyStack
//TODO1: Implement the method: return the top element on the stack. public E peek() { //Return the last element in the array list (without removing it). return null; }
//TODO2: Implement the method: push the element onto the top of the stack public void push(E e) { //Add the element e at the end of list } //TODO3: Implement the method: pop out the element at top of the stack public E pop() { //Remove the last element of the list and return it return null; }
//TODO4: Implement the method: return whether the stack is empty public boolean isEmpty() { //Return whether the list is empty. return false; }
//TODO5: Implement the method: return the size of the stack public int getSize() { //Return the size of the list return 0; }
@Override public String toString() { return "stack: " + list.toString(); } }
public class TestMyStack { public static void main(String[] args) { // Create a stack MyStack
// Add elements to the stack stack.push("Tom"); // Push it to the stack System.out.println("(1) " + stack);
stack.push("Susan"); // Push it to the the stack System.out.println("(2) " + stack);
stack.push("Kim"); // Push it to the stack stack.push("Michael"); // Push it to the stack System.out.println("(3) " + stack);
// Remove elements from the stack System.out.println("(4) " + stack.pop()); System.out.println("(5) " + stack.pop()); System.out.println("(6) " + stack); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started