Skip to content

Instantly share code, notes, and snippets.

@apremalal
Created February 18, 2013 06:01
Show Gist options
  • Save apremalal/4975370 to your computer and use it in GitHub Desktop.
Save apremalal/4975370 to your computer and use it in GitHub Desktop.
An implementation of doubly linked list in java.
public class LinkedList {
Node head;
void insert(LinkedList li, Node x) {
x.next = li.head;
if (li.head != null) {
li.head.pre = x;
}
li.head = x;
x.pre = null;
}
Node search(LinkedList li, int key) {
Node x = li.head;
while (x != null && x.key != key) {
x = x.next;
}
return x;
}
void delete(LinkedList l, Node x) {
x = search(l, x);
if (x.pre != null) {
x.pre.next = x.next;
} else {
l.head = x.next;
}
if (x.next != null) {
x.next.pre = x.pre;
}
}
}
class Node {
Node pre;
Node next;
int key;
Node(int key) {
this.key = key;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment