Given main() and an IntNode class, complete the IntList class by writing the append() and search() methods. The search() method should return the IntNode whose data value matches a given key, and null if the key is not found. The search() method should also set the position of each IntNode searched in the IntList, starting with 1. Ex: If the input is: 12 23 59 37 923 2 -1 12 the output is: 12 found in list at position 1. If the input is: 12 23 59 37 923 2 -1 68 the output is: 68 not found in list.   ______________________________________   import java.util.Scanner; public class SearchList { public static void main (String[] args) { Scanner scnr = new Scanner(System.in); IntList intList = new IntList(); IntNode curNode, foundNode; int num, searchNum; num = scnr.nextInt(); while (num != -1) { // Insert into linked list curNode = new IntNode(num); intList.append(curNode); num = scnr.nextInt(); } searchNum = scnr.nextInt(); foundNode = intList.search(searchNum); if (foundNode != null) { System.out.println(searchNum + " found in list at position " + foundNode.nodePos + "."); } else { System.out.println(searchNum + " not found in list."); } } }   ________________________________________   public class IntList { // Linked list nodes public IntNode headNode; public IntNode tailNode; public IntList() { // Front of nodes list headNode = null; tailNode = null; } // TODO: Write append method // prepend public void prepend(IntNode newNode) { if (headNode == null) { // list empty headNode = newNode; tailNode = newNode; } else { newNode.nextNode = headNode; headNode = newNode; } } // insertAfter public void insertAfter(IntNode curNode, IntNode newNode) { if (headNode == null) { // List empty headNode = newNode; tailNode = newNode; } else if (curNode == tailNode) { // Insert after tail tailNode.nextNode = newNode; tailNode = newNode; } else { newNode.nextNode = curNode.nextNode; curNode.nextNode = newNode; } } // TODO: Write search method that locates node with the same data value (dataVal) // as key and sets each node's position (nodePos) public void printIntList() { IntNode curNode; curNode = headNode; while (curNode != null) { curNode.printNodeData(); System.out.print(" "); curNode = curNode.nextNode; } } }   ______________________________   public class IntNode { public int dataVal; public IntNode nextNode; // Reference to the next node public int nodePos; public IntNode() { dataVal = 0; nextNode = null; } // Constructor public IntNode(int dataInit) { this.dataVal = dataInit; this.nextNode = null; } // Constructor public IntNode(int dataInit, IntNode newNextNode) { this.dataVal = dataInit; this.nextNode = newNextNode; } public void printNodeData() { System.out.print(this.dataVal); } }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

LAB: Finding an integer in a list (singly-linked list)

 

Given main() and an IntNode class, complete the IntList class by writing the append() and search() methods. The search() method should return the IntNode whose data value matches a given key, and null if the key is not found. The search() method should also set the position of each IntNode searched in the IntList, starting with 1.

Ex: If the input is:

12 23 59 37 923 2 -1 12

the output is:

12 found in list at position 1.

If the input is:

12 23 59 37 923 2 -1 68

the output is:

68 not found in list.
 
______________________________________
 

import java.util.Scanner;

public class SearchList {

public static void main (String[] args) {
Scanner scnr = new Scanner(System.in);

IntList intList = new IntList();

IntNode curNode, foundNode;
int num, searchNum;

num = scnr.nextInt();

while (num != -1) {
// Insert into linked list
curNode = new IntNode(num);
intList.append(curNode);
num = scnr.nextInt();
}

searchNum = scnr.nextInt();
foundNode = intList.search(searchNum);

if (foundNode != null) {
System.out.println(searchNum + " found in list at position " +
foundNode.nodePos + ".");
}
else {
System.out.println(searchNum + " not found in list.");
}
}
}

 

________________________________________

 

public class IntList {
// Linked list nodes
public IntNode headNode;
public IntNode tailNode;

public IntList() {
// Front of nodes list
headNode = null;
tailNode = null;
}

// TODO: Write append method

// prepend
public void prepend(IntNode newNode) {
if (headNode == null) { // list empty
headNode = newNode;
tailNode = newNode;
}
else {
newNode.nextNode = headNode;
headNode = newNode;
}
}

// insertAfter
public void insertAfter(IntNode curNode, IntNode newNode) {
if (headNode == null) { // List empty
headNode = newNode;
tailNode = newNode;
}
else if (curNode == tailNode) { // Insert after tail
tailNode.nextNode = newNode;
tailNode = newNode;
}
else {
newNode.nextNode = curNode.nextNode;
curNode.nextNode = newNode;
}
}

// TODO: Write search method that locates node with the same data value (dataVal)
// as key and sets each node's position (nodePos)

public void printIntList() {
IntNode curNode;

curNode = headNode;
while (curNode != null) {
curNode.printNodeData();
System.out.print(" ");
curNode = curNode.nextNode;
}
}
}

 

______________________________

 

public class IntNode {
public int dataVal;
public IntNode nextNode; // Reference to the next node
public int nodePos;

public IntNode() {
dataVal = 0;
nextNode = null;
}

// Constructor
public IntNode(int dataInit) {
this.dataVal = dataInit;
this.nextNode = null;
}

// Constructor
public IntNode(int dataInit, IntNode newNextNode) {
this.dataVal = dataInit;
this.nextNode = newNextNode;
}

public void printNodeData() {
System.out.print(this.dataVal);
}
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY