Wednesday, December 27, 2017

LinkedList


LinkedList implements List interface. The underline data structure  is doubly linkedlist.

there are some point which are followed by linked list.

1. Insertion order is preserved.
2. duplicates are allowed.
3. heterogeneous is allowed.
4. Null insertion is possible.
5. LinkedList implements serializable and clonable interface but not randomAccess.
6. LinkedList is best choice if our frequent operation is insertion or deletion in the middle.
7. LinkedList the worst choice if our frequent operation is retrieval.


Constructors:

LinkedList l=new LinkedList();

creates empty linked list object.

LinkedList l1= new LinkedList(Collection c);


LinkedList Class specific  methods:

Usually we can use LinkedList to develop stack and queue to provide support for these requirement below are the methods.

1. void addFirst(Object o)
2. void AddLast(object o)
3. object getFirst()
4. object getLast()
5. object removeFirst()
6. object removeLast()


Example :

package indrajeet.list.exp;

import java.util.LinkedList;

public class LinkedListDemo {

public static void main(String[] args) {

LinkedList l=new LinkedList();
l.add("indar");
l.add(30);  //heterogeneous OBJECT
l.add(null);
l.add("indar"); // duplicate object
l.set(0,"jeet");
System.out.println(l); // ouput : [jeet, 30, null, indar]
l.removeFirst();
System.out.println(l); // output : [30, null, indar]


}

}


No comments: