Need help with a Java problem please: OrderedLinkedList.java: package orderedLis
ID: 3843952 • Letter: N
Question
Need help with a Java problem please:
OrderedLinkedList.java:
package orderedList;
public class OrderedLinkedList<Item extends Comparable<? super Item>> {
private class Node {
private Item data;
private Node next;
public Node(Item data) {
this.data = data;
this.next = null;
}
public Item getData() {
return this.data;
}
public Node getNext() {
return this.next;
}
public void setData(Item data) {
this.data = data;
}
public void setNext(Node next) {
this.next = next;
}
}
private Node head;
public OrderedLinkedList() {
head = null;
}
public void insert(Item newData) {
// Create a new Node containing newData.
// Declare two Node reference variables, previous (intialized to null)
// and current (initialized to head).
// while true {
// if current == null or newData is less then current.getData() then {
// // If current is null, that means this new node is being inserted
// // at the end. If not null, it's being inserted elsewhere.
// newNode.setNext(current)
// if previous == null then {
// // Special case: Inserting new node as first node in the linked list.
// head = newNode
// } else {
// previous.setNext(newNode)
// }
// return
// }
// previous = current
// current = current.getNext()
// }
//
}
public void remove(Item oldData) {
// Declare two Node reference variables, previous (intialized to null)
// and current (initialized to head).
// while current != null {
// if current.getData() is equal to oldData then {
// if previous == null then {
// // Special case: Removing the first node in the linked list.
// head = head.getNext()
// } else {
// previous.setNext(current.getNext())
// }
// return
// }
// previous = current
// current = current.getNext()
// }
}
public String toString() {
String s = "[";
boolean firstItem = true;
for (Node current = head; current != null; current = current.getNext()) {
if (firstItem) {
firstItem = false;
s += current.getData();
} else {
s += ", " + current.getData();
}
}
s += "]";
return s;
}
}
Create a package called orderedList. Copy into it the incompletely written reference class OrderedLinkedList ava. You will also place into a program class TestOLL.java written according to the instructions below. Specifications The ordered linked list class is a generic class that holds value in a linked list in ascending order. Looking at the code, you will see that the insert and remove methods contain pseudocode. You are to translate that pseudocode into Java. After doing that, write a program called TestOLL. java that tests your updated linked list class. It should: 1. Declare and initialize an empty ordered linked list for strings. 2. Fill it with the strings: "goodbye adios "buenos dias", "bon jour", "adieu", "guten Tag", hello and "auf Wiedersehen 3. Print the list out. This can be done in a single Stdout.println (list) statement because the method to String is defined for the linked list classExplanation / Answer
I have written the insert and remove method and commented those code.
Main Class:
Test Class:
Output:
[Hello, adieu, adios, auf Wiedersehen, bon jour, buneous dias, goodbye, guten Tag]