Friday, 8 June 2012


 
import java.io.*;
class Link
{
int data;
Link next;
Link last;
public Link(int item)
{
data =item;
}
public void displayNode()
{
System.out.println("data is"+data);
}
}
class LinkList
{
Link first;

public LinkList()
{
first=null;}
public void insertFirst(int num)
{
Link newLink=new Link(num);
newLink.next=first;
first=newLink;
newLink.last=first;
}
public void delKey(int key)
{
Link current=first;
Link prev=current;
if (current.data==key)
{
first=current.next;

}
while(current.data!=key)
{
 prev=current;
if(current.next==null)
{System.out.print("error ");
break; }
else
current=current.next;
}
prev.next=current.next;
prev.next.last=prev;
}
public void insertKey(int key,int e)//insert after key
{
Link current=first;
Link prev=current;
if (current.data==key)
{
first=current.next;

}
while(current.data!=key)
{
 prev=current;
if(current.next==null)
{System.out.print("error ");
break; }
else
current=current.next;
}
Link newLink= new Link(e);
newLink.next=current.next;
newLink.last=current;
current.next =newLink;
current=current.next;
current=current.next;
current.last=newLink;
}

void displayList()
{
Link current=first;
while(current!=null)
{
current.displayNode();
current=current.next;
}}}
class DLL
{

public  static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

int m=0,a,d,k,p;
LinkList obj=new LinkList();
while(m<4)
{
System.out.print("Enter ... 1...insert data \n2....display\n3....del\n 4...insert after key ");
 a=Integer.parseInt(br.readLine());
if(a==1)
{
System.out.print("Enter the data.....");
 d=Integer.parseInt(br.readLine());
obj.insertFirst(d);
}
else if(a==3)
{
System.out.print("Enter key ");
 k=Integer.parseInt(br.readLine());
obj.delKey(k);
obj.displayList();
}

else if(a==4)
{
System.out.print("Enter key and data");
 k=Integer.parseInt(br.readLine());
 p=Integer.parseInt(br.readLine());
obj.insertKey(k,p);
obj.displayList();
}

else if(a==2)
{
obj.displayList();
}
else
m++;
}
}
}

0 comments:

Post a Comment

Blogroll

Blogger templates

About