Question

Create a Java code that includes all the methods from the Lecture slides following the ADTs

Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS 2) Array

LECTURE SLIDE

The Array List ADT /**Simplified version of java.util. List */ public interface List<E> { /**Returns the number of elements i

Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS 2) ArrayList ADT that uses a linked list internally (call it LArrayList) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm. In the comments additionally explain the advantages of the array-based implementation over the linked- list-based implementation and vice versa. Keep your code nicely formatted and follow good programming practices. Try to make the code robust and try to use generics.
The Array List ADT /**Simplified version of java.util. List */ public interface List { /**Returns the number of elements in this list */ public int size(); /**Returns whether the list is empty. */ public boolean isEmpty() /** Inserts an element e to be at index I, shifting all elements after this. */ public void add(int I, E e) throws IndexOutOfBoundsException; /** Returns the element at index I, without removing it. */ public E get(int i) throws IndexOutOfBoundsException; /** Removes and returns the element at index I, shifting the elements after this. */ public E remove(int i) throws IndexOutOfBounds Exception; /**Replaces the element at index I with e, returning the previous element at i. */ public E set(int I, E e) throws IndexOutOfBoundsException; }
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// LArrayList.java

public class LArrayList<E> implements List<E> {

      // pointer to head node

      private Node head;

      // current size of list

      private int size;

      // constructor initializes empty list

      public LArrayList() {

            head = null;

            size = 0;

      }

      // returns the size, works in O(1) time

      public int size() {

            return size;

      }

      // returns true if list is empty, works in O(1) time

      public boolean isEmpty() {

            return size == 0;

      }

      // adds an element to particular index, worst case running complexity is

      // O(n)

      public void add(int i, E e) throws IndexOutOfBoundsException {

            // validating index

            if (i < 0 || i > size) {

                  throw new IndexOutOfBoundsException();

            }

            // creating a node

            Node node = new Node(e);

            // adding before head if i is 0

            if (i == 0) {

                  node.next = head;

                  // updating head

                  head = node;

                  size++;

                  return;

            }

            Node temp = head;

            // finding node at i-1 position

            for (int index = 0; index < i - 1; index++) {

                  temp = temp.next;

            }

            // adding node between temp and temp.next

            node.next = temp.next;

            temp.next = node;

            size++; // updating size

      }

      // returns the data at index i, works in O(n) time (worst case)

      public E get(int i) throws IndexOutOfBoundsException {

            // validating index

            if (i < 0 || i >= size) {

                  throw new IndexOutOfBoundsException();

            }

            Node temp = head;

            // finding node at index i

            for (int index = 0; index < i; index++) {

                  temp = temp.next;

            }

            return temp.data;

      }

      // removes an element from a particular index, worst case running complexity

      // is O(n)

      public E remove(int i) throws IndexOutOfBoundsException {

            //validating index

            if (i < 0 || i >= size) {

                  throw new IndexOutOfBoundsException();

            }

            //updating head node if i is 0

            if (i == 0) {

                  //storing current data of head node

                  E data = head.data;

                  head = head.next;

                  size--;

                  //returning removed data

                  return data;

            }

            Node temp = head;

            //finding node at i-1 position

            for (int index = 0; index < i - 1; index++) {

                  temp = temp.next;

            }

            //removing node at temp.next position

            E data = temp.next.data;

            temp.next = temp.next.next;

            size--;

            return data;

      }

      // updates an element at particular index, returns the old value, works in

      // O(n) time (worst case)

      public E set(int i, E e) throws IndexOutOfBoundsException {

            if (i < 0 || i >= size) {

                  throw new IndexOutOfBoundsException();

            }

            Node temp = head;

            for (int index = 0; index < i; index++) {

                  temp = temp.next;

            }

            //storing old value

            E old = temp.data;

            //replacing with new value

            temp.data = e;

            //returning old value

            return old;

      }

      // private class to represent one node

      private class Node {

            E data;

            Node next;

            public Node(E data) {

                  this.data = data;

            }

      }

}

Add a comment
Know the answer?
Add Answer to:
Create a Java code that includes all the methods from the Lecture slides following the ADTs...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a Java code that includes all the methods from the Lecture slides following the ADTs...

    Create a Java code that includes all the methods from the Lecture slides following the ADTs LECTURE SLIDES Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS: 4) Queue ADT that uses a linked list internally (call it LQueue) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm....

  • Collect/finish the Java code (interface and the complete working classes) from lecture slides for the for...

    Collect/finish the Java code (interface and the complete working classes) from lecture slides for the for the following ADT: 3) Queue ADT that uses an array internally (call it AQueue) Make sure you keep the same method names as in the slides (automatic testing will be performed)! Make sure your classes implement the corresponding interfaces. Put your classes in a package called cse11. Try to make the code robust and try to use generics. The Queue ADT The Queue ADT...

  • NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT...

    NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT WOULD CAUSE THE HW1.java TO PRINT THE RIGHT DATA.!!! The LinkedList class implements both the List interface and the Stack interface, but several methods (listed below) are missing bodies. Write the code so it works correctly. You should submit one file, LinkedList.java. Do not change the interfaces. Do not change the public method headers. Do not rename the LinkedList class. None of your methods...

  • Plz help me with the code. And here are the requirement. Thanks!! You are required to...

    Plz help me with the code. And here are the requirement. Thanks!! You are required to design and implement a circular list using provided class and interface. Please filling the blank in CircularList.java. This circular list has a tail node which points to the end of the list and a number indicating how many elements in the list. And fill out the blank of the code below. public class CircularList<T> implements ListInterface<T> { protected CLNode<T> tail; // tail node that...

  • Given the Interface Code write a java class that implements this interface and show the working...

    Given the Interface Code write a java class that implements this interface and show the working functionality in the main method: public interface CustomList<T> { /** * This method should add a new item into the <code>CustomList</code> and should * return <code>true</code> if it was successfully able to insert an item. * @param item the item to be added to the <code>CustomList</code> * @return <code>true</code> if item was successfully added, <code>false</code> if the item was not successfully added (note: it...

  • Complete an Array-Based implementation of the ADT List including a main method and show that the...

    Complete an Array-Based implementation of the ADT List including a main method and show that the ADT List works. Draw a class diagram of the ADT List __________________________________________ public interface IntegerListInterface{ public boolean isEmpty(); //Determines whether a list is empty. //Precondition: None. //Postcondition: Returns true if the list is empty, //otherwise returns false. //Throws: None. public int size(); // Determines the length of a list. // Precondition: None. // Postcondition: Returns the number of items in this IntegerList. //Throws: None....

  • Please help with this Java Program. Thank you! we will be implementing an Ordered List ADT....

    Please help with this Java Program. Thank you! we will be implementing an Ordered List ADT. Our goal is to implement the interface that is provided for this ADT. Notice that there are two interfaces: OrderedListADT builds on ListADT. In this homework, you'll only be responsible for the OrderedListADT. Figure 1: UML Overview 1 Requirements Create a doubly linked implementation of the OrderedListADT interface. Note that the book includes most of the source code for a singly linked implementation of...

  • Use JAVA language. public class DynamicArray2 { private String[] data; // the backing array private int...

    Use JAVA language. public class DynamicArray2 { private String[] data; // the backing array private int virtualArrayLength; // the number of elements in the dynamic array // Throws an IndexOutOfBoundsException if i is not a valid index // for adding to the dynamic array, otherwise inserts s at index i. // Elements can be added from index 0 to this.size(). public void add(int i, String s) { // If there is no room for s in data, create a new...

  • Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface:...

    Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface: /** * An ordered list of items. */ public interface ItemList<E> {      /**       * Append an item to the end of the list       *       * @param item – item to be appended       */ public void append(E item);      /**       * Insert an item at a specified index position       *       * @param item – item to be...

  • Java Programming: The following is my code: public class KWSingleLinkedList<E> {    public void setSize(int size)...

    Java Programming: The following is my code: public class KWSingleLinkedList<E> {    public void setSize(int size)    {        this.size = size;    }    /** Reference to list head. */    private Node<E> head = null;    /** The number of items in the list */    private int size = 0;       /** Add an item to the front of the list.    @param item The item to be added    */    public void addFirst(E...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT