import java.util.Scanner; /** * Question 6: * * Finish the implementation of the
ID: 3691365 • Letter: I
Question
import java.util.Scanner;
/**
* Question 6:
*
* Finish the implementation of the isInList() method.
* This method will return true if a certain value is in the list.
* Otherwise it will return false.
*
* For instance, given the list
* 4 5 6
* isInList(5) would return true.
* isInList(7) would return false.
*
* 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 LinkedList06 {
// 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 LinkedList06(){
head = null;
}
public boolean isInList(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;
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
LinkedList06 list = new LinkedList06();
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();
if (list.isInList(toFind)) {
System.out.println(toFind + " is in the list");
} else {
System.out.println(toFind + " is not in the list");
}
System.out.println("Enter another value to search for");
toFind = input.nextInt();
if (list.isInList(toFind)) {
System.out.println(toFind + " is in the list");
} else {
System.out.println(toFind + " is not in the list");
}
input.close();
}
}
Explanation / Answer
import java.util.Scanner;
/**
* Question 6:
*
* Finish the implementation of the isInList() method. This method will return
* true if a certain value is in the list. Otherwise it will return false.
*
* For instance, given the list 4 5 6 isInList(5) would return true. isInList(7)
* would return false.
*
* 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 LinkedList06 {
// 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 LinkedList06() {
head = null;
}
/**
* checks the data is in the list or not
*
* @param data
* @return
*/
public boolean isInList(int data) {
/** Begin your code here **/
Node position = head;
if (head == null)
System.out.println("The list is empty.");
while (position != null) {
if (position.data == data)
return true;
else
position = position.link;
}
/** end your code here **/
return false;
}
/** 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;
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
LinkedList06 list = new LinkedList06();
System.out.print("How many values do you want to add to the list?");
int toAdd = input.nextInt();
for (int i = 0; i < toAdd; i++) {
System.out.print("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();
if (list.isInList(toFind)) {
System.out.println(toFind + " is in the list");
} else {
System.out.println(toFind + " is not in the list");
}
System.out.println("Enter another value to search for");
toFind = input.nextInt();
if (list.isInList(toFind)) {
System.out.println(toFind + " is in the list");
} else {
System.out.println(toFind + " is not in the list");
}
input.close();
}
}
OUTPUT:
How many values do you want to add to the list?9
Enter value 1 :5
Enter value 2 :7
Enter value 3 :3
Enter value 4 :2
Enter value 5 :4
Enter value 6 :6
Enter value 7 :9
Enter value 8 :8
Enter value 9 :1
Here is the list
5 7 3 2 4 6 9 8 1 Enter a value to search for
19
19 is not in the list
Enter another value to search for
7
7 is in the list
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.