Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

/** * Question: * * Finish the implementation of the indexOf() method. * This me

ID: 3691371 • Letter: #

Question

/**
* Question:
*
* Finish the implementation of the indexOf() method.
* This method will return the first index of a certain value within the list,
* or -1 if the value is not in the list.
* Assume that the list is "zero-indexes." That is, the head node is at
* index 0.
*
* For instance, given the list
* 4 5 5 6
* indexOf(5) would return 1
* isInList(7) would return -1.
*
* You will also need to add the "addToEnd()" method that you
* wrote in question 5. You can simply copy and paste that
* into this class.
*
* Only add code in between the comments that say
* "Begin your code here"
* and
* "End your code here"
*
* or immediately after
* "Paste addToEnd() method here"
*/

public class LinkedList {
   // Private inner class Node
  
   private class Node{
       int data;
   Node link;
  
   @SuppressWarnings("unused")
       public Node(){
       data = Integer.MIN_VALUE;
       link = null;
   }
  
   public Node(int x, Node p){
       data = x;
       link = p;
   }
   }
   // End of Node class
  
   public Node head;
  
   public LinkedList(){
       head = null;
   }
  
   public int indexOf(int data) {
       /** Begin your code here **/
      
       /** end your code here **/
   }
  
   /** Paste addToEnd() method here **/
   public void addToEnd(int data) {
       /** Begin your code here **/
       if(head == null)
       {
       head = new Node(data, head);
       }
       else
       {
       Node position = head;
       while(position.link != null)
       {
       position = position.link;
       }
       position.link = new Node(data, null);
       }
       /** end your code here **/
   }

   public void printList(){
       Node position = head;
       if (head == null)
           System.out.println("The list is empty.");
       while(position != null){
           System.out.print(position.data + " ");
           position = position.link;
       }
       System.out.println();
   }
  
   public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
       LinkedList list = new LinkedList07();
      
       System.out.println("How many values do you want to add to the list?");
       int toAdd = input.nextInt();
       for (int i = 0; i < toAdd; i++) {
           System.out.println("Enter value " + (i + 1));
           list.addToEnd(input.nextInt());
       }
       System.out.println("Here is the list");
       list.printList();

       System.out.println("Enter a value to search for");
       int toFind = input.nextInt();
       int location = list.indexOf(toFind);
       if (location == -1) {
           System.out.println(toFind + " is not in the list");
       } else {
           System.out.println(toFind + " is at index " + location);
       }

       System.out.println("Enter another value to search for");
       toFind = input.nextInt();
       location = list.indexOf(toFind);
       if (location == -1) {
           System.out.println(toFind + " is not in the list");
       } else {
           System.out.println(toFind + " is at index " + location);
       }

       input.close();

   }

}

Explanation / Answer

Hi, indexOf method implemented in LinkedList. Please find the highlighted area for code change.

LinkedList.java


import java.util.Scanner;

public class LinkedList {
// Private inner class Node
int size = 0;
private class Node{
int data;
Node link;
  
@SuppressWarnings("unused")
public Node(){
data = Integer.MIN_VALUE;
link = null;
}
  
public Node(int x, Node p){
data = x;
link = p;
}
}
// End of Node class
  
public Node head;
public Node next;
public Node previous;
public LinkedList(){
head = null;
next = null;
previous = null;
}
  
public int indexOf(int data) {
/** Begin your code here **/
   int index = 0;

Node position = head;
while(position.link != null)
{
  
   if(position.data == data){
       return index;
   }
   index++;
position = position.link;
}
if(index == size-1 && data != position.data){
return -1;
}
else{
return index;
}
  
/** end your code here **/
}

  
/** Paste addToEnd() method here **/
public void addToEnd(int data) {
/** Begin your code here **/
if(head == null)
{
head = new Node(data, head);
}
else
{
Node position = head;
while(position.link != null)
{
position = position.link;
}
position.link = new Node(data, null);
}
size++;
/** end your code here **/
}
public void printList(){
Node position = head;
if (head == null)
System.out.println("The list is empty.");
while(position != null){
System.out.print(position.data + " ");
position = position.link;
}
System.out.println();
}
  
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
LinkedList list = new LinkedList();
  
System.out.println("How many values do you want to add to the list?");
int toAdd = input.nextInt();
for (int i = 0; i < toAdd; i++) {
System.out.println("Enter value " + (i + 1));
list.addToEnd(input.nextInt());
}
System.out.println("Here is the list");
list.printList();
System.out.println("Enter a value to search for");
int toFind = input.nextInt();
int location = list.indexOf(toFind);
if (location == -1) {
System.out.println(toFind + " is not in the list");
} else {
System.out.println(toFind + " is at index " + location);
}
System.out.println("Enter another value to search for");
toFind = input.nextInt();
location = list.indexOf(toFind);
if (location == -1) {
System.out.println(toFind + " is not in the list");
} else {
System.out.println(toFind + " is at index " + location);
}
input.close();
}
}

Output:

How many values do you want to add to the list?
4
Enter value 1
4
Enter value 2
5
Enter value 3
5
Enter value 4
6
Here is the list
4 5 5 6
Enter a value to search for
5
5 is at index 1
Enter another value to search for
7
7 is not in the list

Output for your scenario:

How many values do you want to add to the list?
3
Enter value 1
1
Enter value 2
2
Enter value 3
3
Here is the list
1 2 3
Enter a value to search for
3

3 is at index 2
Enter another value to search for
2
2 is at index 1