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...
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...
write a few English sentences describing how you would change the Linked-Chain implementation of the List...
write a few English sentences describing how you would change the Linked-Chain implementation of the List ADT to support removal of a node from the middle retaining order. Write PSEUDOCODE to insert a node at position 2 in a doubly-linked list (assume position follows classic indexing from 0 to item_count - 1)
Write a pseudocode or java code implementation for linear search that searches elements based on a key value from an array of undetermined length
Write a pseudocode or java code implementation for linear search that searches elements based on a key value from an array of undetermined lengthJAVA
In C++ write an implementation of the ADT sorted list that uses a resizable array (vector...
In C++ write an implementation of the ADT sorted list that uses a resizable array (vector class of C++ STL) to represent the list items.
(a) Write a stack class that is based on a linked list. It can be just...
(a) Write a stack class that is based on a linked list. It can be just pop(), push(), and anything you need for those methods or testing. (b) Write a queue class that is based on a linked list. As above, it can be just enqueue() and dequeue(), as well as anything you need for those methods or testing. (c) Write some test cases, trying to include edge cases. Why did you choose those tests? Did you get the results...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT