Java Programing laungage

java.util Projects

java.util Project 1

Queue Implement

In this programming tutorials, we shall be learn how to implement the queue. A queue holds a collection of data or elements in form of the FIFO ( First In First Out) rule. The FIFO that means which data added first in the list, only that element can be retrieved or removed first from the list.

Previous Home Next
adplus-dvertising

In the sense, we can perform any operation on that data which is add at first in the list. Whenever you need to remove the last added element then you must remove all these elements which are entered before the certain element.In this program uses many methods which is listed bellow:

LinkedList<Integer>(): It is a constructor of the LinkedList class. This class is used by importing the java.util.*; package. This constructor is used for constructing an empty list. It can contain integer types data in the given program because in the declaration of the LinkedList class type checking has been used. The LinkeedList class provides inserting and deleting the data to/from the list.

removeFirst(): This method is used to remove and return the first element of the list.

removeLast(): This method is used to remove and return the last element of the list.

list.isEmpty(): This method is used to checks whether the list is empty or not.

remove(): This method is used to remove the elements in the list in a specified sequence.

Example


package r4r;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class queuetests {
LinkedList<Integer> lst;
String str;
int num;
public static void main(String[] args){
queuetests q = new queuetests();
}
public queuetests(){ try{ lst = new LinkedList<Integer>(); InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); System.out.println("Enter No. of elements : "); str = br.readLine(); if((num = Integer.parseInt(str)) == 0){ System.out.println("You have entered either zero/null."); System.exit(0); } else{ System.out.println("Enter the elements : "); for(int i = 0; i < num; i++){ str = br.readLine(); int n = Integer.parseInt(str); lst.add(n); } } System.out.println("First element is:" + lst.removeFirst()); System.out.println("Last element is:" + lst.removeLast()); System.out.println("Rest elements in the list :"); while(!lst.isEmpty()){ System.out.print(lst.remove() + "\t"); } } catch(IOException e){ System.out.println(e.getMessage() + " is not a right entry."); System.exit(0); } } }
Previous Home Next