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

I have 4 errors in my source code. They are all in my last method. I need help c

ID: 3867014 • Letter: I

Question

I have 4 errors in my source code. They are all in my last method. I need help correcting them. Source code:

public class adding_After{
private Node head;
private Node tail;
  
public void adding_After(T element, T after){
  
Node tmp = head;

Node refNode = null;

System.out.println("Traversing to all nodes..");


while(true){

if(tmp == null){

break;

}

if(tmp.compareTo(after) == 0){

  
refNode = tmp;

break;

}

tmp = tmp.getNextRef();

}

if(refNode != null){

  

Node nd = new Node();

nd.setValue(element);

nd.setNextRef(tmp.getNextRef());

if(tmp == tail){

tail = nd;

}

tmp.setNextRef(nd);

}
else {

System.out.println("Unable to find the given element...");

}

}

public void delete_Front(){

if(head == null){

System.out.println("Underflow...");

}

Node tmp = head;

head = tmp.getNextRef();

if(head == null){

tail = null;

}

System.out.println("Deleted: "+tmp.getValue());

}

public void deleteAfter(T after){

Node tmp = head;

Node refNode = null;

System.out.println("Traversing to all nodes..");

  

while(true){

if(tmp == null){

break;

}

if(tmp.compareTo(after) == 0){


refNode = tmp;

break;

}

tmp = tmp.getNextRef();

}

if(refNode != null){

tmp = refNode.getNextRef();

refNode.setNextRef(tmp.getNextRef());

if(refNode.getNextRef() == null){

tail = refNode;

}

System.out.println("Deleted: "+tmp.getValue());

}
else {

System.out.println("Unable to find the given element...");

}

}

public void traverse(){

Node tmp = head;

while(true){

if(tmp == null){

break;

}

System.out.println(tmp.getValue());

tmp = tmp.getNextRef();

}

}

  
public static void main(String a[]){
  
SinglyLinkedListImpl sl = new SinglyLinkedListImpl();

sl.add(3);

sl.add(32);

sl.add(54);

sl.add(89);

sl.adding_After(76, 54);

sl.delete_Front();

sl.deleteAfter(76);

sl.traverse();

}

}

This is my Node file:

class Node<T> implements Comparable<T> {

private T value;

private Node<T> nextRef;

public T getValue() {

return value;

}

public void setValue(T value) {

this.value = value;

}

public Node<T> getNextRef() {

return nextRef;

}

public void setNextRef(Node<T> ref) {

this.nextRef = ref;

}

public int compareTo(T arg) {

if(arg == this.value){

return 0;

} else {

return 1;

}

}

}

Explanation / Answer

The issue was that your code was not organized.. The methods which you have defined in adding_after class must be there in SinglyLinkedListImpl Class.. I have updated your code and it is working fine now ..

Below is your code.. Please note that there are alot of warnings in your code because Generics are not added. Because of this reason There was error in Node class also. I have resolved it . If you want me to resolve all the warnings also Please comment, I'll do it and update the code....

adding_After.java

public class adding_After {

public static void main(String a[]) {

SinglyLinkedListImpl sl = new SinglyLinkedListImpl();

sl.add(3);

sl.add(32);

sl.add(54);

sl.add(89);

sl.adding_After(76, 54);

sl.delete_Front();

sl.deleteAfter(76);

sl.traverse();

}

}

Node.java

class Node<T> implements Comparable<T> {

private T value;

private Node nextRef;

public T getValue() {

return value;

}

public void setValue(T value) {

this.value = value;

}

public Node getNextRef() {

return nextRef;

}

public void setNextRef(Node ref) {

this.nextRef = ref;

}

public int compareTo(T arg) {

if (arg == this.value) {

return 0;

} else {

return 1;

}

}

}

SinglyLinkedListImpl.java

public class SinglyLinkedListImpl<T> {

private Node<T> head;

private Node<T> tail;

public void add(T element) {

Node<T> nd = new Node<T>();

nd.setValue(element);

System.out.println("Adding: " + element);

if (head == null) {

head = nd;

tail = nd;

} else {

tail.setNextRef(nd);

}

tail = nd;

}

public void adding_After(T element, T after) {

Node tmp = head;

Node refNode = null;

System.out.println("Traversing to all nodes..");

while (true) {

if (tmp == null) {

break;

}

if (tmp.compareTo(after) == 0) {

refNode = tmp;

break;

}

tmp = tmp.getNextRef();

}

if (refNode != null) {

Node nd = new Node();

nd.setValue(element);

nd.setNextRef(tmp.getNextRef());

if (tmp == tail) {

tail = nd;

}

tmp.setNextRef(nd);

} else {

System.out.println("Unable to find the given element...");

}

}

public void delete_Front() {

if (head == null) {

System.out.println("Underflow...");

}

Node tmp = head;

head = tmp.getNextRef();

if (head == null) {

tail = null;

}

System.out.println("Deleted: " + tmp.getValue());

}

public void deleteAfter(T after) {

Node tmp = head;

Node refNode = null;

System.out.println("Traversing to all nodes..");

while (true) {

if (tmp == null) {

break;

}

if (tmp.compareTo(after) == 0) {

refNode = tmp;

break;

}

tmp = tmp.getNextRef();

}

if (refNode != null) {

tmp = refNode.getNextRef();

refNode.setNextRef(tmp.getNextRef());

if (refNode.getNextRef() == null) {

tail = refNode;

}

System.out.println("Deleted: " + tmp.getValue());

} else {

System.out.println("Unable to find the given element...");

}

}

public void traverse() {

Node tmp = head;

while (true) {

if (tmp == null) {

break;

}

System.out.println(tmp.getValue());

tmp = tmp.getNextRef();

}

}

}

Sample Run: -

Adding: 3
Adding: 32
Adding: 54
Adding: 89
Traversing to all nodes..
Deleted: 3
Traversing to all nodes..
Deleted: 89
32
54
76