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

About Sanal Kumar

A tech evangelist, hobbyist blogger(www.inspyreit.com), <!-- Place this tag in the of your document --> window.___gcfg = {lang: 'en'}; (function() {var po = document.createElement("script"); po.type = "text/javascript"; po.async = true;po.src = "https://apis.google.com/js/plusone.js"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(po, s); })();

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>