import java.util.*;
class Nodetype
{
int info;
Nodetype next;
Nodetype(int i)
{
info=i;
next=null;
}
}
//-----------------------------------------------------------------------------------------------------------
class Operations
{
Nodetype list=null;
void display( )
{
/*display all elements of linked list*/
Nodetype temp;
if(list==null)
System.out.println("\nEmpty linked list");
else
{
temp=list;
//System.out.println("\n"+temp);
System.out.println();
while(temp!=null)
{
System.out.print("-->"+temp.info);
temp=temp.next;
}
}
}/*end display*/
//-----------------------------------------------------------------------------------------------------------
void insert(int x)
{
Nodetype p=null;
Nodetype temp=list;
Nodetype node=new Nodetype(x);
if(temp==null)
{
list=node;
node.next=temp;
display( );
return;
}
while(temp!=null)
/*travel linked list till u get its correct position*/
{
if(x >= temp.info)
{
p=temp;
temp=temp.next;
}
else
break;
}
if(temp!=null&&list==temp)
/*insert new node in the beginning*/
{ list=node;
node.next=temp;
display( );
return;
}
/*insert in the middle*/
node.next=p.next;
p.next=node;
display( );
}/*end insert*/
//-----------------------------------------------------------------------------------------------------------
void deleten()
{
System.out.print("\n\nDeleted Element : "+list.info);
list=list.next;
display( );
}
//-----------------------------------------------------------------------------------------------------------
void search(int x)
{
/*search an element in linked list and return its location*/
int i=1;
Nodetype q;
if(list==null)
System.out.println("\nList is empty");
else
{
q=list;
do
{
if(q.info==x)
{
System.out.print("\nElement found at location "+i);
break;
}
i++;
q=q.next;
}
while(q!=null);
if(q==null)
System.out.println("\nElement not found");
}
}/*end search*/
int getNumber()
{
String str;
int ne=0;
InputStreamReader input=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(input);
try
{
str=in.readLine();
ne=Integer.parseInt(str);
}
catch(Exception e)
{
System.out.println("I/O Error");
}
return ne;
}/*end getNumber*/
void menu()
{
int n,ch=0;
do
{
System.out.println("1.Insert");
System.out.println("2.Delete");
System.out.println("3.Search");
System.out.println("4.Exit");
System.out.print("Enter choice : ");
n=getNumber();
switch(n)
{
case 1:System.out.print("Enter value : ");
n=getNumber();
insert(n);
break;
case 2:deleten();
break;
case 3:System.out.print("Enter value : ");
n=getNumber();
search(n);
break;
case 4:System.exit(0);
}
System.out.print("\n\nDo u want to continue(1/0)? : ");
ch=getNumber();
}
while(ch==1);
}
}
//-----------------------------------------------------------------------------------------------------------
class PQSortedLL
{
public static void main(String args[])
{
Operations L =new Operations( );
L.menu();
}/*end main*/
}
/*
Sample Input and Output :
1.Insert
2.Delete
3.Search
4.Exit
Enter choice : 1
Enter value : 1
-->1
Do u want to continue(1/0)? : 1
1.Insert
2.Delete
3.Search
4.Exit
Enter choice : 1
Enter value : 100
-->1-->100
Do u want to continue(1/0)? : 1
1.Insert
2.Delete
3.Search
4.Exit
Enter choice : 1
Enter value : 50
-->1-->50-->100
Do u want to continue(1/0)? : 1
1.Insert
2.Delete
3.Search
4.Exit
Enter choice : 1
Enter value : 25
-->1-->25-->50-->100
Do u want to continue(1/0)? : 1
1.Insert
2.Delete
3.Search
4.Exit
Enter choice : 1
Enter value : 75
-->1-->25-->50-->75-->100
Do u want to continue(1/0)? : 1
1.Insert
2.Delete
3.Search
4.Exit
Enter choice : 1
Enter value : 20
-->1-->20-->25-->50-->75-->100
Do u want to continue(1/0)? : 1
1.Insert
2.Delete
3.Search
4.Exit
Enter choice : 1
Enter value : 80
-->1-->20-->25-->50-->75-->80-->100
Do u want to continue(1/0)? : 1
1.Insert
2.Delete
3.Search
4.Exit
Enter choice : 2
Deleted Element : 1
-->20-->25-->50-->75-->80-->100
Do u want to continue(1/0)? : 1
1.Insert
2.Delete
3.Search
4.Exit
Enter choice : 2
Deleted Element : 20
-->25-->50-->75-->80-->100
Do u want to continue(1/0)? : 1
1.Insert
2.Delete
3.Search
4.Exit
Enter choice : 2
Deleted Element : 25
-->50-->75-->80-->100
Do u want to continue(1/0)? : 1
1.Insert
2.Delete
3.Search
4.Exit
Enter choice : 2
Deleted Element : 50
-->75-->80-->100
Do u want to continue(1/0)? : 1
1.Insert
2.Delete
3.Search
4.Exit
Enter choice : 2
Deleted Element : 75
-->80-->100
Do u want to continue(1/0)? : 1
1.Insert
2.Delete
3.Search
4.Exit
Enter choice : 2
Deleted Element : 80
-->100
Do u want to continue(1/0)? : 1
1.Insert
2.Delete
3.Search
4.Exit
Enter choice : 2
Deleted Element : 100
Empty linked list
Do u want to continue(1/0)? :0
*/
No comments:
Post a Comment