Double Linked List

Posted: March 6, 2012 in Java Programs

import java.util.*;
class Node2
{
int data;
Node2 next,prev;
public Node2(int data)
{
this.data=data;
next=prev=null;
}
}
class DLinkedList
{
private Node2 start=null,last=null;
public boolean empty()
{
if(start==null)
return true;
else
return false;
}
public void create(int n)
{
Node2 p,q;
for(int i=0;i<n;i++)
{
Scanner src=new Scanner(System.in);
System.out.println(“Enter data”);
int ele=src.nextInt();
p=new Node2(ele);
if (start==null)
start=last=p;
else
{
last.next=p;
p.prev=last;
last=p;
}
}
}
public void insertBeg(int ele)
{
Node2 p=new Node2(ele);
if(start==null)
start=last=p;
else
{
p.next=start;
start.prev=p;
start=p;
}
}
public void insertEnd(int ele)
{
Node2 p=new Node2(ele);
if(last==null)
start=last=p;
else
{
p.prev=last;
last.next=p;
last=p;
}
}
public void insertPos(int ele,int pos)
{
Node2 p,q=start;
p=new Node2(ele);
for(int i=0;i<pos-2;i++)
q=q.next;
p.next=q.next;
p.prev=q;
q.next=p;
p.next.prev=p;
}
public int deleteBeg()
{
Node2 p=start;
if(start==last)
start=last=null;
else
start=start.next;
return p.data;
}
public int deleteEnd()
{
Node2 p=last,q;
if(start==last)
start=last=null;
else
{
q=last.prev;
q.next=null;
last=q;
}
return p.data;
}
public int deletePos(int pos)
{
Node2 p,q=start;
for(int i=0;i<pos-2;i++)
q=q.next;
p=q.next;
q.next=p.next;
p.next.prev=q;
return p.data;
}
public void deleteEle(int ele)
{
Node2 p,q=start;
while(q!=null)
{
if(q.data==ele)
break;
q=q.next;
}
if(q==null)
System.out.println(ele+” is not found”);
else
{
System.out.println(ele+” is deleted”);
if(q==start)
{
start=start.next;
start.prev=null;
}
else
{
p=q.prev;
p.next=q.next;
q.next.prev=p;
}
}
}
public void display()
{
Node2 q=start;
while(q!=null)
{
System.out.print(q.data+” “);
q=q.next;
}
System.out.println();
}
public void revdis()
{
Node2 q=last;
while(q!=null)
{
System.out.print(q.data+” “);
q=q.prev;
}
System.out.println();
}
}
class DLLExp
{
public static void main(String g[])
{
int ch,pele;
Scanner src=new Scanner(System.in);
DLinkedList obj=new DLinkedList();
System.out.println(“Enter no of elements to create”);
int n=src.nextInt();
obj.create(n);
while(true)
{
System.out.println(“System choice 1:insert Beg 2:Insert End”);
System.out.println(“3:insert pos 4:delete beg 5:delete end 6:delete pos”);
System.out.println(“7:delete element 8:display 9:Reverse display 10:exit”);
ch=src.nextInt();
if(ch==10)
break;
switch(ch)
{
case 1:
System.out.println(“enter the element to instert”);
int ele=src.nextInt();
obj.insertBeg(ele);
break;
case 2:
System.out.println(“enter the element to instert”);
ele=src.nextInt();
obj.insertEnd(ele);
break;
case 3:
System.out.println(“enter the element to instert”);
ele=src.nextInt();
System.out.println(“enter the position”);
int pos=src.nextInt();
obj.insertPos(ele,pos);
break;
case 4:
if(obj.empty())
System.out.println(“doubly linked list is empty”);
else
{
pele=obj.deleteBeg();
System.out.println(“Deleted element is “+pele);
}
break;
case 5:
if(obj.empty())
System.out.println(“doubly linked list is empty”);
else
{
pele=obj.deleteEnd();
System.out.println(“Deleted element is “+pele);
}
break;
case 6:
if(obj.empty())
System.out.println(“doubly linked list is empty”);
else
{
System.out.println(“enter the position”);
pos=src.nextInt();
pele=obj.deletePos(pos);
System.out.println(“Deleted element is “+pele);
}
break;
case 7:
if(obj.empty())
System.out.println(“doubly linked list is empty”);
else
{
System.out.println(“enter element to delete”);
ele=src.nextInt();
obj.deleteEle(ele);
}
break;
case 8:
if(obj.empty())
System.out.println(“doubly linked list is empty”);
else
obj.display();
break;
case 9:
if(obj.empty())
System.out.println(“doubly linked list is empty”);
else
obj.revdis();
}
}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s