Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Implement the Queue in Java 
 

In this section, you will learn how to implement the queue.

 

Implement the Queue in Java

                         

In this section, you will learn how to implement the queue. 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.

The given program implements a queue. 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. Some methods and APIs are explained below which have been used in the program for the certain purposes: 

LinkedList<Integer>():
This 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 types 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():
Above method removes and returns the first element of the list.

removeLast():
Above method removes and returns the last element of the list.

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

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

Here is the code of program:

import java.io.*;
import java.util.*;

public class QueueImplement{
  LinkedList<Integer> list;
  String str;
  int num;
  public static void main(String[] args){
    QueueImplement q = new QueueImplement();
  }
  public QueueImplement(){
    try{
      list = new LinkedList<Integer>();
      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);
    }
  }
}

Download this example

                         

» View all related tutorials
Related Tags: c reference io types user type system this node create to ref e des section nodes demo use pe ce

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

2 comments so far (
post your own) View All Comments Latest 10 Comments:

Why theirs no output in the saple program of Queue?

Posted by Shieryl on Tuesday, 09.9.08 @ 08:04am | #78170

My name is prathima. this is a very good site which is helpful for java beginers . why dont u place a complete project code in which all hte topics r covered . by this one can understan how where and when to use particular topics .if u add some comments to explain the code it will be the best source to understand java practically .

Posted by prathima on Friday, 07.6.07 @ 22:38pm | #20855

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.