© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
Quick Links
Easy Studies

Queue Implementation Using Java


A queue holds a collection of data or elements and follows the FIFO (First in First Out) rule. The FIFO that means which data added first in the list, only that element can be removed or retrieved first from the list. In other sense, you can remove or perform operation on that data which had been added first in the Collection (list). Whenever you need to remove the last added element then you must remove all these elements which are entered before the certain element. It takes all elements as input by user. These values are added to the list and shows first, last and the rest elements present in the list separately.

LinkedList () is the 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 type data in the given program because in the declaration of the LinkedList class type checking has been used. This is an implementation of the List interface of the Collections Framework. The LinkeedList class provides inserting and deleting the data to/from the list. removeFirst () method removes and returns the first element of the list.removeLast () method removes and returns the last element of the list. list.isEmpty () method checks whether the list is empty or not. remove () method used to remove the elements in the list in a specified sequence.

Queue Implementation Using Java:

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

Sample ScreenShot:

SLogix Student Projects

⇓Student Projects⇓
⇑Student Projects⇑