About

Kannur University btech CSE study materials, question papers, syllabus . . .

Thursday, July 4, 2013

S4 DSA lab - LINKED LIST

/*                   LINKED LIST                 */

import java.io.*;
class Node
{
int data;
Node next;
Node(int d)
{
data=d;
next=null;
}
void display()
{
System.out.println(data);
}
}
class List
{
DataInputStream nn=new DataInputStream(System.in);
Node first;
int k;
List()
{
first=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;
k++;
}
else
{
n.next=first;
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;
k++;
}
else
{
while(curr.next!=null)
{
curr=curr.next;
}
curr.next=n;
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)
{
first=n;
k++;
}
else if(p<=k)
{
while(i<p)
{
prev=curr;
curr=curr.next;
i++;
}
prev.next=n;
n.next=curr;
k++;
}
else
System.out.print("cannot insert");
}
catch(Exception e)
{}
}
void delfirst()
{
Node curr=first;
if(first==null)
System.out.print("No element to delete");
else if(first.next==null)
{
first=null;
k--;
System.out.print("Deleted element is "+curr.data);
}
else
{
first=first.next;
k--;
System.out.print("Deleted element is "+curr.data);
}}
void dellast()
{
Node curr=first;
Node prev=null;
if(first==null)
System.out.print("No element to delete");
else if(first.next==null)
{
first=null;
k--;
System.out.print("Deleted element is "+curr.data);
}
else
{
while(curr.next!=null)
{
prev=curr;
curr=curr.next;
}
prev.next=null;
k--;
System.out.print("Deleted 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("No delition");
else if(first.next==null)
{
first=null;
k--;
System.out.print("Deleted element is "+curr.data);
}
else if(p<=k)
{
while(i<p)
{
prev=curr;
curr=curr.next;
i++;
}
prev.next=curr.next;
k--;
System.out.print("Deleted element is "+curr.data);
}
else
System.out.print("cannot delete");
}
catch(Exception e)
{}
}
void display()
{
Node curr=first;
if(first==null)
System.out.print("No element present");
else
{
while(curr!=null)
{
curr.display();
curr=curr.next;
}}}}
class Link
{
public static void main(String args[])throws Exception
{
DataInputStream get=new DataInputStream(System.in);
int p;
List l=new List();
do
{
System.out.println("1.ins_first\n2.ins_last\n3.ins_mid");
System.out.println("4.Del_first\n5.Del_last\n6.Del_mid\n7.Display\n8.Exit\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.display();break;
}}while(p<8);
}}

                OUTPUT    

1.ins_first
2.ins_last
3.ins_mid
4.Del_first
5.Del_last
6.Del_mid
7.Display
8.Exit
Enter the choice  : 1
Enter the no      : 2
1.ins_first
2.ins_last
3.ins_mid
4.Del_first
5.Del_last
6.Del_mid
7.Display
8.Exit
Enter the choice  : 1
Enter the no      : 1
1.ins_first
2.ins_last
3.ins_mid
4.Del_first
5.Del_last
6.Del_mid
7.Display
8.Exit
Enter the choice  : 2
Enter the no      : 3
1.ins_first
2.ins_last
3.ins_mid
4.Del_first
5.Del_last
6.Del_mid
7.Display
8.Exit
Enter the choice  : 2
Enter the no      : 4
1.ins_first
2.ins_last
3.ins_mid
4.Del_first
5.Del_last
6.Del_mid
7.Display
8.Exit
Enter the choice  : 7
1
2
3
4
1.ins_first
2.ins_last
3.ins_mid
4.Del_first
5.Del_last
6.Del_mid
7.Display
8.Exit
Enter the choice  : 4
Deleted element is 1
1.ins_first
2.ins_last
3.ins_mid
4.Del_first
5.Del_last
6.Del_mid
7.Display
8.Exit
Enter the choice  : 5
Deleted element is 4
1.ins_first
2.ins_last
3.ins_mid
4.Del_first
5.Del_last
6.Del_mid
7.Display
8.Exit
Enter the choice  : 7
2
3
1.ins_first
2.ins_last
3.ins_mid
4.Del_first
5.Del_last
6.Del_mid
7.Display
8.Exit
Enter the choice  : 3
Enter the no      : 10
Enter the possition  : 2
1.ins_first
2.ins_last
3.ins_mid
4.Del_first
5.Del_last
6.Del_mid
7.Display
8.Exit
Enter the choice  : 7
2
10
3
1.ins_first
2.ins_last
3.ins_mid
4.Del_first
5.Del_last
6.Del_mid
7.Display
8.Exit
Enter the choice  : 6
Enter the possition  : 2
Deleted element is 10
1.ins_first
2.ins_last
3.ins_mid
4.Del_first
5.Del_last
6.Del_mid
7.Display
8.Exit
Enter the choice  : 7
2
3
1.ins_first
2.ins_last
3.ins_mid
4.Del_first
5.Del_last
6.Del_mid
7.Display
8.Exit
Enter the choice  : 3
Enter the no      : 11
Enter the possition  : 5
cannot insert
1.ins_first
2.ins_last
3.ins_mid
4.Del_first
5.Del_last
6.Del_mid
7.Display
8.Exit
Enter the choice  : 6
Enter the possition  : 5
cannot delete
1.ins_first
2.ins_last
3.ins_mid
4.Del_first
5.Del_last
6.Del_mid
7.Display
8.Exit
Enter the choice  : 5
Deleted element is 2
1.ins_first
2.ins_last
3.ins_mid
4.Del_first
5.Del_last
6.Del_mid
7.Display
8.Exit
Enter the choice  : 5
Deleted element is 3
1.ins_first
2.ins_last
3.ins_mid
4.Del_first
5.Del_last
6.Del_mid
7.Display
8.Exit
Enter the choice  : 5
Deleted element is 2
1.ins_first
2.ins_last
3.ins_mid
4.Del_first
5.Del_last
6.Del_mid
7.Display
8.Exit
Enter the choice  : 5
No element to delete
1.ins_first
2.ins_last
3.ins_mid
4.Del_first
5.Del_last
6.Del_mid
7.Display
8.Exit
Enter the choice  : 8

























0 comments:

Post a Comment