Implement Queue Using Array - The Coding Shala

Home >> Data Structures >> Implement Queue Using Array

 In this post, we will learn how to Implement Queue Using Array and will write a Java program for the same.

Implement Queue Using Array

We will write a simple Java Program to implement queue using the array, Only Push and Pop method we will implement here.

Java Program: 

class MyQueue {

    int front, rear;
	int arr[] = new int[100005];

    MyQueue()
	{
		front=0;
		rear=0;
	}
	
	/* The method push to push element
	   into the queue */
	void push(int x)
	{
	    arr[rear] = x;
	    rear++;
	} 

    /* The method pop which return the 
       element poped out of the queue*/
	int pop()
	{
		if(front == rear) return -1;
		int res = arr[front];
		front++;
		return res;
	} 
}


Other Posts You May Like
Please leave a comment below if you like this post or found some errors, it will help me to improve my content.

Comments

Popular Posts from this Blog

Anti Diagonals - The Coding Shala

Shell Script to find sum, product and average of given numbers - The Coding Shala

LeetCode - Bulb Switcher Solution - The Coding Shala

New Year Chaos Solution - The Coding Shala

Sorting the Sentence LeetCode Solution - The Coding Shala