Answered step by step
Verified Expert Solution
Link Copied!

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 1:
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 2:
Write a method in the LinkedList class (name it printRevList) to print the
elements in the list in reverse order.
Question 3:
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 =0;
Node p = head;
while (p != null){
// There is an element at p
count++;
p = p.next;
}
return count;
}
public void add(String e){
if (isEmpty()){
head = new Node(e);
last = head;
} else {
// Add to end of existing list
last.next = new Node(e);
last = last.next;
}
}
public void add(int index, String e){
if (index 0|| index > size()){
throw new IndexOutOfBoundsException();
}
// Index is at least 0
if (index ==0){// New element goes at beginning
head = new Node(e, 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 =1; k = index -1; k++){
pred = pred.next;
}
// Splice in a node containing the new element
pred.next = new Node(e, pred.next);
// Is there a new last element ?
if (pred.next.next == null){
last = pred.next;
}
}
public String remove(int index){
if (index 0|| index >= size()){
throw new IndexOutOfBoundsException();
}
String element; // The element to return
if (index ==0){// 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 -1 times
for (int k =1; k = index -1; 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 (pred.next == null){
last = pred;
}
return element;
}
public void print(){
Node ref = head;
while (ref != null){
System.out.println(ref.value +"");
ref = ref.next;
}
}
static class Node {
String value;
Node next;
Node(String val, Node n){
value = val;
next = n;
}
Node(String val){
value = val;
next = null;
}
}
}
image text in transcribed

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_2

Step: 3

blur-text-image_3

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

More Books

Students also viewed these Databases questions

Question

1. Which position would you take?

Answered: 1 week ago