Programming Tutorials

LinkedList Sample program in Java

By: Jagan in Java Tutorials on 2007-09-12  

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:

// 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);
    }
}

The output from this program is shown here:

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]

Because LinkedList implements the List interface, calls to add(Object) append items to the end of the list, as does addLast(). To insert items at a specific location, use the add(int, Object) form of add(), as illustrated by the call to add(1, "A2") in the example. Notice how the third element in ll is changed by employing calls to get() and set(). To obtain the current value of an element, pass get() the index at which the element is stored. To assign a new value to that index, pass set() the index and its new value.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Java )

Latest Articles (in Java)