Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I appreciate any help given. I need help modifying the following code LinkedList to make the following corrections. In the remove method, if the index

I appreciate any help given. I need help modifying the following code LinkedList to make the following corrections.

In the remove method, if the index is exactly the same as the size, it should throw an IndexOutOfBoundsException. This code throws a NullPointerException. On line 47 it should be if(index < 0 || index >= size).

My remove method doesn't return the correct element in the case where index > 0. This code was used to test it: List list = new LinkedList<>(); list.add(3); // [3] list.add(5); // [3, 5] list.add(7); // [3, 5, 7] System.out.println(list.remove(1));

The remove method should remove and return the element and index 1, so it should return 5, but my method returns 3.

On line 57, the temp!=null is unnecessary, because that would only happen if the index is out of bounds, but that case was handled earlier.

You are using O(n) time to update last (lines 65-68), but it can be done in O(1) time.

The code doesn't seem to be using a dummy node, so you don't need to create a new node in the constructor.

The following is the code to be corrected:

import java.util.ArrayList;

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) {

Node temp = new Node<>(e, null);

if(size == 0) {

first = temp;

last = first;

last.next = null;

++size;

return;

}

last.next = temp;

last = last.next;

last.next = null;

++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;

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) {

if(index < 0 || index > size)

throw new IndexOutOfBoundsException("Index out of bound...");

Node temp = first;

if(index == 0) {

--size;

first = first.next;

return temp.data;

}

// Find previous node of the node to be deleted

for (int i=0; temp!=null && i

temp = temp.next;

// Node temp->next is the node to be deleted

// Store pointer to the next of node to be deleted

Node next = temp.next.next;

temp.next = next; // Unlink the deleted node from list

--size;

Node t = first;

while(t.next != null) {

t = t.next;

}

last = t;

return temp.data;

}

// Adds element e to the top of the stack.

public void push(E e) {

Node temp = new Node<>(e, null);

if(size == 0) {

first = last = temp;

last.next = null;

++size;

return;

}

temp.next = first;

first = temp;

++size;

}

// Removes and returns the top element of the stack, or returns null if the stack is empty.

public E pop() {

if(size == 0) {

return null;

}

Node temp = first;

first = first.next;

--size;

return temp.data;

}

// Returns but does not remove the top element of the stack, or returns null if the stack is empty.

public E top() {

return first.data;

}

// Reverses the order of all the elements of the stack.

public void reverse() {

ArrayList list = new ArrayList<>();

while(size != 0) {

list.add(pop());

}

for(E item : list) {

push(item);

}

}

// Returns a string representation of the linked list.

public String toString() {

String str = "";

Node temp = first;

while(temp != null) {

str += temp.data.toString() + " ";

temp = temp.next;

}

return str;

}

// 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;

}

}

}

____________________________________________________________

The following classes are not to be modified in any way but are used to run the LinkedList class:

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); } }

__________________________________________________

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(); }

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

Database Concepts

Authors: David M Kroenke, David J Auer

6th Edition

0132742926, 978-0132742924

More Books

Students also viewed these Databases questions

Question

Discuss the legal framework of HRM in Canada.

Answered: 1 week ago