Gadgets

Thursday, 21 January 2016

Explain about LinkedList with an example program

1. Heterogeneous Objects are allowed.
2. Insertion order is preserved.
3. Duplicate Objects are allowed.
4. Null insertion is possible.
5. Underlying data structure is DoubleLinkedList.
6. LinkedList class implements Serializable and Cloneable interfaces.
7. Best choice for insertion and deletion operation.
import java.util.*;
public class LinkedListDemo {
               public static void main(String[] arg) {                  
                               LinkedList l=new LinkedList();
                               l.add("n");
                               l.add("a"); //char insertion
                               l.add(12); //int insertion
                               l.add("r");
                               l.add("e");
                               l.add(14);
                               l.add("n"); //duplicate insertion
                               l.add(null); //null insertion
                               System.out.println(l); //[n, a, 12, r, e, 14, n, null]
                               l.remove(2); //remove l[2]
                               System.out.println(l); //[n, a, r, e, 14, n, null]
                               l.add(2,10); //add l[2]=10
                               System.out.println(l); //[n, a, 10, r, e, 14, n, null]
                               l.addFirst("A"); //add element at first
                               l.removeLast(); //remove last element
                               System.out.println(l); //[A, n, a, 10, r, e, 14, n]
               }             
}

No comments:

Post a Comment