Write your own LinkedList

At times, one wants to write their implementation of LinkedList, one can use the sample as below:


package com.dirtyyourhands;

public class LinkedList {
  private Student firstElement;
  private int elementCount;

  public LinkedList() {
    this.firstElement = null;
    elementCount = 0;
  }

  /**
   * This helps add new entry to the list
   * 
   * @param studentNode
   *            Student object
   */
  public void add(Student studentNode) {
    if (null == firstElement) {
      this.firstElement = studentNode;
    } else {
      Student currentElement = firstElement;
      while (currentElement.getNext() != null) {
        currentElement = currentElement.getNext();
      }
      currentElement.setNext(studentNode);
    }
    elementCount++;
  }

  /**
   * This helps add new entry to the list at specified position. Start index is counted as 1
   * 
   * @param index
   *            int - position at which new entry should be added
   * @param studentNode
   *            Student - object
   * @throws Exception
   *             throws Exception if index is out of range
   */
  public void add(int index, Student studentNode) throws Exception {
    Student tempElement = studentNode;
    // get element at index
    Student currentElement = get(index);
    if (null == currentElement) {
      throw new Exception(
          "List has lesser values than the position where insertion is requested");
    } else {
      // set element
      tempElement.setNext(currentElement.getNext());
      currentElement.setNext(tempElement);
      elementCount++;
    }
  }

  /**
   * This helps find the element at specified index
   * 
   * @param index
   *            int - index of which the element stored is needed
   * 
   * @return Student - found element is returned
   */
  public Student get(int index) {
    if (index <= 0 || index > elementCount)
      return null;

    Student currentElement = firstElement;
    while (currentElement.getNext() != null && index > 1) {
      currentElement = currentElement.getNext();
      index--;
    }

    if (index > 1)
      return null;

    return currentElement;
  }

  /**
   * This helps remove element from specified index
   * 
   * @param index
   *            int
   * 
   * @return boolean - true if success, else false
   */
  public boolean remove(int index) {
    if (index < 1 || index > size())
      return false;

    Student currentElement = get(index - 1);
    currentElement.setNext(currentElement.getNext().getNext());
    elementCount--;
    return true;
  }

  /**
   * @return the head
   */
  public Student getHead() {
    return firstElement;
  }

  /**
   * @param head
   *            the head to set
   */
  public void setHead(Student head) {
    this.firstElement = head;
  }

  public int size() {
    return getElementCount();
  }

  /**
   * @return the elementCount
   */
  public int getElementCount() {
    return elementCount;
  }

  /**
   * @param elementCount
   *            the elementCount to set
   */
  public void setElementCount(int elementCount) {
    this.elementCount = elementCount;
  }

  /**
   */
  public String toString() {
    Student currentElement = firstElement;
    StringBuffer output = new StringBuffer();
    while (currentElement != null) {
      output.append(currentElement);
      currentElement = currentElement.getNext();
    }
    return output.toString();
  }
}

class Student {
  private String firstName;
  private String lastName;
  private int rollNumber;

  private Student next = null;

  /**
   * Default Constructor
   */
  public Student() {
  }

  /**
   * @param firstName
   * @param lastName
   * @param rollNumber
   */
  public Student(String firstName, String lastName, int rollNumber) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.rollNumber = rollNumber;
  }

  /**
   * @return the firstName
   */
  public String getFirstName() {
    return firstName;
  }

  /**
   * @param firstName
   *            the firstName to set
   */
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  /**
   * @return the lastName
   */
  public String getLastName() {
    return lastName;
  }

  /**
   * @param lastName
   *            the lastName to set
   */
  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

  /**
   * @return the rollNumber
   */
  public int getRollNumber() {
    return rollNumber;
  }

  /**
   * @param rollNumber
   *            the rollNumber to set
   */
  public void setRollNumber(int rollNumber) {
    this.rollNumber = rollNumber;
  }

  /**
   * @return the next
   */
  public Student getNext() {
    return next;
  }

  /**
   * @param next
   *            the next to set
   */
  public void setNext(Student next) {
    this.next = next;
  }

  public String toString() {
    return "[" + getFirstName() + " " + getLastName() + " " + getRollNumber() + " ]";
  }

}

For testing the same, use sample code snippet below


package com.dirtyyourhands;

public class Test {
  public static void main(String[] args) {
    LinkedList linkedList = new LinkedList();

    try {
      // add elements to LinkedList
      linkedList.add(new Student("Name", "001", 1));
      linkedList.add(new Student("Name", "002", 2));
      linkedList.add(new Student("Name", "003", 3));
      linkedList.add(new Student("Name", "004", 4));
      linkedList.add(new Student("Name", "005", 5));

      System.out.println("linkedList - print  before adding at position: " + linkedList);
      linkedList.add(2, new Student("Name", "006", 6));
      // linkedList.add(7, new Student("Name", "005", 5)); // Unblock this line to see error

      System.out.println("linkedList - print : " + linkedList);
      System.out.println("linkedList.size() - print  size: " + linkedList.size());
      System.out.println("linkedList.get(2) - get 2nd element: " + linkedList.get(2));
      System.out
          .println("linkedList.remove(2) - remove 2nd element: " + linkedList.remove(2));
      System.out.println("linkedList.size() - print linkedlist size: " + linkedList.size());
      System.out.println("linkedList - print linkedlist: " + linkedList);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

This will produce an output as below


linkedList - print  before adding at position: [Name 001 1 ][Name 002 2 ][Name 003 3 ][Name 004 4 ][Name 005 5 ]
linkedList - print : [Name 001 1 ][Name 002 2 ][Name 006 6 ][Name 003 3 ][Name 004 4 ][Name 005 5 ]
linkedList.size() - print  size: 6
linkedList.get(2) - get 2nd element: [Name 002 2 ]
linkedList.remove(2) - remove 2nd element: true
linkedList.size() - print linkedlist size: 5
linkedList - print linkedlist: [Name 001 1 ][Name 006 6 ][Name 003 3 ][Name 004 4 ][Name 005 5 ]