Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please include the main class. Question 1 : Write a method in the LinkedList class ( name it searchList ) to search for a value
Please include the main class.
Question :
Write a method in the LinkedList class name it searchList to search for a
value in the linked list and print on screen the the location index of the
value. If the value does not exist in the linked list the method should print
a message stating that the value was not found.
Question :
Write a method in the LinkedList class name it printRevList to print the
elements in the list in reverse order.
Question :
Write a method in the LinkedList class name it searchRemoveLongest to
find the longest string in the list and remove it
Linked List
public class LinkedList
Reference to the head node in the list
Node head;
Node last;
public LinkedList
head null;
last null;
public boolean isEmpty
return head null;
public int size
int count ;
Node p head;
while p null
There is an element at p
count;
p pnext;
return count;
public void addString e
if isEmpty
head new Nodee;
last head;
else
Add to end of existing list
last.next new Nodee;
last last.next;
public void addint index, String e
if index index size
throw new IndexOutOfBoundsException;
Index is at least
if index New element goes at beginning
head new Nodee head;
if last null
last head;
return;
Set pred to node that will be the predecessor
of the new node
Node pred head;
for int k ; k index ; k
pred pred.next;
Splice in a node containing the new element
pred.next new Nodee pred.next;
Is there a new last element
if prednext.next null
last pred.next;
public String removeint index
if index index size
throw new IndexOutOfBoundsException;
String element; The element to return
if index Removal of first item in the list
element head.value;
head head.next;
if head null
last null;
return element;
To remove an element other than the first
find the predecessor of the element to be removed.
Node pred head; Move pred forward index times
for int k ; k index ; k
pred pred.next;
Store the element to return
element pred.next.value;
Route link around the node to be removed
pred.next pred.next.next;
Check if pred is now last
if prednext null
last pred;
return element;
public void print
Node ref head;
while ref null
System.out.printlnrefvalue ;
ref ref.next;
static class Node
String value;
Node next;
NodeString val, Node n
value val;
next n;
NodeString val
value val;
next null;
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