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 ListIterator Interface.



import java.util.LinkedList;

import java.util.ListIterator;

import java.util.Scanner;


public class EmployeeListIterator {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        LinkedList<String> employeeNames = new LinkedList<>();


        // Accepting the number of employees

        System.out.print("Enter the number of employees: ");

        int n = scanner.nextInt();

        scanner.nextLine(); 

        // Taking input from user and storing in LinkedList

        System.out.println("Enter " + n + " employee names:");

        for (int i = 0; i < n; i++) {

         //   System.out.print("Enter name " + (i + 1) + ": ");

            employeeNames.add(scanner.nextLine());

        }


        // Displaying employee names using ListIterator

        System.out.println("\nEmployee names using ListIterator:");

        ListIterator<String> listIterator = employeeNames.listIterator();

        

        // Traverse in forward direction

        while (listIterator.hasNext()) {

            System.out.println(listIterator.next());

        }


     while (listIterator.hasPrevious()) {

            System.out.println(listIterator.previous());

        }


        scanner.close();

    }

}