Java Queue

  1. Integer Queue - Insert & Delete in Java
  2. Float Queue - Insert & Delete in Java
  3. Double Queue - Insert & Delete in Java
  4. Boolean Queue - Insert & Delete in Java
  5. String Queue - Insert & Delete in Java
  6. Peek head of the Queue in Java

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Integer Queue - Insert & Delete in Java


Queue_Ex1.java

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
 
public class Queue_Ex1 {
	
	Scanner scan;
	Queue<Integer> queue;
	
	int n;
	
	void insert() {
		
		scan = new Scanner(System.in);
		queue = new LinkedList<Integer>();
		
		System.out.println("Integer Queue - Insert & Delete");
		
		System.out.println("\nEnter 'n' value :");
		n = scan.nextInt();
		
		System.out.println("Enter the elements");
		
		for(int i=0; i<n; i++) {
			
			queue.add(scan.nextInt());
		}		
	}
	
	void delete() {
		
		System.out.println("\nThe Queue");
		
		while(!queue.isEmpty()) {
			
			System.out.println(queue.poll());
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Queue_Ex1 obj = new Queue_Ex1();
		
		obj.insert();
		obj.delete();;
	}
}

Sample Output

Integer Queue - Insert & Delete

Enter 'n' value :
3
Enter the elements
3
6
9

The Queue
3
6
9




B. Float Queue - Insert & Delete in Java


Queue_Ex2.java

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
 
public class Queue_Ex2 {
	
	Scanner scan;
	Queue<Float> queue;
	
	int n;
	
	void insert() {
		
		scan = new Scanner(System.in);
		queue = new LinkedList<Float>();
		
		System.out.println("Float Queue - Insert & Delete");
		
		System.out.println("\nEnter 'n' value :");
		n = scan.nextInt();
		
		System.out.println("Enter the elements");
		
		for(int i=0; i<n; i++) {
			
			queue.add(scan.nextFloat());
		}		
	}
	
	void delete() {
		
		System.out.println("\nThe Queue");
		
		while(!queue.isEmpty()) {
			
			System.out.println(queue.poll());
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Queue_Ex2 obj = new Queue_Ex2();
		
		obj.insert();
		obj.delete();;
	}
}

Sample Output

Float Queue - Insert & Delete

Enter 'n' value :
3
Enter the elements
23.45
12.36
59.642

The Queue
23.45
12.36
59.642




C. Double Queue - Insert & Delete in Java


Queue_Ex3.java

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
 
public class Queue_Ex3 {
	
	Scanner scan;
	Queue<Double> queue;
	
	int n;
	
	void insert() {
		
		scan = new Scanner(System.in);
		queue = new LinkedList<Double>();
		
		System.out.println("Double Queue - Insert & Delete");
		
		System.out.println("\nEnter 'n' value :");
		n = scan.nextInt();
		
		System.out.println("Enter the elements");
		
		for(int i=0; i<n; i++) {
			
			queue.add(scan.nextDouble());
		}		
	}
	
	void delete() {
		
		System.out.println("\nThe Queue");
		
		while(!queue.isEmpty()) {
			
			System.out.println(queue.poll());
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Queue_Ex3 obj = new Queue_Ex3();
		
		obj.insert();
		obj.delete();;
	}
}

Sample Output

Double Queue - Insert & Delete

Enter 'n' value :
3
Enter the elements
1.23456
2.123456
3.123

The Queue
1.23456
2.123456
3.123




D. Boolean Queue - Insert & Delete in Java


Queue_Ex4.java

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
 
public class Queue_Ex4 {
	
	Scanner scan;
	Queue<Boolean> queue;
	
	int n;
	
	void insert() {
		
		scan = new Scanner(System.in);
		queue = new LinkedList<Boolean>();
		
		System.out.println("Boolean Queue - Insert & Delete");
		
		System.out.println("\nEnter 'n' value :");
		n = scan.nextInt();
		
		System.out.println("Enter the elements");
		
		for(int i=0; i<n; i++) {
			
			queue.add(scan.nextBoolean());
		}		
	}
	
	void delete() {
		
		System.out.println("\nThe Queue");
		
		while(!queue.isEmpty()) {
			
			System.out.println(queue.poll());
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Queue_Ex4 obj = new Queue_Ex4();
		
		obj.insert();
		obj.delete();;
	}
}

Sample Output

Boolean Queue - Insert & Delete

Enter 'n' value :
3
Enter the elements
true
FaLSe
TRuE

The Queue
true
false
true




E. String Queue - Insert & Delete in Java


Queue_Ex5.java

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
 
public class Queue_Ex5 {
	
	Scanner scan;
	Queue<String> queue;
	
	int n;
	
	void insert() {
		
		scan = new Scanner(System.in);
		queue = new LinkedList<String>();
		
		System.out.println("String Queue - Insert & Delete");
		
		System.out.println("\nEnter 'n' value :");
		n = scan.nextInt();
		
		System.out.println("Enter the elements");
		
		for(int i=0; i<n; i++) {
			
			queue.add(scan.next());
		}		
	}
	
	void delete() {
		
		System.out.println("\nThe Queue");
 
		while(!queue.isEmpty()) {
			
			System.out.println(queue.poll());
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Queue_Ex5 obj = new Queue_Ex5();
		
		obj.insert();
		obj.delete();;
	}
}

Sample Output

String Queue - Insert & Delete

Enter 'n' value :
3
Enter the elements
aaa
bbb
ccc

The Queue
aaa
bbb
ccc




F. Peek head of the Queue in Java


Queue_Ex6.java

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
 
public class Queue_Ex6 {
	
	Scanner scan;
	Queue<String> queue;
	
	int n;
	
	void insert() {
		
		scan = new Scanner(System.in);
		queue = new LinkedList<String>();
		
		System.out.println("Peek head of the Queue");
		
		System.out.println("\nEnter 'n' value :");
		n = scan.nextInt();
		
		System.out.println("Enter the elements");
		
		for(int i=0; i<n; i++) {
			
			queue.add(scan.next());
		}		
	}
	
	void peek() {
		
		System.out.println("\nThe head of this queue, " +
				"or null if this queue is empty");
			
		System.out.println(queue.peek());
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Queue_Ex6 obj = new Queue_Ex6();
		
		obj.insert();
		obj.peek();;
	}
}

Sample Output

Peek head of the Queue

Enter 'n' value :
3
Enter the elements
aaa
bbb
ccc

The head of this queue, or null if this queue is empty
aaa
Peek head of the Queue

Enter 'n' value :
0
Enter the elements

The head of this queue, or null if this queue is empty
null






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com