------------------------------------------------------------------ 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);
}
}
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
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.