Hi,
Can anyone help me with Linked List Implementation in Java with an example.
Thanks,
trickson
Hi,
Can anyone help me with Linked List Implementation in Java with an example.
Thanks,
trickson
"I'd rather be riding my bike thinking about God than sitting in church thinking about my bike"
Link List Example in Java
java.util.LinkedList class
This class extends AbstractSequentialList and implements List, Cloneable, Serializable. It permits all elements including null. LinkedList class provides methods get, insert and remove an element at the beginning and end of the list.
add(Object o): Appends the specified element to the end of this list. It returns a boolean value.
size(): Returns the number of elements in this list.
addFirst(Object o): Inserts the given element at the beginning of this list.
addLast(Object o): Inserts the given element at the last of this list.
add(int index,Object o): Insert the specified element at the specified position in this list. It throws IndexOutOfBoundsException if index is out of range.
remove(int index): Remove the element at the specified position in this list. It returns the element that was removed from the list. It throws IndexOutOfBoundsException if index is out of range.
code:
Output:Code:import java.util.*; public class LinkedListDemo{ public static void main(String[] args){ LinkedList link=new LinkedList(); link.add("a"); link.add("b"); link.add(new Integer(10)); System.out.println("The contents of array is" + link); System.out.println("The size of an linkedlist is" + link.size()); link.addFirst(new Integer(20)); System.out.println("The contents of array is" + link); System.out.println("The size of an linkedlist is" + link.size()); link.addLast("c"); System.out.println("The contents of array is" + link); System.out.println("The size of an linkedlist is" + link.size()); link.add(2,"j"); System.out.println("The contents of array is" + link); System.out.println("The size of an linkedlist is" + link.size()); link.add(1,"t"); System.out.println("The contents of array is" + link); System.out.println("The size of an linkedlist is" + link.size()); link.remove(3); System.out.println("The contents of array is" + link); System.out.println("The size of an linkedlist is" + link.size()); } }
Code:The contents of array is[a, b, 10] The size of an linkedlist is3 The contents of array is[20, a, b, 10] The size of an linkedlist is4 The contents of array is[20, a, b, 10, c] The size of an linkedlist is5 The contents of array is[20, a, j, b, 10, c] The size of an linkedlist is6 The contents of array is[20, t, a, j, b, 10, c] The size of an linkedlist is7 The contents of array is[20, t, a, b, 10, c] The size of an linkedlist is6
LinkedList Sample program in Java
The LinkedList class extends AbstractSequentialList and implements the List interface. It provides a linked-list data structure. It has the two constructors, shown here:
LinkedList( )
LinkedList(Collection c)
The first constructor builds an empty linked list. The second constructor builds a linked list that is initialized with the elements of the collection c.
In addition to the methods that it inherits, the LinkedList class defines some useful methods of its own for manipulating and accessing lists. To add elements to the start of the list, use addFirst( ); to add elements to the end, use addLast( ). Their signatures are shown here:
void addFirst(Object obj)
void addLast(Object obj)
Here, obj is the item being added.
To obtain the first element, call getFirst( ). To retrieve the last element, call getLast( ). Their signatures are shown here:
Object getFirst( )
Object getLast( )
To remove the first element, use removeFirst( ); to remove the last element, call removeLast( ). They are shown here:
Object removeFirst( )
Object removeLast( )
The following program illustrates several of the methods supported by LinkedList:
The output from this program is shown here:Code:// Demonstrate LinkedList. import java.util.*; class LinkedListDemo { public static void main(String args[]) { // create a linked list LinkedList ll = new LinkedList(); // add elements to the linked list ll.add("F"); ll.add("B"); ll.add("D"); ll.add("E"); ll.add("C"); ll.addLast("Z"); ll.addFirst("A"); ll.add(1, "A2"); System.out.println("Original contents of ll: " + ll); // remove elements from the linked list ll.remove("F"); ll.remove(2); System.out.println("Contents of ll after deletion: " + ll); // remove first and last elements ll.removeFirst(); ll.removeLast(); System.out.println("ll after deleting first and last: " + ll); // get and set a value Object val = ll.get(2); ll.set(2, (String) val + " Changed"); System.out.println("ll after change: " + ll); } }
Original contents of ll: [A, A2, F, B, D, E, C, Z]
Contents of ll after deletion: [A, A2, D, E, C, Z]
ll after deleting first and last: [A2, D, E, C]
ll after change: [A2, D, E Changed, C]
Bookmarks