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

This is a basic Linked list in JAVA. Please it has classes for Linked list and n

ID: 3854757 • Letter: T

Question

This is a basic Linked list in JAVA. Please it has classes for Linked list and nodes, and Constructor. Also we have to throw an error , as the * comment says, please have a commented well written code to be presentable . Thanks

Required package name: assign4 Required class name: OrderedLinkedList File to submit: OrderedLinkedList.java

OrderedLinkedList

- list: node

- count: int

-
+ OrderedLinkedList()

+ numEntries(): int + insert (int): void
+ delete (int): void * + toString(): String + get(int): int *

* Throws exception Required methods

1. OrderedList ()
a. create an empty linked list of integers

2. numEntries():int

a.

insert a. b.

delete a.

b.

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)

6. get(int): int

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

return the number of integers currently stored in the list (int): void

insert an integer into the list so that the elements are in ascending order

do not allow for duplicates in the array (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

c.
5. toString(): String

Implementation requirements:

You must use singly linked list of integers as given below. You may not use a Java Collection or other data structures.

public class OrderedLinkedList { private class IntNode {

public int info; public IntNode next;

IntNode (int num, IntNode ptr) { info = num;

next = ptr; }

}

private IntNode list;

private int count; }

The toString() and get(int) methods must be implemented using recursion. You may add other private methods as needed to accomplish this.

Follow all style and documentation requirements for the class.

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;
    }
}

If any change is required, please let me know. Shall update them

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote