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.
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language...
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language library LinkedList Queue methods will call the LinkedList methods You can use string as the object Instead of using an array, as the QueueLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : enqueue(), dequeue(), size(), printQueue(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language library LinkedList Stack methods will call the LinkedList methods You can use string as the object Instead of using an array, as the StackLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : push(), pop(), size(), printStackDown(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
Data Structures in Java In the following Singly Linked List implementation, add the following methods, and...
Data Structures in Java In the following Singly Linked List implementation, add the following methods, and write test cases in another java file to make sure these methods work. - Write a private method addAfter(int k, Item item) that takes two arguments, an int argument k and a data item, and inserts the item into the list after the K-th list item. - Write a method removeAfter(Node node) that takes a linked-list Node as an argument and removes the node...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT