Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi I'm trying to finish some Java code and add an addAfter() method but am getting errors, here is the main code - I've added

Hi I'm trying to finish some Java code and add an "addAfter()" method but am getting errors, here is the main code - I've added what I think will work for the new method, thanks for any help

public class Assignment5 { private static class ourNode { String value; ourNode next;

public ourNode(String e) { value = e; next = null; }

public String getValue() { return value; }

public ourNode getNext() { return next; }

public void setNext(ourNode n) { next = n; } }

// below are Assignment5 class members private ourNode head; private int size;

public Assignment5() { head = null; size = 0; }

public boolean isEmpty() { return size == 0; }

public void addAtFront(String e) { ourNode newOne = new ourNode(e); newOne.setNext(head); head = newOne; ++size; }

public void addAfter(String existingValue, String newValue) { ourNode newValue = new ourNode(String); ourNode existingValue = new ourNode(String); //ourNode tmp = head; ourNode cur = head; ourNode previousNode = head; if(isEmpty()) { addAtFront(newValue); return; } while(cur != null) { if(cur.getValue().equals(existingValue)) { newValue.setNext(cur.getNext()); cur.setNext(newValue); ++size;

} previousNode = cur; cur = cur.getNext(); //tmp = tmp.getNext(); }

}

public void traverse() { ourNode tmp = head; while(tmp != null) { System.out.print(tmp.getValue() + "->"); tmp = tmp.getNext(); } }

public static void main(String[] args) { Assignment5 myList = new Assignment5(); myList.addAtFront("EE"); myList.addAtFront("CC"); myList.addAtFront("BB");

// insert value "DD" after the node that has value "CC" myList.addAfter("CC", "DD");

// because there is no value "XX" in the list, "AA" will be put at front myList.addAfter("XX", "AA");

myList.traverse(); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning Databases With PostgreSQL From Novice To Professional

Authors: Richard Stones, Neil Matthew

2nd Edition

1590594789, 978-1590594780

More Books

Students also viewed these Databases questions

Question

How wide are Salary Structure Ranges?

Answered: 1 week ago