Jump to content
  • Advertisement
Sign in to follow this  

Linked Lists in Java.. help!

This topic is 3277 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, so I am suppose to create two linked lists of int and then write a method to find the common numbers in the two lists. My problem is comparing two objects of the list.. how do I do that?!? I might have to override the equals function but how do I do that for int? Here's my code so far.. I am trying to get if (l1.get(2)==l2.get(2)) to work!
package structures;

public class LinkedList{
    private Node head;
    private Node tail;
    private int size;
    
    /** Creates a new instance of LinkedList */
    public LinkedList()
    {
        head = null;
        tail = null;
        size = 0;
    }
    
    public void add(Object o)
    {
        size++;
        if(head==null)
        {
            head = new Node(o);
            return;
        }
        Node last = new Node(o);
        if(tail==null)
            head.next = last;
        else
            tail.next = last;
        tail = last;
    }

    public Object get(int i)
    {
        assert(i>=0 && i<size):"Wrong parameters.";
        Node n = head;
        while(i!=0)
        {
            n=n.next;
            i--;
        }
        return n;
    }
    public String toString()
    {
        String s = "";
        Node n = head;
        while(n!=null)
        {
            s = s + n.toString();
            n = n.next;
        }
        return s;
    }

    private class Node
    {
        public Object data;
        public Node next;
        public Node(Object o)
        {
            data = o;
            next=null;
        }

        public String toString()
        {
            return data.toString();
        }

    }
    
}
public class Main
{
    public static void main(String[] args)
    {
        // TODO code application logic here
        LinkedList l1 = new LinkedList();
        LinkedList l2 = new LinkedList();
        l1.add(new Integer (0));
        l1.add(new Integer (7));
        l1.add(new Integer (3));
        l1.add(new Integer (2));
        l1.add(new Integer (9));

        l2.add(new Integer (1));
        l2.add(new Integer (2));
        l2.add(new Integer (3));
        l2.add(new Integer (4));

        System.out.println(l1);
        System.out.println(l2);

        if (l1.get(2)==l2.get(2))
        {
            System.out.println ("Works!");
        }
    }    
}
Thank you!!!

Share this post


Link to post
Share on other sites
Advertisement
I may be wrong, but shouldn't you be doing

return n.data;



instead of

return n;



in the get method? I think right now what you are comparing are the nodes, instead of the data inside the nodes...

Share this post


Link to post
Share on other sites
Quote:
Original post by musafir2007My problem is comparing two objects of the list.. how do I do that?!? I might have to override the equals function but how do I do that for int?
Here's my code so far.. I am trying to get if (l1.get(2)==l2.get(2)) to work!

*** Source Snippet Removed ***

*** Source Snippet Removed ***

Thank you!!!


Since the data in the Node class is stored as an Object type, just call l1.data.equals(l2.data). Java primitive types will cast to their object types (int -> Integer) when you assign them to an object type. <-- Sorry, that won't work due to having to cast the returned Node object.

As a side note, you might think about returning n.data from your get method (and modifying the code surrounding the get calls). Most LinkedList implementations will return the data associated with a node; they don't return a reference to the node itself which allows the user of your LinkedList to start mucking around with the internal node pointers defeating the purpose of Encapsulation

[edit]Actually, yeah, you need to do the suggestion above anyway. After re-looking at your code I realized what problem you're having.

You're returning a Node type from the get method, which means you're trying to compare one node type to another. You either need to add an equals() method to your node class (not recommended) or return the actual data in the node from your get method (recommended).

Currently, to compare the data returned from the get() method you would need cast the object returned from get() to a Node type (which requires your Node type to be public) and then access the data on the node type like this:


// The Node class also must be public... Again, NOT RECOMMENDED! Just change get() to return the data itself.
if (((LinkedList.Node)l1.get(2)).data.equals(((LinkedList.Node)l2.get(2)).data))





[Edited by - CaspianB on June 1, 2009 8:31:23 AM]

Share this post


Link to post
Share on other sites
Is there any particular reason you are rolling your own list, as opposed to using a java.util.LinkedList<Integer>? The only reason I can think of is homework, and we aren't supposed to help much with that [wink]

Share this post


Link to post
Share on other sites
Yeah, I'd assume it is homework. However, pointing out errors in displayed code is a far cry from showing working code when all they've done is copy/paste the homework question at hand. One is acceptable, the other is not. :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!