Examples
package com.w3schools;
public class Test {
private int capacity;
int queueArray[];
int front = 0;
int rear = -1;
int currentSize = 0;
public Test(int queueSize){
this.capacity = queueSize;
queueArray = new int[this.capacity];
}
/**
* Adds element at the end of the queue.
* @param item
*/
public void enqueue(int item) {
if (isQueueFull()) {
System.out.println("Overflow state.");
} else {
rear++;
if(rear == capacity-1){
rear = 0;
}
queueArray[rear] = item;
currentSize++;
System.out.println("Element " + item+ " is pushed to Queue.");
}
}
/**
* Removes an element from the top of the queue
*/
public void dequeue() {
if (isQueueEmpty()) {
System.out.println("Underflow state.");
} else {
front++;
if(front == capacity-1){
System.out.println("Removed element: "+queueArray[front-1]);
front = 0;
} else {
System.out.println("Removed element: "+queueArray[front-1]);
}
currentSize--;
}
}
/**
* Checks whether the queue is full or not
* @return boolean
*/
public boolean isQueueFull(){
boolean status = false;
if (currentSize == capacity){
status = true;
}
return status;
}
/**
* Checks whether the queue is empty or not
* @return
*/
public boolean isQueueEmpty(){
boolean status = false;
if (currentSize == 0){
status = true;
}
return status;
}
public static void main(String args[]){
try {
Test queue = new Test(4);
queue.enqueue(41);
queue.dequeue();
queue.enqueue(6);
queue.enqueue(24);
queue.enqueue(7);
queue.dequeue();
queue.dequeue();
queue.enqueue(4);
queue.dequeue();
queue.enqueue(918);
} catch (Exception e) {
e.printStackTrace();
}
}
} |
package com.w3schools;
public class Test {
private int capacity;
int queueArray[];
int front = 0;
int rear = -1;
int currentSize = 0;
public Test(int queueSize){
this.capacity = queueSize;
queueArray = new int[this.capacity];
}
/**
* Adds element at the end of the queue.
* @param item
*/
public void enqueue(int item) {
if (isQueueFull()) {
System.out.println("Overflow state.");
} else {
rear++;
if(rear == capacity-1){
rear = 0;
}
queueArray[rear] = item;
currentSize++;
System.out.println("Element " + item+ " is pushed to Queue.");
}
}
/**
* Removes an element from the top of the queue
*/
public void dequeue() {
if (isQueueEmpty()) {
System.out.println("Underflow state.");
} else {
front++;
if(front == capacity-1){
System.out.println("Removed element: "+queueArray[front-1]);
front = 0;
} else {
System.out.println("Removed element: "+queueArray[front-1]);
}
currentSize--;
}
}
/**
* Checks whether the queue is full or not
* @return boolean
*/
public boolean isQueueFull(){
boolean status = false;
if (currentSize == capacity){
status = true;
}
return status;
}
/**
* Checks whether the queue is empty or not
* @return
*/
public boolean isQueueEmpty(){
boolean status = false;
if (currentSize == 0){
status = true;
}
return status;
}
public static void main(String args[]){
try {
Test queue = new Test(4);
queue.enqueue(41);
queue.dequeue();
queue.enqueue(6);
queue.enqueue(24);
queue.enqueue(7);
queue.dequeue();
queue.dequeue();
queue.enqueue(4);
queue.dequeue();
queue.enqueue(918);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output
Element 41 is pushed to Queue.
Removed element: 41
Element 6 is pushed to Queue.
Element 24 is pushed to Queue.
Element 7 is pushed to Queue.
Removed element: 6
Removed element: 24
Element 4 is pushed to Queue.
Removed element: 7
Element 918 is pushed to Queue. |
Element 41 is pushed to Queue.
Removed element: 41
Element 6 is pushed to Queue.
Element 24 is pushed to Queue.
Element 7 is pushed to Queue.
Removed element: 6
Removed element: 24
Element 4 is pushed to Queue.
Removed element: 7
Element 918 is pushed to Queue.