Bulk Emailing

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Saturday, 5 June 2010

LinkedList in Java

Posted on 00:46 by Unknown


This tutorial will show you a Linked List implementation in Java. It will have following features-
- Add nodes from both ends (Beginning and End)
- Delete nodes from Beginning
- Get count of nodes
- Get First & Last node
- Get Node at a specific position
Please let me know if you find some incorrect scenarios.


Node structure-
/**
 * LinkedList Node
 */
public class Node {
      /**
       * Holds any type of Object Data
       */
      public Object data;
      /**
       * Pointer to a Node
       */
      public Node pointer;
}

Linked List
/**
 * LinkedList Implementation
 */
public class LinkedList {

      Node first;
      /**
       * Stores the count of nodes in the linked list
       */
      int count;
     
      /**
       * Add an element at the beginning
       * @param data
       */
      public void addToBeginig(Object data){
            if(first == null){
                  /*
                   * The Linkedlist is empty and adding the first node
                   *
                   */
                  Node node = new Node();
                  node.data = data;
                  node.pointer = null;
                  first = node;
                  count = 1;
                  return;
            }
            // Add the node at the beginning
            Node newFirst = new Node();
            newFirst.data = data;
            newFirst.pointer = first;
            first = newFirst;
            count++;
      }
     
      /**
       * Add one element at the end of the list
       * You can store multiple type Objects in a single LinkedList
       * e.g. String, your Custom Class etc. as long as you cast it properly
       * @param data
       */
      public void addToEnd(Object data){
            if(first == null){
                  /*
                   * The Linkedlist is empty and adding the first node
                   *
                   */
                  Node node = new Node();
                  node.data = data;
                  node.pointer = null;
                  first = node;
                  count = 1;
                  return;
            }
            Node next = first;
            while(next.pointer != null){
                  next = (Node)next.pointer;
            }
            // Add the node at the end
            Node newNode = new Node();
            newNode.data = data;
            newNode.pointer = null;
            next.pointer = newNode;
            count++;
      }
     
     
      /**
       * Deletes the first node
       */
      public void deleteFromBegining(){
           
            if(first == null){
                  /*
                   * The Linklist is empty, nothing to delete
                   */
                  count = 0;
                  return;
            }
            // get the pointer to next node
            Node next = first;
            // delete the first node
            first = null;
            // make the next node as first node
            first = next.pointer;
            count--;
      }
     
      /**
       * Get the first node
       * @return
       */
      public Node getFirst(){
            return first;
      }
     
      /**
       * Get the last node
       * @return
       */
      public Node getLast(){
            Node next = first;
            while(next.pointer != null)
                  next = next.pointer;
            return next;
      }
     
      /**
       * Get a node at a particular position
       * @param nodeID
       */
      public Object get(int position){
            /*
             * If the position index is more than the count of
             * nodes in the Linkedlist, throw Exception
             */
            if(position >= count)
                  throw new IndexOutOfBoundsException();
           
            Node next = first;
            int i = 0;
            if(i == position)
                  return next.data;
            while(next.pointer != null){
                  next = next.pointer;
                  i++;
                  if(i == position)
                        return next.data;
            }
            // for the last element
            i++;
            return next.data;
      }
     
      /**
       * Returns the count of nodes stored in LinkedIn
       * @return
       */
      public int count(){
            return count;
      }
}
 






Email ThisBlogThis!Share to XShare to Facebook
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • CityWeather
    Update: Release 1.1 has been uploaded. It will now provide weekly forecast of your selected cities. Download   CityWeather is an Android...

Blog Archive

  • ►  2013 (6)
    • ►  September (2)
    • ►  May (1)
    • ►  April (1)
    • ►  February (1)
    • ►  January (1)
  • ►  2012 (4)
    • ►  July (2)
    • ►  March (1)
    • ►  January (1)
  • ►  2011 (11)
    • ►  November (1)
    • ►  October (2)
    • ►  August (1)
    • ►  June (1)
    • ►  April (2)
    • ►  March (3)
    • ►  January (1)
  • ▼  2010 (27)
    • ►  December (2)
    • ►  November (3)
    • ►  September (2)
    • ►  August (4)
    • ►  July (4)
    • ▼  June (7)
      • HashMap Internal
      • Binary Search Tree in Java
      • QuickSort in Java
      • Android Parcelable Example
      • CityWeather
      • LinkedList in Java
      • Android- Key Notes
    • ►  May (5)
Powered by Blogger.

About Me

Unknown
View my complete profile