So i've made a Node class :

public class IntNode 
{  
    private int _value;  
    private IntNode _next; 

    public IntNode(int v, IntNode n)  
    {
        _value = v;   _next = n;  
    }

    public int getValue() 
    {   
        return _value;  
    }

    public IntNode getNext() 
    {  
        return _next;  
    }

    public void setValue(int v) 
    {   
        _value = v;  
    }

    public void setNext(IntNode n) 
    {   
        _next = n;  
    } 
} 

and a class with some functions, from them are the isMember (true if num is a member of the list), and a subset which supposed to return true if "other" is a sub of the object :

public boolean isMember (int num)
    {
        if (_head !=null)
        {
            if (_head.getValue()==num)
                return true;
            else{
                IntNode member = _head;
                while (member.getNext() !=null){
                    if (member.getNext().getValue()==num) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    public boolean subSet (Set other)
    {
        IntNode number = _head;
        for (int i=0; i<other.numOfElements(); i++)
        {
            if (isMember(number.getValue())==false)
                return false;
            number.getNext();
        }
        return true;
    }

and i run my simple tester to see if sub works correctly but when it reaches num 9, it get stuck in endless loop. why?

/*# subSet */

        s1 = new Set();
        s1.addToSet(1); s1.addToSet(3); s1.addToSet(9); s1.addToSet(17);
        s2 = new Set();
        s2.addToSet(1);

        System.out.println(s1.subSet(s2));

subSet method is way off - contains code that is not just unneccesary but actually prevents it working - it's really simple given what you already have done, but you have made it too complicated.

s2 is a subset of s1 if

for each element in s2  
    the element is a member of s1 

or, fleshing that out a bit..

    for each element in s2  // use an enhanced for loop syntax
       if (! the element is a member of s1)  // you have a method to do this
         return false
    return true

although i did it i still get the endless loop....it gets stuck at isMember method idk why...

oh just found this issue out just now... if i add more than 2 numbers in the list it gets stuck in loop too..
here's my adding method's code although im sure that this isn't the problem here...

public void addToSet (int x)
    {
        if (x>0&&(x==1||x%2!=0)&&!isMember(x))
        {
            _head = addToSet(_head, x);
        }
    }

    private IntNode addToSet(IntNode node, int x)
    {
        if (node == null) 
        {
            return new IntNode(x, null);
        }
        else {
            node.setNext(addToSet(node.getNext(), x));
            return node;
        }
    }

still it seems so weird to me idk where to look at...

OHHHH SORRY i just noticed and found the issue lol
i forgot to do the member = memeber.getNext() in the while loop of isMember so it won't get stuck in the same node haha stupid me
sorry hehe thank you !

Without any kind of spec etc it's hard to understand what that code is trying to do, but it looks to me like the private method is going into an infinite recursion (?). If you are adding new members at the head then there's there's no recursion involved.
To me it looks like you are trying to write too much code too soon.

There are two things you can do to fix your development process..

  1. Write pseudo code (like my previous post) and test it in pen-and-paper simulation to get the logic right before writing any Java
  2. Test early, test often. Eg Don't attempt to test is-a-subset methods before confirming that you are creating sets/lists properly in the first case. Your first three pieces of code should be:
    define the node class's members/constructor
    a print method that prints a list of nodes
    a method to add nodes to a list
    ... that's the minimum that you can test, but test and fix that before trying to build more code on top. Then go one step at a time, eg don't try to develop a method that uses isMember until you have tested isMember
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.