Created
February 18, 2013 06:01
-
-
Save apremalal/4975370 to your computer and use it in GitHub Desktop.
An implementation of doubly linked list in java.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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