LinkedList class :
- The underlying data structure is Double Linked List.
- Insertion order is preserved.
- Duplicates are allowed.
- Heterogeneous objects are allowed.
- Null insertion is possible.
- Linked List implements Serializable and cloneable interfaces but not Random Access interfaces.
- Linked List is the best choice if our frequent operation is insertion or deletion in the middle.
- Linked List is the worst choice if our frequent operation is retrieval operation.
- Usually, we can use a linked list to implement stacks and queues to provide support for this requirement Linked List class defines the following specific methods.
Methods:
- void addFirst();
- void addLast();
- Object getFirst();
- Object getLast();
- Object removeFirst();
- Object removeLast();
CONSTRUCTOR:
Creates an empty Linked List object.
LinkedList l1 = new LinkedList();
Creates an equivalent Linked List object for a given collection.
LinkedList l1 = new LinkedList(Collection c);
Example of LinkedList :
import java.util.LinkedList; public class LinkedListDemo{ public static void main(String arg[]){ LinkedList l1 = new LinkedList(); l1.add("Preeti"); l1.add(30); l1.add(null); l1.add("Preeti"); l1.set(0,"Software"); l1.add(0,"venkey"); l1.addFirst("ccc"); System.out.println(l1); } }
Output :
[ccc, venkey, Software, 30, null, Preeti]