Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

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

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Factory Active Database For Enterprise Computing

Authors: Schur, Stephen

1st Edition

0471558443, 9780471558446

More Books

Students also viewed these Databases questions