Write a Java program to accept names of n students and insert into LinkedList. Display the contents of list using Iterator and also Display the content in reverse order using ListIterator.
Write a Java program to accept names of n students and insert into LinkedList. Display the contents of list using Iterator and also Display the content in reverse order using ListIterator.
Program:
import java.io.*; import java.util.*; class ReverseLinkedList { public static void main(String args[]) throws IOException { LinkedList students = new LinkedList(); String str; int i, n; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter how many names:"); n = Integer.parseInt(br.readLine()); for(i=0;i<n;i++){ System.out.println("Enter the names of students:"); str = br.readLine(); students.add(str); } Iterator itr = students.iterator(); System.out.println("Content of LinkedList using Iterator"); while(itr.hasNext()){ System.out.println(itr.next()); } System.out.println("Content of LinkedList in reverse using ListIterator"); ListIterator litr = students.listIterator(); while(litr.hasNext()){ litr.next(); } while(litr.hasPrevious()){ System.out.println(litr.previous()); } } }
Comments
Post a Comment