Skip to content

Instantly share code, notes, and snippets.

@malalanayake
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malalanayake/4d2001cb2f684a83728e to your computer and use it in GitHub Desktop.
Save malalanayake/4d2001cb2f684a83728e to your computer and use it in GitHub Desktop.
Simple Linked list implementation in java
/**
* Class which is provide the linked list facility
*
* @author malalanayake
*
*/
public class LinkedList {
private LinkNode first;
private LinkNode last;
/**
* Class which is contain the data
*
* @author malalanayake
*
*/
private class LinkNode {
private Object data;
private LinkNode next;
public LinkNode() {
this.data = null;
this.next = null;
}
public LinkNode(Object obj) {
this.data = obj;
this.next = null;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public LinkNode getNext() {
return next;
}
public void setNext(LinkNode next) {
this.next = next;
}
}
public LinkedList() {
// creating the first node
this.first = new LinkNode();
this.last = this.first;
}
public LinkNode getFirst() {
return first;
}
public void setFirst(LinkNode first) {
this.first = first;
}
public LinkNode getLast() {
return last;
}
public void setLast(LinkNode last) {
this.last = last;
}
public void add(Object obj) {
LinkNode linkNode = new LinkNode(obj);
// check whether it is the first node
if (this.first.getData() == null) {
this.first = linkNode;
this.last = linkNode;
} else {
this.last.setNext(linkNode);
this.last = linkNode;
}
}
public void remove(Object obj) {
LinkNode currentNode = first;
// If the first object is removing
if (obj.equals(first.getData())) {
// if the one node is there
if (first.getNext() == null) {
this.first.setData(null);
this.first = new LinkNode();
this.last = this.first;
} else {
currentNode.setData(null);
currentNode = currentNode.getNext();
this.first = currentNode;
}
} else {
// In the middle object is removing
boolean wasDeleted = false;
while (!wasDeleted) {
// go for the next node
LinkNode nextNode = currentNode.getNext();
if (nextNode != null) {
if (nextNode.getData().equals(obj)) {
currentNode.setNext(nextNode.getNext());
nextNode = null;
wasDeleted = true;
} else {
currentNode = nextNode;
}
}
}
}
}
/**
* Printing whole list
*/
public void print() {
LinkNode current = first;
boolean isDone = false;
while (!isDone) {
System.out.print(current.data + " ");
if (current.next == null) {
isDone = true;
}
current = current.next;
}
System.out.println();
}
public static void main(String[] args) {
LinkedList linkedList = new LinkedList();
// just add
linkedList.add("1");
linkedList.add("2");
linkedList.add("3");
linkedList.print();
// just remove
linkedList.remove("2");
linkedList.print();
// remove the first element
linkedList.add("2");
linkedList.remove("1");
linkedList.print();
// remove the last element
linkedList.add("1");
linkedList.remove("1");
linkedList.print();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment