Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The LinkedList class implements both the List interface and the Stack interface, but several methods are missing bodies. Write the missing code so it works

The LinkedList class implements both the List interface and the Stack interface, but several methods are missing bodies. Write the missing code so it works correctly. Do not change the interfaces. Do not change the public method headers. Do not rename the LinkedList class. 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 modify size appropriately. Classe HW1 and the List and Stack interfaces are completed. The only thing that should be modified is LinkedList, and the only changes should be to fill in the mmissing bodies for public E remove(int index) , public void push(E e), public E pop(), public E top(), public void reverse(), and public String toString(). Thank you for any assistance.

______________________________________________________

 public class LinkedList implements 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; } // Removes and returns the element at the specified index, // or throws an IndexOutOfBoundsException if the index is out of range. public E remove(int index) { // Fill in. } // 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 returns null if the stack is empty. public E pop() { // Fill in. } // Returns but does not remove the top element of the stack, or returns null if the stack is empty. public E top() { // Fill in. } // Reverses the order of all the elements of the stack. public void reverse() { // Fill in. } // Returns a string representation of the linked list. public String toString() { // Fill in. } // Returns the number of elements. public int size() { return size; } private static class Node { E data; Node next; Node(E data, Node next) { this.data = data; this.next = next; } } } 

_________________________________________________________________________

 public interface List { // Adds the specified element to the end of the list. void add(E e); // Returns the element at the specified index, // or throws an IndexOutOfBoundsException if the index is out of range. E get(int index); // Removes and returns the element at the specified index, // or throws an IndexOutOfBoundsException if the index is out of range. E remove(int index); // Returns the number of elements. int size(); } 

________________________________________________________________________

 public interface Stack { // Adds element e to the top of the stack. void push(E e); // Removes and returns the top element of the stack, or returns null if the stack is empty. E pop(); // Returns but does not remove the top element of the stack, or returns null if the stack is empty. E top(); // Reverses the order of all the elements of the stack. void reverse(); // Returns the number of elements. int size(); } 

_____________________________________________________________________

 public class HW1 { public static void main(String[] args) { List list = new LinkedList<>(); list.add(3); // [3] list.add(5); // [3, 5] list.add(7); // [3, 5, 7] System.out.println(list); list.remove(0); // [5, 7] System.out.println(list); list.remove(1); // [5] list.add(9); // [5, 9] for (int i = 0; i < list.size(); ++i) System.out.print(list.get(i) + " "); System.out.println(); System.out.println(); Stack stack = new LinkedList<>(); stack.push(3); // [3] stack.push(5); // [5, 3] stack.push(7); // [7, 5, 3] System.out.println(stack); stack.reverse(); // [3, 5, 7] System.out.println(stack); System.out.println(stack.top()); stack.pop(); // [5, 7] System.out.println(stack); } } 

__________________________________________________

Output:

[ 3 5 7 ] [ 5 7 ] 5 9 [ 7 5 3 ] [ 3 5 7 ] 3 [ 5 7 ] 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Graph Databases In Action

Authors: Dave Bechberger, Josh Perryman

1st Edition

1617296376, 978-1617296376

More Books

Students also viewed these Databases questions