Implement interface with linked lists

Asked

Viewed 76 times

0

Good evening, everyone,

I could use a hand here. I’m starting to learn the implementation of linked lists in java, but I can’t understand or find a solution to this.

I have this interface:

public interface ISet<E> extends Iterable<E> {
/**
* Inserts another element in the set.
* @param x The element to be inserted
* @requires x!=null
*/
public void insert (E x);

/**
* Removes a given element from the set.
* @param x The element to be removed
* @requires x!=null
*/

public void remove (E x);

/**
* Returns an element of the set.
* @param i
* @return
* @requires 0 <= i < size()
*/
public E get(int i);

/**
* Returns true if the element is in the set and false otherwise.
* @param x The element
* @return if the element is in the set
* @requires x!=null
*/
public boolean isIn(E x);

/**
* Returns true if the set is empty and false otherwise.
* @return if the set is empty
*/
public boolean isEmpty();
/**
* Returns the cardinality of the set
* @return the number of elements in the set
*/
public int size();

/**
* Returns true if other is s subset of this and false otherwise.
* @param other The other set
* @return if the set is subset of this
*/
public boolean subSet(ISet<? extends E> other);

/**
* Returns a generator that produces all elements of the set,
* exactly once in arbitrary order
* @return an iterator of the elements in the set
* @requires this not be modified while the generator is used
*/
public Iterator<E> iterator();
}

And I need to build an implementation for this interface using linked lists. Can anyone help me?

I know I need to start like this:

public class Set<E extends Iterable<E>> implements ISet<E>{
    private Node<E> head;
    private int size;

    public Set() {
        head=null;
        size=0;
    }
class Node<E extends Comparable<E>>{
        private E element;
        Node<E> next;

        public Node(E e) {
            element = e;
            next = null;
        }

        @Override
        public String toString() {
            return element.toString();
        }
    }
  • Can’t you use the linked list that already exists in Java to implement this interface? I understand the didactic value of doing "at hand", but if the idea of the exercise is to learn sets, it doesn’t make much sense to make a list to get there.

  • No, it has to be done by hand

  • The class that implements its interface is very strange. It understands the use of Generics in Java?

  • Yes I do understand.

  • Why the extends Comparable<E>?

  • List is a thing and Set is something else. The interface code comes closer to a Set (set without repetitions and possibly without ordering) while the description of your question speaks in "linked lists" (i.e., ordered sequences of elements allowing repetitions). Which one do you want? I’m trying to come up with an answer, but without that information it’s hard.

  • Yes, this exercise seems to me very poorly elaborated... It wants to teach Sets but involves the construction of lists. It doesn’t seem right.

  • @Victorstafusa I want the list linked, but using this interface

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.