Question

In: Computer Science

Array-Based Linked List Implementation: JAVA Decide how to write the methods with items being stored in...

Array-Based Linked List Implementation: JAVA

Decide how to write the methods with items being stored in an array. NOT in linked List.
Implement an array-based Linked List in your language. Use double as the item. You need to create a driver includes several items and inserts them in order in a list.

Identify the necessary methods in a List Linked implementation. Look at previous Data Structures (stack or queue) and be sure to include all necessary methods.
DO NOT USE your language's Library List. You will receive zero points.
Write a LinkedList class:
class LinkList {
// attributes
// size
// maxSize
// array   
// CONSTRUCTORS
// METHODS   
// TOSTRING/DISPLAY--returns string }

Write a driver (tester) or write unit tests to test all your methods.
Test add methods
Test delete methods
Test size
Test print
Test adding to full list--handle exception
Test adding to empty list--handle exception

Solutions

Expert Solution

// LinkList.java

public class LinkList {
  
   // attributes
   private int data[];
   private int size;
   private int maxSize;

   // CONSTRUCTORS
  
   // default constructor that creates an empty list of capacity 100
   public LinkList()
   {
       this(100);
   }
  
   // parameterized constructor that creates an empty list of capacity
   public LinkList(int capacity)
   {
       if(capacity < 0) // capacity < 0, create list of size 100
           maxSize = 100;
       else
           maxSize = capacity;
       size = 0;
       data = new int[maxSize];
   }
  
   // METHODS   
   // method to insert item at the end of list
   public void addLast(int item)
   {
       // list is not full
       if(!isFull())
       {
           data[size] = item;
           size++;
          
       }else   // list is full, throw exception
           throw new IllegalStateException("List is full!!!");
   }
  
   // method to insert item at the front of list
   public void addFirst(int item)
   {
       // list is not full
       if(!isFull())
       {
           for(int i=size;i > 0;i--)
               data[i] = data[i-1];
           data[0] = item;
           size++;
          
       }else   // list is full, throw exception
           throw new IllegalStateException("List is full!!!");
   }
  
   // method to remove and return the front element of the list
   public int removeFirst()
   {
       if(!isEmpty()) // list is not empty
       {
           int item = data[0]; // get the front element

           // loop to shift all elements to left by 1 position from index 1 to end
           for(int i=0;i<size-1;i++)
           {
               data[i] = data[i+1];
           }
           size--;
           return item;
          
       }else // empty list, throw exception
           throw new IllegalStateException("List is empty!!!");
   }


// method to remove and return the last element of the list
   public int removeLast()
   {
       if(!isEmpty()) // list is not empty
       {
           size--; // remove the last element
           return data[size]; // return the last element
          
       }else // empty list, throw exception
           throw new IllegalStateException("List is empty!!!");
   }

   // method to return true if list is empty else return false
   public boolean isEmpty()
   {
       return size == 0;
   }
  
   // method to return true if list is full else return false
   public boolean isFull()
   {
       return size == maxSize;
   }
  
   // method to return string representation of the list
   public String toString()
   {
       String str = "[";
       if(!isEmpty())
       {
           for(int i=0;i<size;i++) {
               str += data[i];
               if(i < size-1)
                   str += ", ";
           }
       }
      
       str += "]";
       return str;
   }
  
   // method to return the number of elements of the queue
   public int size()
   {
       return size;
   }

}

//end of LinkList.java

// LinkListTester.java

public class LinkListTester {
  
