Monday, 10 March 2025

Write a java program to accept n employee names from user, store them into the LinkedList class and display them by using. Iterator Interface.

 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.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();    }}