Single Linked list:
class SLL{
static class Node{
int data;
Node next;
Node(int d){
data=d;
}
}
Node head;
void add(int d){
Node n=new Node(d);
if (head==null)
head=n;
else{
Node t=head;
while (t.next!=null)
t=t.next;
t.next=n;
}
}
void show(){
for (Node t=head; t!=null;t=t.next)
System.out.print(t.data+"");
}
public static void main(String[] args){
SLL l = new SLL();
l.add(10);
l.add(20);
l.add(30);
l.show();
}
}
Double Linked List:
class DLL {
// Node class
static class Node {
int data;
Node prev, next;
Node(int d) {
data = d;
}
}
// Head of the list
Node head;
// Add a new node to the end
void add(int d) {
Node n = new Node(d);
if (head == null)
head = n;
else {
Node t = head;
while (t.next != null)
t = t.next;
t.next = n;
n.prev = t;
}
}
// Display list from start to end
void show() {
for (Node t = head; t != null; t = t.next)
System.out.print(t.data + " ");
}
// Main method
public static void main(String[] args) {
DLL l = new DLL();
l.add(10);
l.add(20);
l.add(30);
l.show();
}
}
