• Post Reply Bookmark Topic Watch Topic
  • New Topic

Implementing a Set using Linked List  RSS feed

 
karina val
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to implement the functions of a set (union, complement, etc) using a linked list. I had to implement a class called mySet to provide all the functions. I've already created the sets and added to them, but I'm not sure how to join them.

In main I have :




And in mySet class I just create a new linked list for each set which works fine when I print the sets :



The union method was just an idea since it doesn't work, I just don't know how to join set A and B if they're of type mySet not linked list...?


 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you mean the mathematical Set here. Is this an assignment to use LinkedList instead of the Set? Instead of using addAll() you should first check if the List contains the elements already being added.

Note that you create another instance in the union() method and the one created in create() method is not referenced anymore.

The union method was just an idea since it doesn't work

Please read ItDoesntWorkIsUseless
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
karina val wrote:I have to implement the functions of a set (union, complement, etc) using a linked list.

Why? What's wrong with plain old LinkedHashSet? That's already a Set that maintains insertion order.

...

That stuff shouldn't go in a method (create) that you may forget to call, leading to unexpected NullPointerExceptions. You should do this in a constructor instead:
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What a strange requirement. Searching a linked list runs in linear time, so union intersection and difference of two sets will run in quadratic time. You would want something like a.union(b), not c.union(a, b).
I think you are trying to do too much all at once. You should get one method at a time written and working. You should also start with isEmpty() and contains() methods

You should also find the correct conventions about capital letters and names of variables.
 
dennis deems
Ranch Hand
Posts: 808
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:
karina val wrote:I have to implement the functions of a set (union, complement, etc) using a linked list.

Why? What's wrong with plain old LinkedHashSet? That's already a Set that maintains insertion order.

Apart from the fact that union, complement and intersection functions are already implemented, leaving nothing for the student to do?
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You would want something like a.union(b), not c.union(a, b).

Agreed. The more closely the method signatures resemble what you would do with paper and pencil, the easier it will be to work with MySet.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!