views:

184

answers:

2

Possible Duplicate:
How do I implement a Linked List in Java?

hi all,

I need to create linked list in java, but not from the built-in class, i want to create from the beginning. i have created a class and when i try to manually add elements, it worked, but how to implement add(element) method so i can add element to that linked list.

thanks.

A: 

Well, if you want it as part of the linked list class, you can do:

public class LinkedList
{
    Node tail;

    public add(Object j)
    {
        Node node = new Node(j);
        node.previous = tail;
        node.next = null;
    }
}

Something along those lines. You can also consider a constructor for Node that does this as well.

CookieOfFortune
Thanks, also any help for how can i iterate through this linkedlist?
srisar
@srisar - you use the `node.next` reference.
Stephen C
can u some body post me a working model of this linked list class, that would be very helpful
srisar
... I'm pretty sure linked list implementations like mine are freely available if you spent 5 minutes on Google.
CookieOfFortune
A: 

Use Google Code Search and wander through the hits, I'm sure you'll see what you want.

http://www.google.com/codesearch?hl=en&sa=N&filter=0&q=LinkedList++lang:java&ct=rr&cs_r=lang:java

Dhananjay Ragade