import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Scanner;
class EmployeeListIterator
{
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(); // clear buffer
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:");
ListIterator<String> itr = list.listIterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
sc.close();
}
}
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();
}
}




