21 Sept 2012

LinkedList in java with example

The LinkedList class extends AbstractSequentialList. This AbstractSequentialList class extends AbstractList.AbstractList implements the List interface. LinkedList class finally implements the List interface. LinkedList class has the two constructors;
1.LinkedList()
ex: LinkedList ll=new LinkedList();

2.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.

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
public void addFirst(Object o)
ex: ll.addFirst(new Integer(10));

To add elements to the end, use
public void addLast(Object o)
Here, 'o' is the item being added.
ex: ll.addLast(new Integer(10));

To obtain the first element, use
public Object getFirst()
ex: Object o= ll.getFirst();

To obtain the last element, use
public Object getLast()
ex: Object o= ll.getLast();

To remove the first element, use
public Object removeFirst()
ex: Object o= ll.removeFirst();

To remove the last element, use
public Object removeLast()
ex: Object o= ll.removeLast();

To insert items at a specific location, use
public void add(int, Object)
ex: ll.add(1, "A2")

To get items at a specific location, use
public Object get(int)
ex: Object o=ll.get(1);

To remove items at a specific location, use
public Object remove(int)
ex: Object o=ll.remove(1);


import java.util.*;

public class LinkedListExample
  {
  public static void main(String[] args)
  {
  System.out.println("Linked List Example!");
  LinkedList list = new LinkedList();
  int size;
  Iterator iterator;
  //Adding data in the list
  list.add(new Integer(11));
  list.add(new Integer(22));
  list.add(new Integer(33));
  list.add(new Integer(44));
  size = list.size();
  System.out.print( "Linked list data: "); 
  //Create a iterator
  iterator = list.iterator();
  while (iterator.hasNext()){
  System.out.print(iterator.next()+" "); 
  }
  System.out.println();
  //Check list empty or not
  if (list.isEmpty()){
  System.out.println("Linked list is empty");
  }
  else{
  System.out.println( "Linked list size: " + size);
  }
  System.out.println("Adding data at 1st location: 55");
  //Adding first
  list.addFirst(new Integer(55));
  System.out.print("Now the list contain: ");
  iterator = list.iterator();
  while (iterator.hasNext()){
  System.out.print(iterator.next()+" "); 
  }
  System.out.println();
  System.out.println("Now the size of list: " + list.size());
  System.out.println("Adding data at last location: 66");
  //Adding last or append
  list.addLast(new Integer(66));
  System.out.print("Now the list contain: ");
  iterator = list.iterator();
  while (iterator.hasNext()){
  System.out.print(iterator.next()+" "); 
  }
  System.out.println();
  System.out.println("Now the size of list: " + list.size());
  System.out.println("Adding data at 3rd location: 99");
  //Adding data at 3rd position
  list.add(2,new Integer(99));
  System.out.print("Now the list contain: ");
  iterator = list.iterator();
  while (iterator.hasNext()){
  System.out.print(iterator.next()+" "); 
  }
  System.out.println();
  System.out.println("Now the size of list: " + list.size());
  //Retrieve first data
  System.out.println("First data: " + list.getFirst());
  //Retrieve lst data
  System.out.println("Last data: " + list.getLast());
  //Retrieve specific data
  System.out.println("Data at 4th position: " + list.get(3));
  //Remove first
  int first = list.removeFirst();
  System.out.println("Data removed from 1st location: " + first);
  System.out.print("Now the list contain: ");
  iterator = list.iterator();
  //After removing data
  while (iterator.hasNext()){
  System.out.print(iterator.next()+" "); 
  }
  System.out.println();
  System.out.println("Now the size of list: " + list.size());
  //Remove last
  int last = list.removeLast();
  System.out.println("Data removed from last location: " + last);
  System.out.print("Now the list contain: ");
  iterator = list.iterator();
  //After removing data
  while (iterator.hasNext()){
  System.out.print(iterator.next()+" "); 
  }
  System.out.println();
  System.out.println("Now the size of list: " + list.size());
  //Remove 2nd data
  int second = list.remove(1);
  System.out.println("Data removed from 2nd location: " + second);
  System.out.print("Now the list contain: ");
  iterator = list.iterator();
  //After removing data
  while (iterator.hasNext()){
  System.out.print(iterator.next()+" "); 
  }
  System.out.println();
  System.out.println("Now the size of list: " + list.size());
  //Remove all
  list.clear();
  if (list.isEmpty()){
  System.out.println("Linked list is empty");
  }
  else{
  System.out.println( "Linked list size: " + size);
  }
  }
}

Output:
LinkedListExample
Linked List Example!
Linked list data: 11 22 33 44
Linked list size: 4
Adding data at 1st location: 55
Now the list contain: 55 11 22 33 44
Now the size of list: 5
Adding data at last location: 66
Now the list contain: 55 11 22 33 44 66
Now the size of list: 6
Adding data at 3rd location: 55
Now the list contain: 55 11 99 22 33 44 66
Now the size of list: 7
First data: 55
Last data: 66
Data at 4th position: 22
Data removed from 1st location: 55
Now the list contain: 11 99 22 33 44 66
Now the size of list: 6
Data removed from last location: 66
Now the list contain: 11 99 22 33 44
Now the size of list: 5
Data removed from 2nd location: 99
Now the list contain: 11 22 33 44
Now the size of list: 4
Linked list is empty

0 comments:

Post a Comment