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

Really my Turkish instructor has problems with communicating the material. He\'s

ID: 3664682 • Letter: R

Question

Really my Turkish instructor has problems with communicating the material. He's still drawing children's pictures, I need some help in uderstanding this linked ds method. Here are the instructions. In the code below only the section for the method can be made and altered.(IN BOLD) I am providing the SLList and Tester class codes.

You need to implement one method only; insertAfter method. This method takes 2 integer parameters. Find the first parameter in the list. If found, insert the second parameter after the found node.

Here is the SLList:

public class SLList {

public class Node {
private int info = 0;
private Node next = null;

public Node(int el) {
info = el;
}
public Node(int el, Node ref) {
info = el;
next = ref;
}
}
private Node head;

public SLList() {
head = null;
}

public void addToHead(int el) {
head = new Node(el, head);
}

public void printAll() {
if(head==null){
System.out.println("List is empty.");
}
else{
Node tmp = head;
do{
System.out.print(tmp.info + " ");
tmp = tmp.next;
}while(tmp != null);
System.out.println();
}
}

private Node find(int target) {
Node position = head;
while (position != null) {
if (position.info ==target)
return position;
position = position.next;
}
return null; //target was not found
}

// Do not modify any code above this line.
// find the first number in the list.
// Insert the second parameter after the found node.
// Write insertAfter method here.

}

Here is the Tester class:

public class P4 {
public static void main(String[] args) {
System.out.println("This program is written by . ");
SLList list = new SLList();
list.addToHead(41);
list.addToHead(30);
list.addToHead(20);
list.addToHead(10);
list.printAll();
list.insertAfter(20, 100);
list.printAll();
list.insertAfter(5, 6);
list.printAll();
}
}

This is the desired output.

10 20 30 41 100 was inserted after 20 10 20 100 30 41 5 was not found. 10 20 100 30 41

Explanation / Answer

//requested function

// Write createListForProject3 method here.
public List createListForProject3(){
Node tmp = head;
List Result = new List();
  
while(tmp != null){
if (tmp.data % 10 != 0)
Result.addToHead(tmp.data);
tmp = tmp.next;
}
return Result;
}

// full code

public class List {
private class Node {
private int data;
private Node next;
public Node(int d, Node nodePar){
data = d;
next = nodePar;
}
}

private Node head;

public List(){
head = null;
}
// Do not modify any code above this line.

// Write createListForProject3 method here.
public List createListForProject3(){
Node tmp = head;
List Result = new List();
  
while(tmp != null){
if (tmp.data % 10 != 0)
Result.addToHead(tmp.data);
tmp = tmp.next;
}
return Result;
}


public static void displayMyName(){
System.out.println("Programmed by name. ");
}

public void addToHead(int dataPar){
head = new Node(dataPar, head);
}

public void displayList(){
Node tmp = head;
while(tmp != null){
System.out.print(tmp.data+ " ");
tmp = tmp.next;
}
System.out.println();
}
  
public static void main(String[] args) {
List.displayMyName();
List list1 = new List();
list1.addToHead(8);
list1.addToHead(20);
list1.addToHead(6);
list1.addToHead(4);
list1.addToHead(50);
list1.addToHead(2);
list1.displayList();
List list2 = list1.createListForProject3();
list1.displayList();
list2.displayList();
System.out.println("Good Bye..");
}
}