Many of Us feel that
one can't implement Link List in Java since Pointer is not available...
But its not true...check the below program... :) .... It will change
your concept about Objects in Java ....
class
LinkList{
int data;
LinkList next=null;
static int count=0;
static LinkList head;
void add(int item){
if(head==null){
head=
new LinkList();
head.data
= item;
head.next=null;
count++;
}
else{
LinkList
t=head;
while(t.next!=null){
t=t.next;
}
t.next
= new LinkList();
t.next.data
= item;
t.next.next=null;
count++;
}
}
void display(){
LinkList t=head;
if(t==null){
System.out.println("List
Empty");
return;
}
while(t!=null){
System.out.println(" "+t.data);
t=t.next;
}
}
void menu()
{
System.out.print("\n
: Link List Using Java :\n 1.ADD, 2.Display and 3.Exit");
while(true){
java.util.Scanner
sc = new java.util.Scanner(System.in);
System.out.print("\nEnter
Your Choice:");
int
c = sc.nextInt();
switch(c){
case
1 :
System.out.print("\nEnter
Number to insert in LL.:");
c
= sc.nextInt();
add(c);
break;
case
2 :
display();
break;
case
3 :
System.out.print("\nThank
You :) .. ");
return;
}
}
}
}
class
Program{
public static void main(String args[]){
LinkList l=new
LinkList();
l.menu();
}
}
No comments:
Post a Comment