You have been given the following code:
Class: LinkedList
Interfaces: List / Stack
Driver class to use for testing: HW1.java
Sample output

The LinkedList class implements both the List interface and the Stack interface, but several methods (listed below) are missing bodies. Write the code so it works correctly. You should submit one file, LinkedList.java.

Do not change the interfaces. Do not change the public method headers. Do not rename the LinkedList class.

If you change them, it will not work when I test it.

The given code uses a dummy node. If you prefer not to use a dummy node, you may change it.

None of your methods should print anything to the console. All of the printing happens in HW1.java.

Any method that adds or removes an element should update size appropriately.

public String toString()

Returns a String representation of the linked list.

Your format does not have to be exactly the same as mine, but the String must include every element (not including the dummy node).

Examples of acceptable formats are "[1, 2, 3]", "[1,2,3]", "[ 1 2 3 ]", "1 2 3".

public void clear()

Removes all elements. Don't forget to update everything that needs to be updated.

public boolean remove(E e)

Removes the first occurrence of the specified element, if it is present. For example, remove(7) removes the first element equal to 7 (use the equals method to compare elements).

Returns true if the list has been modified, or return false if the list has not been modified.

If the node you removed is the last node, don't forget to update last.

public void push(E e)

Adds element e to the top of the stack.

push and pop must happen on the same side.

The "top" to be at the beginning of the linked list, because there's no efficient way to remove the last node of a singly-linked list.

public E pop()

Removes and returns the top element of the stack, or throws a NoSuchElementException if the stack is empty.

public E top()

Returns but does not remove the top element of the stack, or throws a NoSuchElementException if the stack is empty.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.