import
java.io.*;
class Node
{
int data;
Node next;
Node pre;
Node(int d)
{
data=d;
pre=null;
next=null;
}
void display()
{
System.out.println(data);
}
}
class List
{
DataInputStream nn=new
DataInputStream(System.in);
Node first,last;
int k;
List()
{
first=null;
last=null;
k=0;
}
void insfirst()
{
try
{
System.out.print("Enter the no : ");
int d=Integer.parseInt(nn.readLine());
Node n=new Node(d);
if(first==null)
{
first=n;
last=n;
k++;
}
else
{
n.next=first;
first.pre=n;
first=n;
k++;
}
}
catch(Exception e)
{}
}
void inslast()
{
try
{
System.out.print("Enter the no : ");
int d=Integer.parseInt(nn.readLine());
Node n=new Node(d);
Node curr=first;
if(first==null)
{
first=n;
last=n;
k++;
}
else
{
while(curr.next!=null)
{
curr=curr.next;
}
curr.next=n;
last=n;
n.pre=curr;
k++;
}
}
catch(Exception e)
{}
}
void insmid()
{
try
{
System.out.print("Enter the no : ");
int d=Integer.parseInt(nn.readLine());
System.out.print("Enter the
possition : ");
int p=Integer.parseInt(nn.readLine());
int i=1;
Node n=new Node(d);
Node curr=first;
Node prev=null;
if(first==null)
{
System.out.print("Insertion is not
possible");
}
else if(p<=k)
{
while(i<p)
{
prev=curr;
curr=curr.next;
i++;
}
prev.next=n;
n.pre=prev;
n.next=curr;
curr.pre=n;
k++;
}
else
System.out.print("cannot
insert");
}
catch(Exception e)
{}
}
void delfirst()
{
Node curr=first;
if(first==null)
System.out.print("\nNo element to
delete");
else if(first.next==null)
{
first=null;
last=null;
k--;
System.out.print("\nDeleted element is
"+curr.data);
}
else
{
first=first.next;
first.pre=null;
k--;
System.out.print("\nDeleted element is
"+curr.data);
}
}
void dellast()
{
Node curr=first;
Node prev=null;
if(first==null)
System.out.print("\nNo element to
delete");
else if(first.next==null)
{
first=null;
last=null;
k--;
System.out.print("\nDeleted element is
"+curr.data);
}
else
{
while(curr.next!=null)
{
prev=curr;
curr=curr.next;
}
last=prev;
prev.next=null;
curr.pre=null;
k--;
System.out.print("\nDeleted element is
"+curr.data);
}
}
void delmid()
{
try
{
System.out.print("Enter the
possition : ");
int p=Integer.parseInt(nn.readLine());
Node curr=first;
Node prev=null;
int i=1;
if(first==null)
System.out.print("\nNo
delition");
else if(first.next==null)
{
first=null;
last=null;
k--;
System.out.print("\nDeleted element is
"+curr.data);
}
else if(p<=k)
{
while(i<p)
{
prev=curr;
curr=curr.next;
i++;
}
prev.next=curr.next;
curr.pre=null;
curr.next.pre=prev;
curr.next=null;
k--;
System.out.print("\nDeleted element is
"+curr.data);
}
else
System.out.print("cannot
delete");
}
catch(Exception e)
{}
}
void ftrav()
{
Node curr=first;
if(first==null)
System.out.print("\nNo element
present");
else
{
while(curr!=null)
{
curr.display();
curr=curr.next;
}
}
}
void btrav()
{
Node curr=last;
if(first==null)
System.out.print("\nNo element
present");
else
{
while(curr!=null)
{
curr.display();
curr=curr.pre;
}
}
}
void search()
{
try
{
Node curr=first;
int i=0,r=0,j=0;
int a[]=new int[k];
if(first==null)
System.out.print("\nno element to
search");
else
{
System.out.print("\nelement to search
: ");
int p=Integer.parseInt(nn.readLine());
while(curr!=null)
{
if(p==curr.data)
{
i=1;
a[j]=r+1;
j++;
}
curr=curr.next;
r++;
}
if(i==0)
System.out.print("\nNot found ");
else
{
System.out.print("\nfound at loc :
");
for(i=0;i<j;i++)
System.out.print(a[i]+" ");
}
}
}
catch(Exception e)
{}
}
}
class Dl
{
public static void main(String
args[])throws Exception
{
DataInputStream get=new
DataInputStream(System.in);
int p;
List l=new List();
do
{
System.out.println("\n\n1.ins_first\n2.ins_last\n3.ins_mid");
System.out.println("4.Del_first\n5.Del_last\n6.Del_mid\n7.fortra");
System.out.print("8.btrav\n9.search\n10.Exit\n\n");
System.out.print("Enter the
choice : ");
int ch=Integer.parseInt(get.readLine());
p=ch;
switch(ch)
{
case 1 : l.insfirst();break;
case 2 : l.inslast();break;
case 3 : l.insmid();break;
case 4 : l.delfirst();break;
case 5 : l.dellast();break;
case 6 : l.delmid();break;
case 7 : l.ftrav();break;
case 8 : l.btrav();break;
case 9 : l.search();break;
}
}while(p<10);
}
}
OUTPUT
1.ins_first
2.ins_last
3.ins_mid
4.Del_first
5.Del_last
6.Del_mid
7.fortra
8.btrav
9.search
10.Exit
Enter the
choice : 1
Enter the
no : 20
1.ins_first
2.ins_last
3.ins_mid
4.Del_first
5.Del_last
6.Del_mid
7.fortra
8.btrav
9.search
10.Exit
Enter the
choice : 1
Enter the
no : 10
1.ins_first
2.ins_last
3.ins_mid
4.Del_first
5.Del_last
6.Del_mid
7.fortra
8.btrav
9.search
10.Exit
Enter the
choice : 2
Enter the
no : 30
1.ins_first
2.ins_last
3.ins_mid
4.Del_first
5.Del_last
6.Del_mid
7.fortra
8.btrav
9.search
10.Exit
Enter the
choice : 7
10
20
30
1.ins_first
2.ins_last
3.ins_mid
4.Del_first
5.Del_last
6.Del_mid
7.fortra
8.btrav
9.search
10.Exit
Enter the
choice : 8
30
20
10
1.ins_first
2.ins_last
3.ins_mid
4.Del_first
5.Del_last
6.Del_mid
7.fortra
8.btrav
9.search
10.Exit
Enter the
choice : 10






0 comments:
Post a Comment