import java.util.Scanner; /** * Question 7: * * Finish the implementation of the
ID: 3694495 • Letter: I
Question
import java.util.Scanner; /** * Question 7: * * 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 LinkedList07 { // 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 LinkedList07(){ head = null; } public int indexOf(int data) { /** Begin your code here **/ /** end your code here **/ } /** Paste addToEnd() method 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); LinkedList07 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, I have implemented indexOf() and addToEnd() methods. Highlighted areas are the code changes.
import java.util.Scanner;
/**
* Question 7:
*
* 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 LinkedList07 {
// 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 LinkedList07(){
head = null;
}
public int indexOf(int data) {
/** Begin your code here **/
int index = 0;
Node position = head;
do{
if(position.data == data){
return index;
}
index++;
position = position.link;
}while(position.link != null);
if(position.link == null && data != position.data){
return -1;
}
else{
return index;
}
/** end your code here **/
}
/** Paste addToEnd() method 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);
LinkedList07 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();
}
}
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
------------------------------------------------------
How many values do you want to add to the list?
7
Enter value 1
1
Enter value 2
2
Enter value 3
3
Enter value 4
4
Enter value 5
5
Enter value 6
6
Enter value 7
7
Here is the list
1 2 3 4 5 6 7
Enter a value to search for
7
7 is at index 6
Enter another value to search for
8
8 is not in the list
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.