   public static void main(String[] args) {

       // create a list of size 10
       LinkList list = new LinkList(10);
      
       // insert 5 elements at the front
       for(int i=1;i<=5;i++)
           list.addFirst(i);

       // insert 5 elements at the end
       for(int i=6;i<=10;i++)
           list.addLast(i);
      
       // display the list
       System.out.println("List: "+list); // [5, 4, 3, 2, 1, 6, 7, 8, 9, 10]
       System.out.println("Size: "+list.size()); // 10
      
       // try to add to a full list
       try
       {
           System.out.println("Adding to full list:");
           list.addLast(11);
       }catch(IllegalStateException e)
       {
           System.out.println(e.getMessage());
       }
      
       // loop to empty the list
       while(!list.isEmpty())
       {
           System.out.println("Element removed from first: "+list.removeFirst());
           System.out.println("Element removed from last: "+list.removeLast());
       }
      
       // try to remove from an empty list
       try
       {
           System.out.println("Removing from an empty list:");
           list.removeLast();
       }catch(IllegalStateException e)
       {
           System.out.println(e.getMessage());
       }
      
       System.out.println("Size: "+list.size());
       System.out.println("List: "+list);
   }

}

//end of LinkListTester.java

Output:


Related Solutions

Write an array-based implementation of the ADT list that expands the size of the array of...
Write an array-based implementation of the ADT list that expands the size of the array of list entries as needed so that the list can always accommodate a new entry. Also reduce the size of the array as needed to accommodate several removals. When the size of the array is greater than 20 and the number of entries in the list is less than half the size of the array, reduce the size of the array so that it is...
JAVA Write a class for a Stack of characters using a linked list implementation. Write a...
JAVA Write a class for a Stack of characters using a linked list implementation. Write a class for a Queue of characters using a linked list implementation. Write a class for a Queue of integers using a circular array implementation.
Using the implementation of the array based list given, write a CLIENT method (that is NOT...
Using the implementation of the array based list given, write a CLIENT method (that is NOT part of the class) called replace, that given a value and a position replaces the value of the element at that position. REMEMBER error checking public static void replace( List aList, int newValue, int position) public class ArraybasedList implements MyListInterface{ private static final int DEFAULTSIZE = 25; // Data members: private int currentSize; private int maxSize; private S[] elements; //default constructor has NO parameters...
Create an array-based implementation of a binary tree. (WRITE IN JAVA) DON'T FORGET TO INCLUDE PSEUDOCODE...
Create an array-based implementation of a binary tree. (WRITE IN JAVA) DON'T FORGET TO INCLUDE PSEUDOCODE AND UML DIAGRAM
JAVA: Provide two different implementations, an array and a linked list, to maintain a list of...
JAVA: Provide two different implementations, an array and a linked list, to maintain a list of names (two separate programs).The following operations are available: insert rear, insert front, remove a particular element, and print the whole list. Do not implement an ADT(Do not use a class with data and operations) Just set up a fixed size array or a linked list of nodes in main and provide code in main or functions/static methods to perform insert, remove, and print. You...
Java The List ADT has an interface and a linked list implementation whose source code is...
Java The List ADT has an interface and a linked list implementation whose source code is given at the bottom of this programming lab description. You are to modify the List ADT's source code by adding the method corresponding to the following UML: +hasRepeats() : boolean hasRepeats() returns true if the list has a value that occurs more than once in the list hasRepeats() returns false if no value in the list occurs more than once in the list For...
Java The List ADT has an interface and a linked list implementation whose source code is...
Java The List ADT has an interface and a linked list implementation whose source code is given at the bottom of this programming lab description. You are to modify the List ADT's source code by adding the method corresponding to the following UML: +hasRepeats() : boolean hasRepeats() returns true if the list has a value that occurs more than once in the list hasRepeats() returns false if no value in the list occurs more than once in the list For...
. Implement your own custom linked list array implementation with the following changes: (a) Fill in...
. Implement your own custom linked list array implementation with the following changes: (a) Fill in the public E get(int index) method (b) Also add code to the get method to print out a message for each time an element in the list is checked while searching for the element You may want to study how the toString method goes from element to element in the list Java
I need this written in Java, it is a Linked List and each of it's Methods....
I need this written in Java, it is a Linked List and each of it's Methods. I am having trouble and would appreciate code written to specifications and shown how to check if each method is working with an example of one method being checked. Thank you. public interface Sequence <T> { /** * Inserts the given element at the specified index position within the sequence. The element currently at that * index position (and all subsequent elements) are shifted...
Java Generic 2D Linked List Problem How to convert a 1D linked List into multiple linked...
Java Generic 2D Linked List Problem How to convert a 1D linked List into multiple linked lists with sequential values together? //Example 1: [1,1,2,3,3] becomes [[1,1],[2],[3,3]] //Example 1: [1,1,2,1,1,2,2,2,2] becomes [[1,1],[2],[1,1],[2,2,2,2]] //Example 3: [1,2,3,4,5] becomes [[1],[2],[3],[4],[5]] public <T> List<List<T>> convert2D(List<T> list) { // Given a 1D, need to combine sequential values together. }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT