Write a JAVA program to accept names of n students and insert into LinkedLlist i) Display the contents of list using Iterator. ii) Display the content in reverse order. (using ListIterator)

//roll no:-5211
//slp no:-28
import java.util.*;
import java.io.*;
class slip28
{
public static void main(String args[])
{
String str;
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
LinkedList a=new LinkedList();
System.out.println("Enter a no of String you want:");
int n=Integer.parseInt(br.readLine());
for(int i=0;i<n;i++)
{
 System.out.println("Enter string:");
str=br.readLine();
a.add(str);
}
 System.out.println("Display LinkedList:");
Iterator iter=a.iterator();
while(iter.hasNext())
{
System.out.println(iter.next());
}
Collections.reverse(a);
System.out.println("Reversing LinkedList containtns:"+a);
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Error");
}
}
}

/* *****output******
F:\5211>javac slip28.java
Note: slip28.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

F:\5211>java slip28
Enter a no of String you want:
2
Enter string:

Rupesh
Enter string:
Rohan
Display LinkedList:
Rupesh

Rohan
Reversing LinkedList containtns:[Rohan,Rupesh]
*/

No comments:

Post a Comment