Write a Java program to read n integers into LinkedList collection. Do the following operations. Display only negative integers and delete last element.
Write a Java program to read n integers into LinkedList collection. Do the following operations. Display only negative integers and delete last element.
Program:
import java.io.*; import java.util.*; class LinkedListInt { public static void main(String args[]) throws IOException { LinkedList list = new LinkedList(); int i,n,x; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("How many integers:"); n = Integer.parseInt(br.readLine()); for(i=0;i<n;i++){ System.out.println("Enter Number:"); x = Integer.parseInt(br.readLine()); list.add(x); } Iterator itr = list.iterator(); while(itr.hasNext()){ Integer y = (Integer)itr.next(); int a = (int)y; if(a<0){ System.out.println("Negative number:"+a); } } System.out.println("List before removing last element:"+list); list.removeLast(); System.out.println("After removing:"+list); } }
Comments
Post a Comment