Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Add the following methods to your LinkedList implementations: public int lastIndexOf (T item); public boolean removeAll (T item); public void removeRange (int start, int end);

Add the following methods to your LinkedList implementations:

public int lastIndexOf (T item); public boolean removeAll (T item); public void removeRange (int start, int end);

For removeAll(), the method should return true if the item was in the data structure, false otherwise.

For reoveRange (int start, int end), make the indices both inclusive.

public class Methods

{

public static void main (String [] args)

{

LinkedList letters = new LinkedList< >();

letters.add('J');

letters.add('a');

letters.add('v');

letters.add('a');

letters.add('&');

letters.add('C');

letters.add('+');

letters.add('+');

letters.add('a');

System.out.println("Here are the characters in the data structure:");

System.out.println(letters);

System.out.println("Removing items from indices 4 to 6");

letters.removeRange(4,6);

System.out.println(letters);

}

}

/*

* interface for a list

*

* ordered collection with duplicates allowed

*/

public interface List

{

void add(T item);

boolean remove (T item);

boolean contains(T item);

int size();

String toString();

// list-specific methods

void add (int index, T item);

T get (int index);

T remove (int index);

T set(int index, T item);

int indexOf(T item);

}

/*

* LinkedList

*

* linked implementation of a list

*

* ordered collection with duplicates allowed

*/

public class LinkedList implements List

{

private class Node

{

private T data;

private Node next;

public Node(T item)

{

data = item;

next = null;

}

}

private Node head;

private Node tail;

private int size;

public LinkedList()

{

head = null;

tail = null;

size = 0;

}

/*

* adds item to list

*/

public void add (T item)

{

checkForNull(item);

Node newest = new Node (item);

if (size == 0)

{

head = newest;

}

else

{

tail.next = newest;

}

tail = newest;

size++;

}

/*

* removes item from list

*

* returns true if item found in list

*/

public boolean remove (T item)

{

Node previous = null;

Node current = head;

while (current != null)

{

if(current.data.equals(item)){

current = current.next;

if (previous == null)

previous = current;

else previous.next = current;

size--;

return true;

} else {

previous = current;

current = current.next;

}

}

return false;

}

/*

* returns true if item is in list

*/

public boolean contains (T item)

{

Node current = head;

for (int i = 0; i < size; i++)

{

if (current.data.equals(item))

{

return true;

}

current = current.next;

}

return false;

}

/*

* returns size of list

*/

public int size()

{

return size;

}

/*

* returns string representation of list

*/

public String toString()

{

if (size == 0)

{

return "[ ]";

}

String s = "[";

Node current = head;

while (current.next != null)

{

s+= current.data + ", ";

current = current.next;

}

return s + current.data + "]";

}

/* list-specific methods */

/*

* adds item at specified index

*/

public void add (int index, T item)

{

checkForNull(item);

checkIndex(index);

Node newest = new Node (item);

if (index == 0)

{

newest.next = head;

head = newest;

}

else

{

Node current = getNode(index-1);

newest.next = current.next;

current.next = newest;

}

size++;

}

/*

* replaces item at specified index with new value

*

* returns original value

*/

public T set (int index, T item)

{

checkForNull(item);

checkIndex(index);

Node current = getNode(index);

T removed = current.data;

current.data = item;

return removed;

}

/*

* removes item at specified index

*

* returns removed item

*/

public T remove (int index)

{

checkIndex(index);

if(index == 0) {

Node current = head;

head = head.next;

return current.data;

}

Node current = head;

int i=0;

while(i < index-1){

i++;

current = current.next;

}

Node nextNode = current.next;

current.next = current.next.next;

return nextNode.data;

}

/*

* returns item at specified index

*/

public T get (int index)

{

checkIndex(index);

return getNode(index).data;

}

/*

* returns index of specified item

*

* returns -1 if item not in list

*/

public int indexOf (T item)

{

Node current = head;

for (int i = 0; i < size; i++)

{

if (current.data.equals(item))

{

return i;

}

current = current.next;

}

return -1;

}

/* helper methods */

/*

* checks to make sure item isn't null

*/

private void checkForNull (T item)

{

if (item == null)

{

throw new IllegalArgumentException ("null not a possible value!");

}

}

/*

* checks to make sure index falls within list

*/

private void checkIndex (int index)

{

if (index < 0 || index >= size)

{

throw new IndexOutOfBoundsException("index out of range!");

}

}

/*

* returns pointer to node at specified index

*/

private Node getNode (int index)

{

Node current = head;

for (int i = 0; i < index; i++)

{

current = current.next;

}

return current;

}

}

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

Concepts of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

7th edition

978-1111825911, 1111825912, 978-1133684374, 1133684378, 978-111182591

More Books

Students also viewed these Databases questions