Question

In: Computer Science

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 example, if a List ADT object is created and has the following values inserted into it:

1, 1, 3, 4, 5

hasRepeats() would return true because the value 1 occurs more than once in the list

but if a List ADT object is created and has the following values inserted into it:

1, 2, 3, 4, 5

hasRepeats() would return false because no value in the list occurs more than once in the list.

The List ADT has methods that may be useful in writing hasRepeats(). You may call any existing methods of the List ADT inside hasRepeats().

Once you have modified the List ADT to have the hasRepeats() method, write a Java Client program that makes a List ADT object, takes integers as input from a user, stores the integers in the List ADT object, and uses the hasRepeats() method to display whether the List ADT object contains repeated values.

The Client program will have a main() method and is the program that is run at the command line. You may give your Client program any name you like. The Client program should perform a loop, ask for input, and display output in the following way.

Input a list of integers: 1 1 3 4 5
The list has repeated values.
Do you want to continue (y/n):  y
  
Input a list of integers: 1 2 10 20 100 200
The list does not have repeated values.
Do you want to continue (y/n):  y
 
Input a list of integers:
The list does not have repeated values.
Do you want to continue (y/n):  y

Input a list of integers: 2
The list does not have repeated values.
Do you want to continue (y/n):  

public interface ListInterface
{
   public void add(T newEntry);
   public void add(int newPosition, T newEntry);
   public T remove(int givenPosition);
   public void clear();
   public T replace(int givenPosition, T newEntry);
   public T getEntry(int givenPosition);
   public T[] toArray();
   public boolean contains(T anEntry);
   public int getLength();
   public boolean isEmpty();
}

public class LList implements ListInterface
{
   private Node firstNode; // reference to the first node of chain
   private int numberOfEntries;
   public LList ()
   {
       initializeDataFields();
   }// end default constructor
  
   public void clear()
   {
       initializeDataFields();
   }//end clear
  
   public void add(T newEntry) // outOfMemoryError possible
   {
       Node newNode = new Node(newEntry);
       if (isEmpty())
           firstNode = newNode;
       else                    // add to end of nonempty link list
       {
           Node lastNode = getNodeAt(numberOfEntries);
           lastNode.setNextNode(newNode);   // Make last node reference new node
          
       }// end if
       numberOfEntries++;
   }// end add
  
   public void add(int givenPosition, T newEntry) // outOfMemoryError possible
   {
       if ((givenPosition >= 1) && (givenPosition <= numberOfEntries +1))
       {
           Node newNode = new Node(newEntry);
          
           if(givenPosition == 1)       // case 1
           {
               newNode.setNextNode(firstNode);
               firstNode = newNode;
           }
           else                        // case 2
           {                           // and givenPosition > 1
               Node nodeBefore = getNodeAt(givenPosition -1);
               Node nodeAfter = nodeBefore.getNextNode();
               newNode.setNextNode(nodeAfter);
               nodeBefore.setNextNode(newNode);
           }// end if
           numberOfEntries ++;
       }
       else
           throw new IndexOutOfBoundsException("illegal position given to add operation");
   }// end add
  
   public T remove(int givenPosition)
   {
       T result = null;                   //return value
       if ((givenPosition >= 1) && (givenPosition <= numberOfEntries))
       {   // assertion: !isEmpty
           if(givenPosition == 1)
           {
               result = firstNode.getData();
               firstNode = firstNode.getNextNode();
           }
           else
           {
               Node nodeBefore = getNodeAt(givenPosition -1);
               Node nodeToRemove = nodeBefore.getNextNode();
               result = nodeToRemove.getData();
               Node nodeAfter = nodeToRemove.getNextNode();
               nodeBefore.setNextNode(nodeAfter);
           }
           numberOfEntries--;
           return result;
       }
       else
           throw new IndexOutOfBoundsException("illegal position given to remove operation.");
   }
  
   public T replace(int givenPosition, T newEntry)
   {
       if ((givenPosition >= 1) && (givenPosition <= numberOfEntries)) {
           Node desiredNode = getNodeAt(givenPosition);
           T originalEntry = desiredNode.getData();
           desiredNode.setData(newEntry);
           return originalEntry;
       }
       else
           throw new IndexOutOfBoundsException("illegal position given to replace operation.");
   }
   public T getEntry(int givenPosition)
   {
       if ((givenPosition >= 1) && (givenPosition <= numberOfEntries)) {
           return getNodeAt(givenPosition).getData();
       }
       else
           throw new IndexOutOfBoundsException("illegal position given to getEntry operation.");
   }
   public T[] toArray()
   {
       T[] result = (T[])new Object[numberOfEntries];
       int index = 0;
       Node currentNode = firstNode;
       while ((index < numberOfEntries) && (currentNode != null)) {
           result[index] = currentNode.getData();
           currentNode = currentNode.getNextNode();
           index ++;
       }
       return result;
   }
  
  
  
  
   public boolean contain(T anEntry)
   {
       boolean found = false;
       Node currentNode = firstNode;
       while (!found && (currentNode != null)) {
           if (anEntry.equals(currentNode.getData()))
               found = true;
           else
               currentNode = currentNode.getNextNode();
       }
       return found;
   }
   public int getLength() {
       return numberOfEntries;
      
   }
   public boolean isEmpty()
   {
       boolean result;
       if (numberOfEntries == 0) // Or getLength() == 0
       {
           // Assertion: firstNode == null
           result = true;
       }
       else
       {
           // Assertion: firstNode != null
           result = false;
       }
       return result;
   }
   private void initializeDataFields()
   {
       firstNode = null;
       numberOfEntries = 0;
   }
   private Node getNodeAt(int givenPosition)
   {
       Node currentNode = firstNode;
       for (int counter =1; counter < givenPosition; counter++)
           currentNode = currentNode.getNextNode();
       return currentNode;
   }
   private class Node
   {
       private T data;
       private Node next;
      
       private Node(T dataPortion) {
           data = dataPortion;
           next = null;
       }
      
       private Node(T dataPortion, Node nextNode) {
           data = dataPortion;
           next = nextNode;
       }
       private T getData() {
           return data;
       }
       private void setData(T newData) {
           data = newData;
       }
       private Node getNextNode()
       {
           return next;
       }
       private void setNextNode(Node nextNode) {
           next = nextNode;
       }
   }
  
  
}

Solutions

Expert Solution

import java.util.*;
public interface ListInterface
{
public void add(T newEntry);
public void add(int newPosition, T newEntry);
public T remove(int givenPosition);
public void clear();
public T replace(int givenPosition, T newEntry);
public T getEntry(int givenPosition);
public T[] toArray();
public boolean contains(T anEntry);
public int getLength();
public boolean isEmpty();
public boolean hasRepeats();
}

public class LList implements ListInterface
{
private Node firstNode; // reference to the first node of chain
private int numberOfEntries;
public LList ()
{
initializeDataFields();
}// end default constructor
  
public void clear()
{
initializeDataFields();
}//end clear
  
public static void main(String[] args){
  
   char c='y';
   while(c=='y'){
       ListInterface<Integer> l=new LList<Integer>();
       Scanner sc=new Scanner(System.in);
       while(sc.hasNextInt()){
           int t=sc.next();
           l.add(t);
       }
       if(l.hasRepeats()){
           System.out.println("The list has repeated values.");
       }
       else{
           System.out.println("The list does not have repeated values.");
       }
       System.out.println("Do you want to continue (y/n): ");
       c=sc.next().charAt(0);
   }
}
  
public void add(T newEntry) // outOfMemoryError possible
{
Node newNode = new Node(newEntry);
if (isEmpty())
firstNode = newNode;
else // add to end of nonempty link list
{
Node lastNode = getNodeAt(numberOfEntries);
lastNode.setNextNode(newNode); // Make last node reference new node
  
}// end if
numberOfEntries++;
}// end add
  
public void add(int givenPosition, T newEntry) // outOfMemoryError possible
{
if ((givenPosition >= 1) && (givenPosition <= numberOfEntries +1))
{
Node newNode = new Node(newEntry);
  
if(givenPosition == 1) // case 1
{
newNode.setNextNode(firstNode);
firstNode = newNode;
}
else // case 2
{ // and givenPosition > 1
Node nodeBefore = getNodeAt(givenPosition -1);
Node nodeAfter = nodeBefore.getNextNode();
newNode.setNextNode(nodeAfter);
nodeBefore.setNextNode(newNode);
}// end if
numberOfEntries ++;
}
else
throw new IndexOutOfBoundsException("illegal position given to add operation");
}// end add
  
public T remove(int givenPosition)
{
T result = null; //return value
if ((givenPosition >= 1) && (givenPosition <= numberOfEntries))
{ // assertion: !isEmpty
if(givenPosition == 1)
{
result = firstNode.getData();
firstNode = firstNode.getNextNode();
}
else
{
Node nodeBefore = getNodeAt(givenPosition -1);
Node nodeToRemove = nodeBefore.getNextNode();
result = nodeToRemove.getData();
Node nodeAfter = nodeToRemove.getNextNode();
nodeBefore.setNextNode(nodeAfter);
}
numberOfEntries--;
return result;
}
else
throw new IndexOutOfBoundsException("illegal position given to remove operation.");
}
  
public T replace(int givenPosition, T newEntry)
{
if ((givenPosition >= 1) && (givenPosition <= numberOfEntries)) {
Node desiredNode = getNodeAt(givenPosition);
T originalEntry = desiredNode.getData();
desiredNode.setData(newEntry);
return originalEntry;
}
else
throw new IndexOutOfBoundsException("illegal position given to replace operation.");
}
public T getEntry(int givenPosition)
{
if ((givenPosition >= 1) && (givenPosition <= numberOfEntries)) {
return getNodeAt(givenPosition).getData();
}
else
throw new IndexOutOfBoundsException("illegal position given to getEntry operation.");
}
public T[] toArray()
{
T[] result = (T[])new Object[numberOfEntries];
int index = 0;
Node currentNode = firstNode;
while ((index < numberOfEntries) && (currentNode != null)) {
result[index] = currentNode.getData();
currentNode = currentNode.getNextNode();
index ++;
}
return result;
}
public boolean contain(T anEntry)
{
boolean found = false;
Node currentNode = firstNode;
while (!found && (currentNode != null)) {
if (anEntry.equals(currentNode.getData()))
found = true;
else
currentNode = currentNode.getNextNode();
}
return found;
}
public int getLength() {
return numberOfEntries;
  
}
public boolean isEmpty()
{
boolean result;
if (numberOfEntries == 0) // Or getLength() == 0
{
// Assertion: firstNode == null
result = true;
}
else
{
// Assertion: firstNode != null
result = false;
}
return result;
}
private void initializeDataFields()
{
firstNode = null;
numberOfEntries = 0;
}
private Node getNodeAt(int givenPosition)
{
Node currentNode = firstNode;
for (int counter =1; counter < givenPosition; counter++)
currentNode = currentNode.getNextNode();
return currentNode;
}
public boolean hasRepeats(){
   HashSet<T> set=new HashSet<T>();
   public Node temp=firstNode;
   while(temp!=null){
       if(set.contains(temp.data)){
           return true;
       }
       else{
           set.add(temp.data);
       }
       temp=temp.next;
   }
   return false;
}
private class Node
{
private T data;
private Node next;
  
private Node(T dataPortion) {
data = dataPortion;
next = null;
}
  
private Node(T dataPortion, Node nextNode) {
data = dataPortion;
next = nextNode;
}
private T getData() {
return data;
}
private void setData(T newData) {
data = newData;
}
private Node getNextNode()
{
return next;
}
private void setNextNode(Node nextNode) {
next = nextNode;
}
}
  
  
}


Related Solutions

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...
******IN JAVA******** I need the following interface implemented accordingly. It is a linked list. The interface...
******IN JAVA******** I need the following interface implemented accordingly. It is a linked list. The interface can be found below: List.java public interface List<T> extends Iterable<T> { /** * Insert an element at a specified location. * @param index * @param obj * @throws IndexOutOfBoundsException */ public void add(int index, T obj); /** * Append an object to the end of the list. * @param obj */ public boolean add(T obj); public void clear(); public boolean contains(T obj); /** *...
Overview: implement the ADT List in Java. This program is meant to the ADT List from...
Overview: implement the ADT List in Java. This program is meant to the ADT List from the ground up In the lecture, we learned how to implement an ADT like the ArrayList you have used in Project 1. With this project, you have the chance to implement an ADT called MyList, which is a simplified replacement for the full-blown ArrayList. Requirements You will implement the MyList ADT according to the following: 1. MyList must implement the List interface. It will...
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 C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
Add a copy constructor for the linked list implementation below. Upload list.cpp with your code added....
Add a copy constructor for the linked list implementation below. Upload list.cpp with your code added. (DO NOT MODIFY THE HEADER FILE OR TEST FILE. only modify the list.cpp) /*LIST.CPP : */ #include "list.h" using namespace std; // Node class implemenation template <typename T> Node<T>::Node(T element) { // Constructor    data = element;    previous = nullptr;    next = nullptr; } // List implementation template <typename T> List<T>::List() {    head = nullptr;    tail = nullptr; } template...
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...
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...
Java OVERVIEW This program primarily focuses on the implementation of a ArrayList type interface and the...
Java OVERVIEW This program primarily focuses on the implementation of a ArrayList type interface and the necessary methods to implement the ArrayList. It also includes polymorphism and class comparison. INSTRUCTIONS Your deliverable will be to turn in three files. The files will be named Card.java, PremiumCard.java and the last file will be called CardArrayList.java. For this assignment, any use of a data control structure other than a simple Arrary or String will result in no credit. I am aware that...
Please write in JAVA 1. Given the following array-based ADT list called colorList whose elements contain...
Please write in JAVA 1. Given the following array-based ADT list called colorList whose elements contain strings             red, orange, yellow, blue, indigo, violet write the statement to insert the String element “pink” to the end of the list. Assume the front of the list is on the left. 2. Outline the basic steps to remove a node from the beginning of a list. Completed answers will be given an immediate upvote :)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT