Skip to content

Instantly share code, notes, and snippets.

@hoangong
Last active January 7, 2019 01:33
Show Gist options
  • Save hoangong/71806c42c48d659ffa86 to your computer and use it in GitHub Desktop.
Save hoangong/71806c42c48d659ffa86 to your computer and use it in GitHub Desktop.
Linked List implementation #linkedlist #java
/**
* Created by hoangong on 23/12/2015.
*/
public class LinkedList {
private Node head;
private Node tail;
private int count;
public Node getHead() {
return head;
}
public Node getTail() {
return tail;
}
public void add(Node i) {
if(i == null)
return;
if (head == null) {
setHead(i);
setTail(i);
} else {
getTail().setNext(i);
setTail(i);
}
count++;
}
public void setHead(Node head) {
this.head = head;
}
public void setTail(Node tail) {
this.tail = tail;
}
public int getCount() {
return count;
}
@Override
public String toString() {
Node cur = getHead();
StringBuilder s = new StringBuilder();
while (cur != null) {
if (s.toString().equals("")) {
s.append(cur.getValue());
} else {
s.append(" -> " + cur.getValue());
}
cur = cur.getNext();
}
return s.toString();
}
public Node find(int i) {
Node cur = getHead();
while (cur != null) {
if(cur.getValue() == i)
return cur;
cur = cur.getNext();
}
return null;
}
public void delete(int i) {
Node pre = null;
Node cur = getHead();
while (cur != null) {
if(cur.getValue() == i){
if(pre == null){
setHead(cur.getNext());
} else {
pre.setNext(cur.getNext());
}
count--;
return;
}
pre = cur;
cur = cur.getNext();
}
}
}
class Node {
private final Integer value;
private Node next;
public Node(Integer i, Node next) {
this.value = i;
this.next = next;
}
public void setNext(Node next){
this.next = next;
}
public Integer getValue() {
return value;
}
public Node getNext() {
return next;
}
}
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Created by hoangong on 23/12/2015.
*/
public class LinkedListTest {
@Test
public void createEmptyLLWithEmptyHeadAndTail(){
LinkedList ll = new LinkedList();
assertEquals(null,ll.getHead());
assertEquals(null,ll.getTail());
}
@Test
public void setFirstItemWillSetAsHeadAndTail(){
LinkedList ll = new LinkedList();
Node i = new Node(1, null);
ll.add(i);
assertEquals(i,ll.getHead());
assertEquals(i,ll.getTail());
}
@Test
public void setNotFirstItemAppendToTheBottom(){
LinkedList ll = new LinkedList();
Node i1 = new Node(1, null);
Node i2 = new Node(2, null);
Node i3 = new Node(3, null);
ll.add(i1);
ll.add(i2);
ll.add(i3);
assertEquals(i1,ll.getHead());
assertEquals(i3,ll.getTail());
}
@Test
public void canCount(){
LinkedList ll = new LinkedList();
Node i1 = new Node(1, null);
Node i2 = new Node(2, null);
Node i3 = new Node(3, null);
ll.add(i1);
ll.add(i2);
ll.add(i3);
assertEquals(3,ll.getCount());
}
@Test
public void cannotAddNull(){
LinkedList ll = new LinkedList();
Node i1 = new Node(1, null);
Node i2 = new Node(2, null);
Node i3 = new Node(3, null);
ll.add(i1);
ll.add(i2);
ll.add(i3);
ll.add(null);
assertEquals(3,ll.getCount());
}
@Test
public void printLL(){
LinkedList ll = new LinkedList();
Node i1 = new Node(1, null);
Node i2 = new Node(2, null);
Node i3 = new Node(3, null);
ll.add(i1);
ll.add(i2);
ll.add(i3);
assertEquals("1 -> 2 -> 3", ll.toString());
}
@Test
public void printEmpty(){
LinkedList ll = new LinkedList();
assertEquals("", ll.toString());
}
@Test
public void findAnItem(){
LinkedList ll = new LinkedList();
Node i1 = new Node(1, null);
Node i2 = new Node(2, null);
Node i3 = new Node(3, null);
ll.add(i1);
ll.add(i2);
ll.add(i3);
Node find = ll.find(2);
assertEquals(i2, find);
}
@Test
public void notFindAnItem(){
LinkedList ll = new LinkedList();
Node i1 = new Node(1, null);
Node i2 = new Node(2, null);
Node i3 = new Node(3, null);
ll.add(i1);
ll.add(i2);
ll.add(i3);
Node find = ll.find(5);
assertEquals(null, find);
}
@Test
public void deleteItem(){
LinkedList ll = new LinkedList();
Node i1 = new Node(1, null);
Node i2 = new Node(2, null);
Node i3 = new Node(3, null);
ll.add(i1);
ll.add(i2);
ll.add(i3);
ll.delete(2);
assertEquals(2, ll.getCount());
assertEquals(i1, ll.getHead());
assertEquals(i3, ll.getTail());
ll.delete(3);
assertEquals(1, ll.getCount());
assertEquals(i1, ll.getHead());
ll.delete(1);
assertEquals(0, ll.getCount());
assertEquals(null, ll.getHead());
}
@Test
public void deleteNonExistItem(){
LinkedList ll = new LinkedList();
Node i1 = new Node(1, null);
Node i2 = new Node(2, null);
Node i3 = new Node(3, null);
ll.add(i1);
ll.add(i2);
ll.add(i3);
ll.delete(5);
assertEquals(3, ll.getCount());
assertEquals(i1, ll.getHead());
assertEquals(i3, ll.getTail());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment