Tuesday, July 9, 2019

Custom LinkedList implementation in java


package com.shubh.example;

public class CustomLinkedList {

 Node head; // head of list
 private static int counter;

 static class Node {

  Object data;
  Node next;

  // Constructor
  Node(Object d) {
   data = d;
   next = null;
  }

  public Object getData() {
   return data;
  }

  @SuppressWarnings("unused")
  public void setData(Object dataValue) {
   data = dataValue;
  }

  public Node getNext() {
   return next;
  }

  public void setNext(Node nextValue) {
   next = nextValue;
  }
 }

 // This methods is use to add element into list
 public void add(Object data) {
  // create new node while adding new element
  Node new_node = new Node(data);
  new_node.next = null;

  // checking if current linked list is empty the assign new data at head
  if (head == null) {
   head = new_node;
  } else {
   // else find the last node
   // and add the new element there
   Node last = head;
   while (last.next != null) {
    last = last.next;
   }

   // Insert the new element at last node
   last.next = new_node;
  }

  // increment the number of elements variable
  incrementCounter();
 }

 // add element at the specified position in the linked list
 public void add(int index, Object data) {
  Node new_node = new Node(data);
  Node currentNode = head;
  if (index == 0) {
   new_node.setNext(currentNode);
   head = new_node;
  } else {
   if (currentNode != null) {
    for (int i = 0; i < index - 1 && currentNode.getNext() != null; i++) {
     currentNode = currentNode.getNext();
    }
   }
   // set the new node's next-node reference to this node's next-node
   // reference
   new_node.setNext(currentNode.getNext());

   // now set this node's next-node reference to the new node
   currentNode.setNext(new_node);
  }

  // increment the number of elements variable
  incrementCounter();
 }

 // removes the element at the specified position in this list.
 public boolean remove(int index) {

  // check if index is out of range
  if (index < 1 || index > size()) {
   return false;
  }

  Node currentNode = head;
  if (head != null) {
   for (int i = 0; i < index - 1; i++) {
    if (currentNode.getNext() == null) {
     return false;
    } else {
     currentNode = currentNode.getNext();
    }
   }
   currentNode.setNext(currentNode.getNext().getNext());

   // decrement the number of elements variable while remove element
   decrementCounter();
   return true;

  }
  return false;
 }

 // this method is used to get element at specified index or position
 public Object get(int index) {
  // check index must be grater 0 or equal to 0 (Zero)
  if (index < 0) {
   return null;
  }
  Node currentNode = null;
  if (head != null) {
   currentNode = head;
   for (int i = 0; i < index; i++) {
    if (currentNode.getNext() == null) {
     return null;
    } else {
     currentNode = currentNode.getNext();
    }
   }
   return currentNode.getData();
  }
  return currentNode;

 }

 // Method to print the LinkedList.
 public static void printLinkedList(CustomLinkedList list) {
  Node currentNode = list.head;

  System.out.print("LinkedList: ");

  // Traverse through the LinkedList
  while (currentNode != null) {
   // Print the element at current node of list
   System.out.print(currentNode.data + " ");

   // Go to next element of list
   currentNode = currentNode.next;
   // System.out.println("--p--"+currentNode.data);
  }
  System.out.println();

 }

 private static void incrementCounter() {
  counter++;
 }

 private void decrementCounter() {
  counter--;
 }

 // returns the total number of elements in linked list.
 public int size() {
  return getCounter();
 }

 private static int getCounter() {
  return counter;
 }

 public String toString() {
  String output = "";

  if (head != null) {
   Node currentNode = head.getNext();
   while (currentNode != null) {
    output += "[" + currentNode.getData().toString() + "]";
    currentNode = currentNode.getNext();
   }

  }
  return output;
 }

 public static void main(String[] args) {
  // Initialize empty linked list
  CustomLinkedList list = new CustomLinkedList();
  // list.add(0);
  list.add(1);
  list.add(2);
  list.add(3);
  list.add(4);
  list.add(5);
  list.add(6);
  list.add(7);
  list.add(8);
  // Print all elements of the LinkedList
  printLinkedList(list);
  list.remove(3);
  System.out.println("After remove element At Index 3 : element 4 is removed");
  printLinkedList(list);
  list.add(0, 5);
  System.out.println("After add element At index 0 : element 5 is added");
  printLinkedList(list);
  list.add(3, 10);
  System.out.println("After add element At index 3 : element 10 is added.");
  printLinkedList(list);
  System.out.println("Get element At index 0 :" + list.get(0));
 }
}

No comments:

Post a Comment