A Objectives To Change 2 Methods In The Unsortedlist Adts For Array And Implement A Tostring Method To Implement A New 1 (90.39 KiB) Viewed 28 times
A Objectives To Change 2 Methods In The Unsortedlist Adts For Array And Implement A Tostring Method To Implement A New 2 (36.96 KiB) Viewed 28 times
A Objectives To Change 2 Methods In The Unsortedlist Adts For Array And Implement A Tostring Method To Implement A New 3 (27.4 KiB) Viewed 28 times
A Objectives To Change 2 Methods In The Unsortedlist Adts For Array And Implement A Tostring Method To Implement A New 4 (31.08 KiB) Viewed 28 times
a Objectives: To change 2 methods in the UnsortedList ADTs for Array and implement a toString method. To implement a new method to merge 2 lists into one for the linkedlist implementation To test the Unsorted List ADTs with driver programs Task 1: UnsortedList ADT - Array based (40 marks) Note that ID are the rightmost 2 digits of you ID Use the Unsorted List class ADT using an array based implementation to store the list data items and implement the following (for IDs = 00, 05 and 09 use 10, 15 and 19 instead) 1. Fix the problems with the additem method to make sure that the item is inserted only when maximum length will not be exceeded (i.e. the list is not full-print an error message then). 2. Fix the deleteltem method to ensure that it does not do anything if the item is not found in the list. 3. Implement a toString method for the class that returns a String of all the elements of the list only. 4. Write a driver program/class that allows the user to do the following (print the list after each step): a) Declare an UnsortedList (use maximum size of 10) b) Initialize the list to have the items [ID 24 11 14 8 6] using a normal array and a for loop c) Add the item 30 to the list d) getItem 8 (should be in the list) and 19 (not in the list) e) add items 31, 33, 34 and 36 to the list A Delete item ID from the list Task 2: UnsortedType ADT - LinkedList based (55 marks) - The UnsortedType class given to you is to be extended by the addition of function, mergelists(), which inputs a list (as a parameter) and merges it with the encapsulated list into a single list. 1. Write the Java code for function mergelists() using the methods of the class only. 2. Write a driver program/class that allows the user to do the following (print the list after each step): a) Declare 2 UnosortedType lists b) Do the steps b)-f) that you did for the array based list above with the same numbers (note the difference in the outputs between the two implementations). For c) use additemLast for 30. c) Add the items 22 26 27 into the second list and merge it with the first list. Task 3: Compare the big-Oh complexity of the main methods for both implementations (5 marks) Use a table for this task Sample Output Array based (Assuming ID-45): List has 6 items: 45 24 11 14 8 6 l output from tostring method underlined List after adding 30 has 7 items: 45 24 11 14 8 6 30 Item is in the list Item 19 is not in the list List is full with 10 items: 45 24 11 14 8 6 30 31 33 34 // from put.Item method List after adding 31, 33, 34, and 36 has 10 items: 45 24 11 14 8 6 30 31 33 34 List after deleting 45 has 9 items: 34 24 9 14 B 5 30 31 33 Sample Output LinkedList based (Assuming ID=45) : list has 6 items: 6 8 14 11 24 45 List after adding 30 last has 7 items: 6 8 14 11 24 45 30 Item B is in the list Item 19 is not in the list List after adding 31, 33, 34, and 36 has 11 Items: 36 34 33 31 6 8 14 11 24 45 30 List after deleting 45 has 10 items: 36 34 33 31 6 8 14 11 24 30 List 2 has 3 items: 27 26 22 List after merging has 13 items: 22 26 27 36 34 33 31 6 8 14 11 24 30
Lab4 Accompanying file (Array Based class) public class UnsortedListE private static final int MAX=10; private int size private info: public int size () return size: public boolean is Pullo tetom size=MAX} public boolean isEmpty() { return size - 0:1 public UnsortedList (this (MAX); } publie Unsortedlist(int capacity) info - (El) new Object (capacity: public void addItem Item change the code to enter that the west des noteces the maximum si info[size++j=item; public boolean getItem E item) for(int i=0; i<size 1++) 11(item-info[1]) continue: else return true: > return false; > public boolean deleteItem (E iten) = for (int 1-0: icsize: 1++) 1. (itemsinfo111) infotil-into size-1]: size-- return true; public string tostring) Wimpement this not to return the e Laba Accompanying file (Linkedlist based class} // note that some of the methods are not used but keep for revision public class UnsortedType<E> private static class Nodecies class Mode private Element: private Node<E> next: publie Node (E e, Node<E> n) + elemente: nexten: public EgetElement() { return element public Node<E> getNext() Izetare next public void setNext (Nodec> n) next- end o inner dans de private Node<E> head - nol: private Node<E> tail - soll: private int size: public UnsortedType 1 public int size() { return size public boolean 1sEmpty tizeturn size=0:1 public E First f(isempty()) return null;
Totum head.getElemento: > public Elast() sf fisEmpty()) return null; return tail.getElement(); public void addItem(s) head-new Node> {e, head); 1 (size) tail-head: size++; > public void addItemtast(e) Node<E> newest-new Nodecte, null); it (isempty()) head newest: else tail.setNext (newest); tail-newest size.+; publio E deleteItem() 1 (Empty()) return null; E answer - head.getElement): head-head.getNext(); size- if(size) tail-null; return answers > public E deleteItem (E) II (isEmpty)) return null; E answer - head.getElement() Node<E> cur - head; Node<E> prev = null; boolean found - false; while (currently answer-cur.getElement(); ito answer) found true break; Helse preveu: Curcur.next; 1 (Cound) size if (size) tail-null: head-null; } else prev.next-eur.next t(cur.nextwnull) taiteur! return answer: else return null; public E deleteItemLast 18 (isEmpty()) return null; E answer - tail.getElement(); Node<E> prev - nulli Nodec> current head; while (current.next!-null) precurrent current-current.next; II
if (prev null) { // only one node exist in the list head-tail-null; } else { prev.setNext (null); tail-prev: } size--; return answer public String toString() Node<E> cur = head; String s=""; if (head!=null) dol St-cur.getElement()+ cur cur.next, 1 while (cur!=null); } return; "; public boolean getItem(E ) Node<E> cur - head; boolean found = false; * (head!=null) dod if (e==cur.getElement()) found - true; break; cur cur.next; } while (cur.getNext()!=null); 1 return found > public void mergelists (UnsortedType ut) 7/ implement this method using a Node object that points to the head of the // second 115t and then add the elements of this new list to the encapsulated 1/list using the method addItem. After you add the element then you move the W object to the next one until you reach null }//end of class UnsortedType You must implement 2 driver classes to test your code
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!