Created
October 9, 2018 03:42
-
-
Save prdk0/c377cfbff390ea144ff5c519352811e8 to your computer and use it in GitHub Desktop.
Implemetation of basic LinkedList with add,find,delete with keywords or position values. Implemented feature like contains? ,which will return a boolean.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dsa; | |
/** | |
* Author: pradeek<pradeek.k@gmail.com> | |
* Date: 21/8/18 | |
* Jdk-version: 10.0.1 | |
* Time: 11:11 AM | |
*/ | |
public class Main{ | |
public static void main(String args[]){ | |
} | |
} | |
class Node{ | |
private Node next; | |
private Object data; | |
public Node(Object data){ | |
this.data = data; | |
next = null; | |
} | |
public Node(Object data, Node next){ | |
this.data = data; | |
this.next = next; | |
} | |
public Node getNext() { | |
return next; | |
} | |
public void setNext(Node next) { | |
this.next = next; | |
} | |
public Object getData() { | |
return data; | |
} | |
public void setData(Object data) { | |
this.data = data; | |
} | |
} | |
class LinkedList{ | |
private int length; | |
private Node head; | |
public Object findin(int position){ | |
Node currentNode = head; | |
int counter = 1; | |
if (getLength() == 0 || position < 1 || position > getLength()){ | |
throw new NullPointerException("No value found in this position: "+"'"+position+"'"); | |
} | |
if (currentNode == null){ | |
System.out.println("No value found in this position"); | |
} | |
if (position == 1){ | |
return currentNode.getData(); | |
} | |
while (counter < position && currentNode.getNext() != null){ | |
currentNode = currentNode.getNext(); | |
counter++; | |
} | |
return currentNode.getData(); | |
} | |
public boolean contains(Object data){ | |
Node currentNode = head; | |
if (currentNode == null){ | |
return false; | |
}else if(currentNode.getData() == data){ | |
return true; | |
}else { | |
while(currentNode.getNext() != null){ | |
currentNode = currentNode.getNext(); | |
if (currentNode.getData() == data){ | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
public void delete_by(Object position){ | |
if (position instanceof Integer){ | |
int position_val = Integer.parseInt(position.toString()); | |
deleteNodeAtNthPosition(position_val); | |
}else if (position instanceof String){ | |
String position_str = position.toString(); | |
switch(position_str){ | |
case "END": | |
deleteNodeAtLast(); | |
break; | |
case "BEG": | |
deleteNodeAtFirst(); | |
break; | |
default: | |
System.out.println("Invalid position value: "+"'"+position+"'"); | |
} | |
}else { | |
System.out.println("Incompactible type of position: "+"'"+position+"'"); | |
} | |
} | |
public void addNode(Object data, Object position){ | |
if(position instanceof Integer){ | |
int position_val = Integer.parseInt(position.toString()); | |
addAtNthPosition(data, position_val); | |
}else if(position instanceof String){ | |
String position_str = (String) position; | |
switch (position_str){ | |
case "BEG": | |
addNodeAtBeginning(data); | |
break; | |
case "END": | |
addNodeAtLast(data); | |
break; | |
default: | |
System.out.println("Invalid postion value: "+"'"+position+"'"); | |
} | |
}else { | |
System.out.println("Incompactible type of position value: "+"'"+position+"'"); | |
} | |
} | |
private void addNodeAtLast(Object data){ | |
Node currentNode = head; | |
Node newNode = new Node(data); | |
if (currentNode == null){ | |
head = newNode; | |
} | |
if (currentNode != null){ | |
while (currentNode.getNext() != null){ | |
currentNode = currentNode.getNext(); | |
} | |
currentNode.setNext(newNode); | |
} | |
incrementLength(); | |
} | |
private void addAtNthPosition(Object data, int position){ | |
int counter = 1; | |
Node currentNode = head; | |
if (currentNode == null){ | |
head = new Node(data); | |
} | |
if (length == 0 || position < 1 || position > length){ | |
throw new NullPointerException("Node not found in this position : "+position); | |
} | |
if(position == 1){ | |
addNodeAtBeginning(data); | |
}else if (position == getLength()){ | |
addNodeAtLast(data); | |
}else { | |
while(counter < position -1){ | |
currentNode = currentNode.getNext(); | |
counter++; | |
} | |
Node newNode = new Node(data); | |
Node tailNode = currentNode.getNext(); | |
currentNode.setNext(newNode); | |
newNode.setNext(tailNode); | |
incrementLength(); | |
} | |
} | |
private void addNodeAtBeginning(Object data){ | |
Node currentNode = head; | |
Node newNode = new Node(data); | |
newNode.setNext(currentNode); | |
head = newNode; | |
incrementLength(); | |
} | |
public void add(Object data){ | |
addNodeAtLast(data); | |
} | |
public void delete(Object data){ | |
Node currentNode = head; | |
if (currentNode == null){ | |
System.out.println("cannot perform delete on empty list"); | |
}else if (currentNode.getData() == data){ | |
currentNode = currentNode.getNext(); | |
head = currentNode; | |
decrementLength(); | |
}else { | |
while (currentNode.getNext() != null){ | |
if (currentNode.getNext().getData() == data){ | |
currentNode.setNext(currentNode.getNext().getNext()); | |
decrementLength(); | |
break; | |
} | |
currentNode = currentNode.getNext(); | |
} | |
} | |
} | |
private void deleteNodeAtNthPosition(int position){ | |
int counter = 1; | |
Node currentNode = head; | |
if(currentNode == null){ | |
System.out.println("Empty List cannot delete."); | |
} | |
if (length == 0 || position < 1 || position > length){ | |
throw new NullPointerException("Node not found in this position : "+position); | |
} | |
if (position == 1){ | |
deleteNodeAtFirst(); | |
}else if (position == getLength()){ | |
deleteNodeAtLast(); | |
}else { | |
while(counter < position - 1){ | |
currentNode = currentNode.getNext(); | |
counter++; | |
} | |
currentNode.setNext(currentNode.getNext().getNext()); | |
decrementLength(); | |
} | |
} | |
private void deleteNodeAtLast(){ | |
Node currentNode = head; | |
int count = 1; | |
if(currentNode == null){ | |
System.out.println("Delete operation cannot perform on empty list."); | |
} | |
while (count < getLength() - 1){ | |
currentNode = currentNode.getNext(); | |
count++; | |
} | |
currentNode.setNext(null);; | |
decrementLength(); | |
} | |
private void deleteNodeAtFirst(){ | |
Node currentNode = head; | |
if (currentNode == null){ | |
System.out.println("Delete operation cannot perform on empty list."); | |
} | |
currentNode = currentNode.getNext(); | |
head = currentNode; | |
decrementLength(); | |
} | |
private int decrementLength(){ | |
return length--; | |
} | |
private int incrementLength(){ | |
return length++; | |
} | |
public int getLength(){ | |
return length; | |
}; | |
public String toString(){ | |
String output = ""; | |
Node currentNode = head; | |
if (currentNode == null){ | |
System.out.println("Empty list"); | |
}else { | |
while (currentNode != null){ | |
output += currentNode.getData().toString()+" -> "; | |
currentNode = currentNode.getNext(); | |
} | |
} | |
return "HEAD -> "+output+"NULL"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment