Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Link List Example in Java Sample program in Java

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 04-08-2009
Member
 
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"
Reply With Quote
  #2  
Old 04-08-2009
Member
 
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
Reply With Quote
  #3  
Old 04-08-2009
Member
 
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]
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 09:41 PM.