Results 1 to 3 of 3

Thread: Link List Example in Java Sample program in Java

  1. #1
    Join Date
    Apr 2008
    Posts
    29

    Link List Example in Java Sample program in Java

    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"

  2. #2
    Join Date
    May 2008
    Posts
    44

    Re: Link List Example in Java Sample program in Java

    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:

    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());
      }
    }
    Output:

    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

  3. #3
    Join Date
    May 2008
    Posts
    22

    Re: Link List Example in Java Sample program in Java

    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:

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

Similar Threads

  1. Looking for Excel Web Query for Java Applet working sample
    By Omswaroop in forum MS Office Support
    Replies: 3
    Last Post: 17-01-2014, 09:04 AM
  2. Dr. Java program
    By bamggut29 in forum Software Development
    Replies: 2
    Last Post: 26-11-2011, 03:24 AM
  3. Don't know how to run the java bean program
    By Reema_n in forum Software Development
    Replies: 5
    Last Post: 23-12-2009, 10:01 AM
  4. GUI image program java
    By Caden Fernandes in forum Software Development
    Replies: 3
    Last Post: 12-11-2009, 10:48 AM
  5. How do i excute java program from asp.net
    By softte in forum Software Development
    Replies: 2
    Last Post: 09-05-2009, 02:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •