Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class TestLL { public static void main(String[] args){ // create an empty linked list LList list = new LList(); System.out.print(empty list : ); System.out.println(list);

image text in transcribed

public class TestLL{ public static void main(String[] args){ // create an empty linked list LList list = new LList(); System.out.print("empty list : "); System.out.println(list); // create a list with one element // list = [cat] list = new LList(new Node("cat")); System.out.print("singleton : "); System.out.println(list); // add some elements to the back and front list.addBack("dog"); list.addFront("owl"); list.addBack("bat"); System.out.print("some adds : "); System.out.println(list); // abritrary adds try{ list.add(1, "crow"); }catch(OutOfBoundsException e){ System.out.println(e); } try{ list.add(1, "goat"); }catch(OutOfBoundsException e){ System.out.println(e); } try{ list.add(2, "eel"); }catch(OutOfBoundsException e){ System.out.println(e); } System.out.print("more adds : "); System.out.println(list); // some gets System.out.println("x0 = " + list.get(0)); System.out.println("x1 = " + list.get(1)); System.out.println("x5 = " + list.get(5)); System.out.println("xn = " + list.get(list.getSize()-1)); // removes list.removeFront(); list.removeFront(); System.out.println("removes : " + list); // removes list.remove(3); list.remove(list.getSize()-1); System.out.println("removes : " + list); // remove front add to back System.out.println("before : " + list); System.out.println("move front to back "); list.addBack( list.removeFront() ); System.out.println("after : " + list); LList l1 = new LList(new Node("a")); LList l2 = new LList(new Node("a")); try{ l1.addFront("b").addBack("c").add(1,"d"); l1.addFront("b").addBack("c").add(1,"eeee"); }catch(OutOfBoundsException e){ System.out.println(e); } System.out.println( "l1.compareTo(l2) = " + l1.compareTo(l2)); // uncomment this next line // System.out.println( "same(l1,l2) = " + same(l1,l2)) ; } }
Generics All of the classes in the Java Colections Framework (JCF) use generics. As we saw w ith the cosparable interface, one purpose of this is that it lats us specify w hich type of objects we are deaing w ith (we can think of the class as having a parameter that spec?ies a type inside it). Note that w hen using generics we can only use reference data types. Java does not alow us to use primitive data types w ith generics (and this is one very good reason we have the primitive w rapper classes). We w ill cover generics in more detail in class later ArrayList The arraylist is a commorly used class in the the JCF It implements the list abstract data type (ADT) https:docs.oracle.comjavase/8idocs/apijavali ArrayList.hm Open up a web brow ser and see what methods the ArrayList class provides. In particular, look at add(E eadint index, E e) get(int index), renove(int index), set(int index, E e?, and size(). Note, E is the type you specify when creating the ArrayList (as follow s) isport java.uts.Arraylist eapty string 1ist ( is String here) ArrayListeString slist new ArraylisteStrin0 eapty integer list (E is Integer here)* ArrayListeinteger ilist new ArrayListeinteger> Modiy your TesELL program so that for each linked list in the program you have a mirror list that is an ArrayList. Use the same operations (w hen applcable) to create the ArrayLists. 2. Overload the sase method to take an LList and an ArraylisteString> that checks if tw o lists are the same

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

Database Principles Programming And Performance

Authors: Patrick O'Neil

1st Edition

1558603921, 978-1558603929

More Books

Students also viewed these Databases questions