Skip to content

Instantly share code, notes, and snippets.

@malalanayake
Created June 15, 2014 13:44
Show Gist options
  • Save malalanayake/d7d2130e59b805e3c20d to your computer and use it in GitHub Desktop.
Save malalanayake/d7d2130e59b805e3c20d to your computer and use it in GitHub Desktop.
Linked List Implementation with Generics
import java.util.Currency;
/**
* LinkList implementation with generics
*
* @author malalanayake
*
* @param <T>
*/
public class LinkedListWithGenerics<T> {
private LinkedNode<T> first;
private LinkedNode<T> last;
private int count;
/**
* Internal linked node implementation
*
* @author malalanayake
*
* @param <T>
*/
private class LinkedNode<T> {
private T data;
private LinkedNode<T> next;
public LinkedNode() {
this.data = null;
this.next = null;
}
public LinkedNode(T obj) {
this.data = obj;
this.next = null;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public LinkedNode<T> getNext() {
return next;
}
public void setNext(LinkedNode<T> next) {
this.next = next;
}
}
public LinkedListWithGenerics() {
LinkedNode<T> newLiked = new LinkedNode<T>();
this.first = newLiked;
this.last = this.first;
}
/**
* Add values to the list
*
* @param data
*/
public void add(T data) {
LinkedNode<T> newData = new LinkedNode<T>(data);
if (this.first.getData() == null) {
this.first = newData;
this.last = this.first;
} else {
this.last.setNext(newData);
this.last = newData;
}
count++;
}
/**
* Remove values from the list
*
* @param data
*/
public void remove(T data) {
LinkedNode<T> current = first;
if (this.first.getData().equals(data)) {
if (this.first.getNext() == null) {
LinkedNode<T> newNode = new LinkedNode<T>();
this.first.setData(null);
this.first = newNode;
this.last = this.first;
} else {
this.first.setData(null);
this.first = this.first.getNext();
}
} else {
boolean wasDeleted = false;
while (!wasDeleted) {
LinkedNode<T> currentNext = current.getNext();
if (currentNext.getData().equals(data)) {
currentNext.setData(null);
current.setNext(currentNext.getNext());
currentNext = null;
wasDeleted = true;
count--;
} else {
current = current.getNext();
}
}
}
}
public void print() {
boolean allPrinted = false;
LinkedNode<T> crr = first;
System.out.print("[");
while (!allPrinted) {
if (crr.getData() != null) {
if (crr.getNext() != null) {
System.out.print(crr.getData().toString() + ",");
LinkedNode<T> crrNext = crr.getNext();
crr = crrNext;
} else {
System.out.print(crr.getData().toString() + "]");
allPrinted = true;
}
} else {
allPrinted = true;
}
}
System.out.println();
}
public int getCount() {
return count;
}
public static void main(String[] args) {
LinkedListWithGenerics<String> linkedLst = new LinkedListWithGenerics<String>();
linkedLst.add("Test");
linkedLst.add("Free");
linkedLst.add("Yes");
linkedLst.add("Me");
linkedLst.print();
System.out.println(linkedLst.getCount());
linkedLst.remove("Me");
linkedLst.print();
System.out.println(linkedLst.getCount());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment