Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Java Linked list class public class LList implements Comparable { protected Node head; protected Node tail; protected int size; /* constructors */ public LList(){ head
Java
Linked list class
public class LList implements Comparable{ protected Node head; protected Node tail; protected int size; /* constructors */ public LList(){ head = tail = null; size = 0; } public LList(Node n){ head = tail = n; size = 1; } /* simple getters */ public int getSize(){ return size; } public String get(int position){ // returns data of element at index position // returns null otherwise if( position size -1 || head == null){ return null; } int count = 0; Node current = head; while(count size){ throw new OutOfBoundsException("Cannot add an element with index " + position + "into a list of size " + size); } if( position == 0){ return addFront(d); }else if( position == size ){ return addBack(d); } // find node at index position-1 Node prev = head; int count = 0; while( count ["; current = current.getNext(); } return out+current.getData() +"]"; } }
Node class
public class Node{ protected String data; protected Node next; public Node(String d){ this(d, null);} public Node(String d, Node n){ this.data = d; this.next = n; } public String getData(){ return data; } public Node getNext(){ return next; } public void setData(String d){ data = d; } public void setNext(Node n){ next = n;} @Override public String toString(){ return data; } }
OutOfBoundsClass
/** This exception should be thrown whenever a linked list with n elements tries to access an element X_m for any m>=n or m=n or mn or mTestLL classpublic 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)) ; } }SetOrList classimport java.util.HashSet; import java.util.ArrayList; public class SetORList{ public static void main(String[] args){ ArrayListlist = new ArrayList (); HashSet set = new HashSet (); int size = 100; System.out.print("making collections... "); for(int i=0; i
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started