
write a java pogram to add elements to linked list using keyboard and display it

Hi Friend,
Try this:
import java.util.*;
class LinkedListExample
{
public static void main(String[] args)
{
LinkedList<String> list=new LinkedList<String>();
Scanner input=new Scanner(System.in);
System.out.print("how many elemenst do you want to add: ");
int num=input.nextInt();
for(int i=0;i<num;i++){
System.out.print("Add Element: ");
String element=input.next();
list.add(element);
}
System.out.println();
System.out.println("LinkedList elements are: ");
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
}
}
Thanks
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.