:::: MENU ::::

Double Linked List Implementation in Java

Double Linked List have 3 attributes in the Node data, next, previous.

operations implemented are size(), isEmpty(), print(), add().

size() returns the length of the Double LinkedList

isEmpty() returns true if Double LinkedList is empty and false if not

print() prints all the elements of the Double LinkedList

add() inserts elements into the Double LinkedList.

Program

=======

public class Doubly_Linked_List_Impl
{
class Node
{
int data;
Node prev;
Node next;
}
public static Node front = null;
public static int length=0;
public boolean isEmpty()
{
return front==null;
}
public int size()
{
return length;
}
public void add(int val)
{
Node newNode = new Node();
newNode.data = val;
newNode.next = null;
newNode.prev = null;
if(front==null)
{

front = newNode;
}
else
{
Node current = front;
while(current.next!=null)
{
current = current.next;
}
newNode.prev = current;
current.next = newNode;
}
length++;
}
public void print()
{
Node current = front;
while(current!=null)
{
System.out.print(“["+current.data+"]==>”);
current = current.next;
}
}
public static void main(String args[])
{
Doubly_Linked_List_Impl dll = new Doubly_Linked_List_Impl();
dll.add(10);
dll.add(20);
dll.add(30);
dll.add(40);
dll.add(5);
dll.add(40);
dll.add(12);
dll.print();
System.out.println(“\nIs Empty() :”+dll.isEmpty());
System.out.println(“Length() :”+dll.size());
System.out.println(front.next.next.prev.data);

}
}

output

+++++

[10]==>[20]==>[30]==>[40]==>[5]==>[40]==>[12]==>

Is Empty() :false

Length() :7

20

Comments

comments


So, what do you think ?