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

------------------------------------------------------------------ TestQuestion1

ID: 3701483 • Letter: #

Question

------------------------------------------------------------------

TestQuestion1:

import java.util.Arrays;

public class TestQuestion1 {
public static void main(String [] args) {
  
int []testData = {1,2,3,4,5};
IntLinkedList list = new IntLinkedList(testData);
  
System.out.println("These two lists should be in the same order:");
System.out.println(list.toString());
System.out.println(Arrays.toString(testData));
System.out.println("Is the above list empty? " + list.empty());

IntLinkedList list2 = new IntLinkedList();
System.out.println(" List 2: "+list2);
System.out.println("Is List 2 empty? " + list2.empty());
  
  
list.remove(4);
list.remove(0);
System.out.println(" Should contain 2,3,4:");
System.out.println(list);
  
// will the code crash?
list.remove(0);
list.remove(0);
  
// Next test
int []testData2 = {7,4,5,15,0,1};
IntLinkedList list3 = new IntLinkedList(testData2);
System.out.println(" The new list: " + list3);
//System.out.println(" The new list reversed: " + list2.printReverse());
  
// clone some data
IntLinkedList cloned = list3.clone();
System.out.println(" Old list and new list should be identical:");
System.out.println("Old: " + list3);
System.out.println("New: " + cloned);
  
cloned.add(88);
cloned.add(99);
System.out.println(" The old list should be unchanged:");
System.out.println("Old: " + list3);
System.out.println("New: " + cloned);
  
  
// remove elements that don't exist
cloned.remove(-1);
cloned.remove(100);
System.out.println(" The new list should be unchanged:");
System.out.println("New: " + cloned);



}
}

Question 1: Linked Lists Create an IntLinkedList class, much like the one presented in class. It should implement a linked list that will hold values of type int. It should use an IntNode class to implement the nodes in the linked list. The linked list must have the following methods: A constructor with no parameters which creates an empty list. . void add(int data)- adds data to the front of the list. A constructor IntLinkedList(intl ) which will create a linked list containing the same values as the int ] array, and in the same order. void remove(int index) -remove one node from the list, at the specified index, starting at 0. If the index is invalid, remove nothing. String toString() - return a String containing the integer values, in the order they appear in the list, with surrounding them, and commas separating them. There should not be a comma after the last integer For example,??12,9,18>> not??12, 9, 18,>>) boolean empty)-returns true if the list is empty, and false otherwise. IntLinkedList clone() return a deep copy of the linked list, with a new set of nodes. The data should be in the same order as the data in the original list. For full marks, your code must be efficient, and must not go through the nodes of any list more than once.

Explanation / Answer

public class IntNode
{
    public int data ;
    public IntNode next ;

    public IntNode(int data, IntNode next)
    {
        this.data = data ;
        this.next = next ;
    }
}
================

public class IntLinkedList {

private IntNode first;
private int count;

public IntLinkedList(int[] array) {
  createList(array);
}

public IntLinkedList() {
  
}

void add(int i) {
  IntNode node = new IntNode(i, first);
  first = node;
}

void createList(int[] arr) {
  first = new IntNode(arr[0], null);
  boolean skipFirst = true;
  IntNode temp = first;
  count = 1;
  for (int i : arr) {
   if (skipFirst == true) {
    skipFirst = false;
   } else {
    IntNode node = new IntNode(i, null);
    temp.next = node;
    temp = temp.next;
    count++;
   }

  }
  //first = temp;
}

void remove(int index) {
  if (first == null)
   return;
  if (index == 0) {
   first = first.next;
   return;
  }
  if(index > count) {
   return;
  }
  int cnt = 0;
  IntNode current = first;
  for(;cnt < index;cnt++) {
   
   current = current.next;
   //cnt++;
  }
  
  IntNode nodeToRemove = current.next;
  if(current.next!=null) {
   IntNode nextToRemove = current.next.next;
   current = nextToRemove;
  }
}

boolean empty() {
  return (count > 0) ? false : true;
}

protected IntLinkedList clone() {
  IntLinkedList intList = new IntLinkedList();

  IntNode temp = first;
  intList.first = first;
  IntNode inListtemp = intList.first;
  while (temp != null) {
   inListtemp.next = temp.next;
   inListtemp = inListtemp.next;
   temp = temp.next;
  }
  return intList;
}

public String toString() {
  String output="";
  IntNode temp = first;
  while(temp!=null) {
   output+=temp.data+" ";
   temp = temp.next;
  }
  
  return output;
}

}

=================

Output

These two lists should be in the same order:
1 2 3 4 5
[1, 2, 3, 4, 5]
Is the above list empty? false

List 2:
Is List 2 empty? true

Should contain 2,3,4:
2 3 4 5

The new list: 7 4 5 15 0 1

Old list and new list should be identical:
Old: 7 4 5 15 0 1
New: 7 4 5 15 0 1

The old list should be unchanged:
Old: 7 4 5 15 0 1
New: 99 88 7 4 5 15 0 1

The new list should be unchanged:
New: 99 88 7 4 5 15 0 1