Skip to content

Instantly share code, notes, and snippets.

@honux77
Created April 12, 2017 09:34
Show Gist options
  • Save honux77/c8d8b0cff181546cd19927a904180146 to your computer and use it in GitHub Desktop.
Save honux77/c8d8b0cff181546cd19927a904180146 to your computer and use it in GitHub Desktop.
package honux;
public class DoubleLinkedList {
private Node head, tail;
private int count;
public DoubleLinkedList() {
head = tail = null;
count = 0;
}
public void addFirst(int i) {
Node n = new Node(i);
n.next = head;
n.prev = null;
head = n;
if(tail == null)
tail = head;
}
public void addLast(int i){
}
public void insert(int index) {
}
public int get(int index) {
return 0;
}
public int removeFirst() {
return 0;
}
public int removeLast() {
return 0;
}
public int remove(int index) {
return 0;
}
public int size() {
return this.count;
}
public static void test(String s, boolean b) throws Exception {
if(b==false)
throw new Exception(s +" assertion fail");
}
//python range function
public static int[] range(int n) {
int[] r = new int[n];
for(int i = 0; i < n; i++)
r[i] = i;
return r;
}
//simple test
public static void main(String[] args) throws Exception {
DoubleLinkedList d = new DoubleLinkedList();
int[] r = range(5);
//test1 addFirst get
for(int i:r) {
d.addFirst(5 - i - 1); //0 1 2 3 4
}
for(int i:r) {
test("test1", d.get(i) == i);
}
//test2 addLast
for(int i:r) {
d.addLast(5 + i);
test("test2", d.get(5 + i) == 5 + i); //0 1 2 3 4 5 6 7 8 9
}
//test3 removeFirst
for(int i:r) {
int n = d.removeFirst();
test("test3", i == n);
}
//test4 removeLast
//test5
//...
}
}
class Node {
int item;
Node prev;
Node next;
Node(int i) {
this.item = i;
prev = next = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment