Question

Create a linked list with given code Given code: import java.util.Iterator; public interface Set { /**...

Create a linked list with given code

Given code:

import java.util.Iterator;

public interface Set {
   
   /**
      Removes all of the elements from this set
   */
   void clear();
   
   /**
      Returns true if this set contains no elements
      @return true if this set contains no elements
   */
   boolean isEmpty();
   
   /**
      Returns the number of elements in this set (its cardinality)
      @return the number of elements in this set
   */
   int size();
   
   /**
      Returns an iterator over the elements in this set
      @return an iterator over the elements in this set
   */
   Iterator iterator();

   /**
      Returns true if this set contains the specified element
      @param e element whose presence in this set is to be tested
      @return true if this set contains the specified element
   */
   boolean contains (E e);
   
   /**
      Adds the specified element to this set if it is not already present
      @param e element to be added to this set
      @return true if this set did not already contain the specified element
   */
   boolean add (E e);
   
   /**
      Removes the specified element from this set if it is present
      @param e element to be removed from this set, if present
      @return true if this set contained the specified element
   */
   boolean remove (E e);
}
   
Task #1. Develop the class LinkedSet 
Assuming you already have the file Set.java saved in your working directory, open it and look for all the methods you need to implement the Set interface. Develop your LinkedSet class in a file named LinkedSet.java with the prototype 

public class LinkedSet implements Set

Your class should consist of the following members. Decide upon the access specifier to use for each of these members, including the nested classes. Use the linked list class examples as guidelines, but the recommendation is that you develop the iterator class first and implement the set interface methods using the iterator methods

A nested node class for storing elements of type E and reference to the next node
  o Data fields (note that the outer class has direct access to inner class data) 
  o Method, including constructors

A nested iterator class implementing the iterator interface
  private class LinkedSetIterator implements Iterator
  o Carefully choose the data fields. How many reference variables are needed? 
  o Decide upon whether to make constructors public—this depends on how you plan on instantiating an iterator. 
  o Methods enforced by the iterator interface 
     hasNext to check if there is any more element 
     next to return the next element 
     remove to remove the element returned by last call to next

Data fields needed for the whole set object
Methods enforced by the set interface (as per downloaded Set.java)
  o clear to remove all elements from the set 
  o isEmpty to check if the set has no elements o size to find the number of elements in the set 
  o iterator to get a new instance of set iterator 
  o contains to check if a given element is in the set 
  o add to check if a given element is not in the set and subsequently added (new elements are always added to the front of the underlying linked list) 
  o remove to check if a given element is in the set and subsequently removed
  o toString to print the whole set (element by element printing can be easily done using the iterator, which we already have).


No need to do every bullet point completely, I just need to know mainly how to start the code and how to incorporate set.java into it.
  
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Given below is the code for the question. Please do rate the answer if it helped. Thank you.


Set.java
---
import java.util.Iterator;

public interface Set<E extends Comparable<E>> {

/**
Removes all of the elements from this set
*/
void clear();

/**
Returns true if this set contains no elements
@return true if this set contains no elements
*/
boolean isEmpty();

/**
Returns the number of elements in this set (its cardinality)
@return the number of elements in this set
*/
int size();

/**
Returns an iterator over the elements in this set
@return an iterator over the elements in this set
*/
Iterator iterator();

/**
Returns true if this set contains the specified element
@param e element whose presence in this set is to be tested
@return true if this set contains the specified element
*/
boolean contains (E e);

/**
Adds the specified element to this set if it is not already present
@param e element to be added to this set
@return true if this set did not already contain the specified element
*/
boolean add (E e);

/**
Removes the specified element from this set if it is present
@param e element to be removed from this set, if present
@return true if this set contained the specified element
*/
boolean remove (E e);
}

LinkedSet.java
-----
import java.util.Iterator;


public class LinkedSet<E extends Comparable<E>> implements Set<E>
{
   class Node{
       E data;
       Node next;
       Node(E val, Node nxt){
           data = val;
           next = nxt;
       }
   }

   private Node head;


   /**
       Removes all of the elements from this set
   */
   public void clear(){
       head = null;
   }

   /**
       Returns true if this set contains no elements
       @return true if this set contains no elements
   */
   public boolean isEmpty(){
       return head == null;
   }

   /**
       Returns the number of elements in this set (its cardinality)
       @return the number of elements in this set
   */
   public int size(){
       int count = 0;
       for(Node n = head; n != null; n = n.next)
           count++;
       return count;
   }

   /**
       Returns an iterator over the elements in this set
       @return an iterator over the elements in this set
   */
   public Iterator iterator(){
       return new LinkedSetIterator();
   }

   /**
       Returns true if this set contains the specified element
       @param e element whose presence in this set is to be tested
       @return true if this set contains the specified element
   */
   public boolean contains (E e){
       for(Node n = head; n != null; n=n.next)
       {
           if(n.data.equals(e))
               return true;
       }
       return false;
   }

