Create an ordered linked list As for the style and documentation, please don\'t
ID: 3854331 • Letter: C
Question
Create an ordered linked list
As for the style and documentation, please don't return or break from inside a loop. Also, please add lots of comments so that I know what you're doing.
Package name should be "assign6"
ui class name File to submit: Ordered LinkedList.java OrderedLinkedList list: node - count: int + OrderedLinkedList0 + numEntries0: int + insert (int): void + delete (int): void* + toString0: String + get(int): int * Throws exception Required methods 1. OrderedList 0 2. numEntries0:int 3. insert (int): void a. create an empty linked list of integers a. return the number of integers currently stored in the list a. b. insert an integer into the list so that the elements are in ascending order do not allow for duplicates in the array 4. delete (int): void find the integer in the list and remove it if the value is not in the array, throw an exception with the string containing the number that could not be deleted and the message “Delete not successful" if the value is in the array, remove it so that the elements remain in ascending order a. b. c. 5. toString0: String return the values in the array so that all values are separated by a space the string should be “1 2 3" not" 1 2 3 “ an empty list should return an empty string (0 characters) a. b, c. 6. get(int): int a. b. find the integer in the list at the position indicated and return it if the value is not in the list, throw an exception with the string containing the number of the position and the message "Get not successful" the position of the nodes starts at 0, not 1 c.Explanation / Answer
I hope this helps you. :)
package assign6;
/**
*
* @author Sam
*/
public class OrderedLinkList {
private class IntNode {
public int info;
public IntNode next;
public IntNode(int info, IntNode next) {
this.info = info;
this.next = next;
}
}
private IntNode list;
private int count;
public OrderedLinkList() { //simple costructor
list = null;
count = 0;
}
public int numEntries(){ //simple
return count;
}
public void insert(int info) { //insert
if (list == null){ //insert 1st item
list = new IntNode(info, null);
return;
}
if (list.info > info) { //item ti insert is less the the fist item and will be inserted at position 1
list = new IntNode(info, list);
return;
}
IntNode tmp = list;
while (tmp.next != null){ //find the corrent position, when the next item is just grater than info
if (tmp.next.info > info) //position found
break;
tmp = tmp.next;
}
tmp.next = new IntNode(info, tmp.next); //insert data just after tmp
}
public void delete(int info){ //delete node with given info
if (list == null) { //case when the list is empty
throw new NullPointerException("Delete not successful");
}
if (list.info == info) { //first item is the info, we need to change the list itsefl
list = list.next;
return;
}
IntNode tmp = list; //tmp pointer
while (tmp.next != null){ //shift tmp untill the next item is the target
if (tmp.next.info == info) {
tmp.next = tmp.next.next; //remove link to target
return; //quit
}
tmp = tmp.next;
}
throw new IllegalArgumentException("Delete not successful"); //if not found in the loop, we throw error
}
public int get(int index) { //get the element at poistion index
if (index >= count) //out of bound condition
throw new IndexOutOfBoundsException("Count: " + count+ ". Get not Successful");
IntNode tmp = list; //tmp is temporary current pointer
for (int i = 0; i<index; i++) //shift index number of times to reach node with position index
tmp = tmp.next;
return tmp.info;// return info to the tmp
}
@Override
public String toString() {
String out = "";
if (list == null)
return out;
IntNode tmp = list;
while (tmp.next != null) {
out += tmp.info + " ";
tmp = tmp.next;
}
out += tmp.info;
return out;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.