Last active
December 13, 2015 21:08
-
-
Save apremalal/4975361 to your computer and use it in GitHub Desktop.
An implementation of single 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
import java.util.Arrays; | |
public class SingleLinkedList { | |
Node head; | |
int i = 0; | |
void insert(SingleLinkedList li, Node x) { | |
x.next = li.head; | |
li.head = x; | |
} | |
Node search(SingleLinkedList li, int key) { | |
Node x = li.head; | |
while (x != null && x.key != key) { | |
x = x.next; | |
} | |
return x; | |
} | |
} | |
class Node { | |
Node next; | |
int key; | |
Node(int key) { | |
this.key = key; | |
} | |
} | |
//Testing the algorithm | |
class Test { | |
public static void main(String args[]) { | |
SingleLinkedList sl = new SingleLinkedList(); | |
sl.insert(sl, new Node(4)); | |
sl.insert(sl, new Node(3)); | |
sl.insert(sl, new Node(9)); | |
sl.insert(sl, new Node(6)); | |
System.out.println(sl.search(sl, 20).key); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment