Implement the Queue in Java

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

Ads

Tutorials   
How to read properties file in Java? Generate random numbers in Java Cloneable Interface in Java What is the flaw with the Stack class? List In Java Java Stack Example HashSet In Java Enumeration in java How to generate random number in java Iterator in java Queue in java Transient Java Keyword clone method in Java Java Queue example Logger in Java Java current date - How to get current date in Java Collection : LinkedList Example Collection : ArrayList Example Random alpha numeric string of fixed size Java List Iterator Example The Hashtable Class The BitSet class Java Vector Java Enumeration Data Structures in Java Java Transient Variable Java Date Format Example Java Properties File Example Map.Entry Java Interface Java Property File java preferences,Change the value of Preferences by user Retrieving a Preference Node Java hasNext Write Keys and Values to the Properties file in Java Get All Keys and Values of the Properties files in Java Properties file in Java Properties file in Java Determining If a Preference Node Contains a Specific Value Change the value of Preferences by user Clone method example in Java Implementing a Simple Event Notifier Determining When a Preference Node Is Added or Removed Listening for Changes to Preference Values in a Preference Node Importing Preferences Finding a Preference in a Preference Tree Retrieving the Parent and Child Nodes of a Preference Node Determining If a Preference Node Exists Removing a Preference Node Creating a Preference Node Getting the Maximum Size of a Preference Key and Value
Ads

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

Advertisements

Ads
Share on Google+Share on Google+

Implement the Queue in Java

Posted on: April 16, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Implement the Queue in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:6
Prasad
April 21, 2011
java

I want ask questions for java
Prasad
April 21, 2011
java

I like this
laks
April 15, 2012
waste fellow

is this program for queue?
amila
May 30, 2012
queue

this programe have lot of errors......
may zhu
October 10, 2012
java212, implement the queue in java

Implement a class Queue which supports the following operations: boolean isEmpty() Returns true if the queue is empty, and false otherwise. int peak() Returns the value at the front of a non-empty queue without deleting it. int pop() Removes the item at the front of a non-empty queue and returns it. void push(int o) Inserts the argument o at the back of the queue. int size() Returns the total number of elements in the queue. You can use an array as your basic data structure and set its size to 100, i.e. assuming that the user will never enter more than 100 items. Your not allowed to move around any elements inside the array. Instead use two integers front, size which indicates the front and length of the queue. Then wrap around at the end of the array when accessing elements, using the mod operator. please help me to do this
anbu
July 4, 2013
feedback

it have some errors

Ads

 

Ads