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

Please write the following program in JAVA. Thanks! Create a class named MyList.

ID: 3911699 • Letter: P

Question

Please write the following program in JAVA. Thanks!

Create a class named MyList. It will use a "Linked List” of integers as its attribute. The constructor will simply create an empty list. This will be a different type of Linked List, as all of the items will be in ascending numerical order. That means items will not necessarily be placed at the end of the list, but placed where it should be located. Your list should only contain unique items. Therefore if your add) method is passed an integer that already exists on the list, do not add it. The list should contain only one instance of an integer; no duplicate values. Note that the iterator simply uses the next) method, so if you use the iterator to find the location, you may need to keep track of the current index yourself. he following are the methods to create for this class 1. add(value) : Add the specified integer value to its appropriate place on the list 2. remove(value) : Remove the specified value from the list 3. list0 : This method uses the iterator to print the contents of the list. It should appear on the same line, separated by commas. For example: 1, 3, 4, 6, 8 Note that the last item will NOT have a comma following it. 4. maxgap0 : This method will return the largest difference between items on the list. For example, in the sample list displayed in #3 above, the largest gap is 2 (between 4 and 6 and between 6 and 8)

Explanation / Answer

Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you


MyList.java
------
import java.util.Iterator;

//a linked list containing elements in sorted order
public class MyList implements Iterable<Integer> {
class Node{
Integer value;
Node next;

Node(int val){
value = val;
}

Node(int val, Node nxt){
value = val;
next = nxt;
}
}

private Node head;

public MyList(){
head = null;
}

public void add(int val){
Node previous = null, current = head;
while(current != null){
if(current.value == val) //duplicate data, don't add
return;
if(val > current.value){
previous = current;
current = current.next;
}

else
break;
}

Node n = new Node(val, current);
if(previous == null)//to be inserted in begining
head = n;
else
previous.next = n;

}

public void remove(int value){
Node current = null;
Node previous = null;

for(current = head; current != null; current = current.next){
if(current.value.equals(value)) //found
break;
previous = current;
}

if(current == null)
return ;
else{
if(current == head){ //removing first node
head = head.next;
}
else
{
previous.next = current.next;
}
}
}

public void list(){
Iterator iter = this.iterator();
if(iter.hasNext()){
System.out.print(iter.next() );
while(iter.hasNext())
System.out.print(", " + iter.next());
}
System.out.println();
}

public int maxgap(){
int maxGap = 0;
if(head != null)
{
int num1 = head.value;
for(Node current = head.next; current != null; current = current.next){
int num2 = current.value;
int gap = num2 - num1;
if(gap > maxGap)
maxGap = gap;

num1 = num2;
}
}
return maxGap;
}
@Override
public Iterator<Integer> iterator() {
return new MyListIterator();
}
public class MyListIterator implements Iterator<Integer>{
Node current = head;
@Override
public boolean hasNext() {
return current != null;
}

@Override
public Integer next() {
Integer val = current.value;
current= current.next;
return val;
}

}
}


TestMyList.java
------------

public class TestMyList {
public static void main(String[] args) {
MyList mlist = new MyList();

System.out.println("Adding 3 6 1 8 4");
mlist.add(3);
mlist.add(6);
mlist.add(1);
mlist.add(8);
mlist.add(4);

System.out.println("The list contains ");
mlist.list();
System.out.println("The max gap is " + mlist.maxgap());

System.out.println("Removing 6");
mlist.remove(6);
System.out.println("The list contains ");
mlist.list();

System.out.println("Removing non existing 5");
mlist.remove(5);
System.out.println("The list contains ");
mlist.list();

}
}


output
-------
Adding 3 6 1 8 4
The list contains
1, 3, 4, 6, 8
The max gap is 2
Removing 6
The list contains
1, 3, 4, 8
Removing non existing 5
The list contains
1, 3, 4, 8