Write a java program to accept n employee names from user, store them into the LinkedList class and display them by using. Iterator Interface.
import java.util.LinkedList;
import java.util.Iterator;
import java.util.Scanner;
class EmployeeLinkedList
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
LinkedList<String> list = new LinkedList<>();
System.out.print("Enter number of employees: ");
int n = sc.nextInt();
sc.nextLine();
for(int i = 0; i < n; i++)
{
System.out.print("Enter employee name: ");
String name = sc.nextLine();
list.add(name);
}
System.out.println("Employee Names:");
Iterator<String> itr = list.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
sc.close();
}
}
Second Method
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Iterator;
public class EmployeeList {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
LinkedList<String> employees = new LinkedList<>();
// Get the number of employees
System.out.print("Enter number of employees: ");
int n = scanner.nextInt();
scanner.nextLine();
// Get employee names
System.out.println("Enter employee names:");
for (int i = 0; i < n; i++) {
// System.out.print("Employee " + (i + 1) + ": ");
employees.add(scanner.nextLine());
}
// Display names using Iterator
System.out.println("\nEmployee Names:");
Iterator<String> it = employees.iterator();
while (it.hasNext()) {
System.out.println(it.next());
} scanner.close(); }}