   /**
       Adds the specified element to this set if it is not already present
       @param e element to be added to this set
       @return true if this set did not already contain the specified element
   */
   public boolean add (E e){
       if(!contains(e)){
           head = new Node(e, head);
           return true;
       }
       else
           return false;
   }

   /**
       Removes the specified element from this set if it is present
       @param e element to be removed from this set, if present
       @return true if this set contained the specified element
   */
   public boolean remove (E e){
       Node prev = null, curr = head;
       while(curr != null && !curr.data.equals(e)){
           prev = curr;
           curr = curr.next;
       }
       if(curr == null) //not found
           return false;

       if(curr == head) //remove head node?
           head = head.next;
       else
           prev.next = curr.next;
       return true;
   }

   class LinkedSetIterator implements Iterator<E>{
       Node curr;

       public LinkedSetIterator(){
           curr = head;
       }

       public boolean hasNext(){
           return curr != null;
       }

       public E next(){
           return curr.data;
       }
   }
}

Add a comment
Know the answer?
Add Answer to:
Create a linked list with given code Given code: import java.util.Iterator; public interface Set { /**...
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
  • Methods enforced by the set interface: import java.util.Iterator; public interface Set<E> { /** Removes all of...

    Methods enforced by the set interface: import java.util.Iterator; public interface Set<E> { /** Removes all of the elements from this set */ void clear(); /** Returns true if this set contains no elements @return true if this set contains no elements */ boolean isEmpty(); /** Returns the number of elements in this set (its cardinality) @return the number of elements in this set */ int size(); /** Returns an iterator over the elements in this set @return an iterator over...

  • JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public...

    JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public E previous() { // Returns the previous Element return null; } Explanation: We have this class with these two implemented inferfaces: The interfaces are: package edu.ics211.h04; /** * Interface for a List211. * * @author Cam Moore * @param the generic type of the Lists. */ public interface IList211 { /** * Gets the item at the given index. * @param index the index....

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • Using a doubly linked list as the underlying data structure, implement a list ADT that implements...

    Using a doubly linked list as the underlying data structure, implement a list ADT that implements the ListInterface.java found in the ProgProjTwo Eclipse project starting point for this assignment. In addition to the forward iterator defined by resetIterator( ) and getNextItem( ) in ListInterface.java, implement a backwards iterator by providing resetBackIterator( ) and getPreviousItem( ) methods. As noted in the syllabus addendum, you are encouraged to develop a find( ) helper method that can support various list ADT operations. A...

  • In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {...

    In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {    private BinaryTreeNode root;    /**    * Creates an empty binary tree.    */    public LinkedBinaryTree() {        root = null;    }    /**    * Creates a binary tree from an existing root.    */    public LinkedBinaryTree(BinaryTreeNode root) {        this.root = root;    }    /**    * Creates a binary tree with the specified element...

  • 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...

  • 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...

  • import java.util.*; /** * A class that implements the ADT set by using a linked bag....

    import java.util.*; /** * A class that implements the ADT set by using a linked bag. * The set is never full. * * */ public class LinkedSetWithLinkedBag> implements SetInterface { private LinkedBag setOfEntries; /** * Creates a set from a new, empty linked bag. */ public LinkedSetWithLinkedBag() { //TODO Project1 } // end default constructor public boolean add(T newEntry) { //TODO Project1 // new node is at beginning of chain if(this.setOfEntries.isEmpty()) { if (!this.setOfEntries.contains(newEntry)) this.setOfEntries.add(newEntry); } return true; //...

  • create a class named IntegerQueue given a singlylinkedqueue of integers, write the following methods: max(SinglyLinkedQueue<Integer> s)...

    create a class named IntegerQueue given a singlylinkedqueue of integers, write the following methods: max(SinglyLinkedQueue<Integer> s) to return the max element in the queu. min(SinglyLinkedQueue<Integer> s) to return the min element in the queue. sum(SinglyLInkedQueue<Integer> s) to return the sum of elements in the queu. median(SinglyLinkedQueue<Integer> s) to return the median of elements in the queue. split(SinglyLinkedQueue<Integer> s) to separate the SinglyLinkedQueue into two ArrayQueues based on whether the element values are even or odd. package Stack_and_Queue; import java.util.Iterator; import...

  • Generic Linked Lists ( Program should be written in Java language). Modify the linked list class...

    Generic Linked Lists ( Program should be written in Java language). Modify the linked list class presented in this chapter so it works with generic types. Add the following methods drawn from the java.util.List interface: -void clear(): remove all elements from the list. -E get(int index): return the element at position index in the list. -E set(int index, E element): replace the element at the specified position with the specified element and return the previous element. Test your generic linked...

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