Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Fill in the mmissing bodies for public String toString(), public void clear() , public boolean remove(E e), public void push(E e) , public E pop(),
Fill in the mmissing bodies for public String toString(), public void clear(), public boolean remove(E e), public void push(E e), public E pop(), public E top() .
import java.util.NoSuchElementException; public class LinkedListimplements List , Stack { private Node first, last; private int size = 0; // Construct a new empty list. public LinkedList() { first = last = new Node<>(null, null); } // Adds element e to the end of the list. public void add(E e) { last.next = new Node<>(e, null); last = last.next; ++size; } // Returns the element at the specified index, // or throws an IndexOutOfBoundsException if the index is out of range. public E get(int index) { if (index < 0 || index >= size) throw new IndexOutOfBoundsException(); Node t = first.next; for (int i = 0; i < index; ++i) t = t.next; return t.data; } // Returns a string representation of the linked list. public String toString() { // Fill in. } // Removes all elements. public void clear() { // Fill in. } // Removes the first occurrence of the specified element, if it is present. // Returns true if the list has been modified. public boolean remove(E e) { // Fill in. } // Returns the number of elements. public int size() { return size; } // Adds element e to the top of the stack. public void push(E e) { // Fill in. } // Removes and returns the top element of the stack, // or throws a NoSuchElementException if the stack is empty. public E pop() { // Fill in. } // Returns but does not remove the top element of the stack, // or throws a NoSuchElementException if the stack is empty. public E top() { // Fill in. } private static class Node { E data; Node next; Node(E data, Node next) { this.data = data; this.next = next; } } }
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