views:

25484

answers:

17

What's the best way to make a linked list in Java?

+30  A: 

Java has a LinkedList implementation, that you might wanna check out. You can download the JDK and it's sources at java.sun.com.

david
Does Java's Linkedlist doesn't allow you insert and remove elements at arbitrary positions?
Seun Osewa
Isn't that the whole point of a linked list?
jrockway
+2  A: 

That question is timely and hilarious. Thanks!

Chris Farmer
A: 

You should check your textbook and class notes.

Matt Price
+10  A: 

I hope you don't get too many down votes for making a joke. I love the homework tag, nice touch.

Kevin Sheffield
Is the link valid?
chedine
+2  A: 

I lol'd.

Will
+1  A: 

Oh I see, you were just pretending to be a 29 year old developer/users group founder in all those other questions, this is the real you. . . .right?

Tarks
A: 

Vote down requires 100 reputation. :\

bpapa
+9  A: 

Use java.util.LinkedList. Like this:

list = new java.util.LinkedList()
Juha Syrjälä
+1  A: 

You can just read the source code of how Java implements it, comes with every JDK.

Akira
A: 

Implement it in C.

Rob Lachlan
+2  A: 

I second the statement about using java's LinkedList class, but you could also man up and code your own from scratch.

icco
+24  A: 

The obvious solution to developers familiar to Java is to use the LinkedList class already provided in java.util. Say, however, you wanted to make your own implementation for some reason. Here is a quick example of a linked list that inserts a new link at the beginning of the list, deletes from the beginning of the list and loops through the list to print the links contained in it. Enhancements to this implementation include making it a double-linked list, adding methods to insert and delete from the middle or end, and by adding get and sort methods as well.

Note: In the example, the Link object doesn't actually contain another Link object - nextLink is actually only a reference to another link.

class Link {
    public int data1;
    public double data2;
    public Link nextLink;

    //Link constructor
    public Link(int d1, double d2) {
     data1 = d1;
     data2 = d2;
    }

    //Print Link data
    public void printLink() {
     System.out.print("{" + data1 + ", " + data2 + "} ");
    }
}

class LinkList {
    private Link first;

    //LinkList constructor
    public LinkList() {
     first = null;
    }

    //Returns true if list is empty
    public boolean isEmpty() {
     return first == null;
    }

    //Inserts a new Link at the first of the list
    public void insert(int d1, double d2) {
     Link link = new Link(d1, d2);
     link.nextLink = first;
     first = link;
    }

    //Deletes the link at the first of the list
    public Link delete() {
     Link temp = first;
     first = first.nextLink;
     return temp;
    }

    //Prints list data
    public void printList() {
     Link currentLink = first;
     System.out.print("List: ");
     while(currentLink != null) {
      currentLink.printLink();
      currentLink = currentLink.nextLink;
     }
     System.out.println("");
    }
}  

class LinkListTest {
    public static void main(String[] args) {
     LinkList list = new LinkList();

     list.insert(1, 1.01);
     list.insert(2, 2.02);
     list.insert(3, 3.03);
     list.insert(4, 4.04);
     list.insert(5, 5.05);

     list.printList();

     while(!list.isEmpty()) {
      Link deletedLink = list.delete();
      System.out.print("deleted: ");
      deletedLink.printLink();
      System.out.println("");
     }
     list.printList();
    }
}
Kamikaze Mercenary
you could also quite easily improve this code to use generics for the data type rather than storing an int and a double.
shsteimer
@shsteimer: quite definitely, but since pretty much the only good use of this code is to demonstrate the technique, it wouldn't help anyone. It would only diffuse the basic idea.
Joachim Sauer
It isn't good OO approach to have `public Link nextLink` and operating on it outside the class. It could be respectable when `Link` would be an internal class of `LinkList`. It is another bunch of code written as Java was only another-version-of-c.
Bart
+2  A: 

Its much better to use java.util.LinkedList, because it's probably much more optimized, than the one that you will write.

Darth
And it will work first time.
Peter Lawrey
A: 

this linked list creation is right that program create a linked list with out using linkedclass. by dev dixit

A: 

ur code is not thread safe..

jagan
A: 

The above linked list display in opposite direction. I think the correct implementation of insert method should be

public void insert(int d1, double d2) { 
    Link link = new Link(d1, d2); 

    if(first==null){
        link.nextLink = null;
        first = link; 
        last=link;
    }
    else{
        last.nextLink=link;
        link.nextLink=null;
        last=link;
    }
} 
arnab sarkar
A: 

The delete function has a memory leak! The variable first should be set to NULL!