#27 - Implement Queue using Array in java - Data Structure series - Part -3

preview_player
Показать описание
In this video , I have explained how to implement Queue using Array in java.

Array implementation of Queue is not dynamic in nature.
There are two most important operations of Queue:
enQueue: It is operation when we insert element into the queue.
deQueue: It is operation when we remove element from the queue.

~~~Subscribe to this channel, and press bell icon to get some interesting videos on Selenium and Automation:

Follow me on my Facebook Page:

Let's join our Automation community for some amazing knowledge sharing and group discussion on Telegram:

Paid courses (Recorded) videos:
Рекомендации по теме
Комментарии
Автор

I have seen your dsa videos(logics) i crack the software development engineer -1 Thank you Naveen sir.

triangularthoughts
Автор

Thanks Naveen, for this awesome content, can we have this series continues,
i have implemented queue using Node class, feels good.
Kudos to you :)

VipinDahiya
Автор

Hi Naveen, logic was correct the catch was in enqueue method you should modify
if(rear == capacity){
rear = 0;
}
it solves the array full size issue and no need to increase your capacity

also in dequeue method modify
if (front == capacity){

+ " removed from the queue");
front = 0;
}
this will solve deleting first element twice.

I became your fan and hats off to your dedication and teaching :)

sandeepis
Автор

Got clear concept and logic. Thanks Naveen

sandipvishwakarma
Автор

Please come LIVE. 🙏
Looking forward to see you! Have many doubts to clear from.
Informative video🙏
Thank you!

testinginsights-muktasharma
Автор

what is the use of setting rear=0 if(rear==capacity-1); please explain.

shadmairam
Автор

Hi Naveen, I guess the catch here is, both isEmpty() and isFull() methods are returning false because of which we dint see syso statements when the queue was full.

sravanthibana
Автор

It is a very cool tutorial, thank alot.

ehkalanephaung
Автор

Still there is an issue in your code. You are not operating to the full capacity. U r operating only till capacity-1.try to insert values till the capacity without dequeue operation and then print the array everytime, then you will find the issue. To rectify the issue pls compare if(front==capacity) in enqueue and if(rear==capacity) in dequeue.

sruthitenneti
Автор

public class Queue{
int capacity;
int arr[];
int front;
int rear;
int size;

public Queue(int size){
this.size = size;
arr = new int[size];
front = 0;
rear = - 1;
capacity = 0;
}

public void enqueue(int element){
if(isFull()){
System.out.println("Queue is full, cannot enqueue anymore");
}else{
if(rear == size - 1){
rear = 0;
}else{
rear++;
}
arr[rear] = element;
capacity++;
System.out.println("Inserted element : " + element);
}
}

public int dequeue() {
if(isEmpty()){
System.out.println("Queue is empty, cannot dequeue element.");
return -1;
}else{
if(front == size - 1){
int returnEle = arr[front];
System.out.println("Dequeued element is : " + returnEle);
front = 0;
capacity--;
return returnEle;
}else{
int returnEle = arr[front];
System.out.println("Dequeued element is : " + returnEle);
front ++;
capacity--;
return returnEle;
}
}
}

public boolean isEmpty(){
return (capacity == 0);
}

public boolean isFull(){
return (capacity == size);
}

public static void main(String[] args){
Queue q1 = new Queue(5);





q1.enqueue(10);
q1.enqueue(20);
q1.dequeue();

q1.enqueue(30);
q1.enqueue(40);
q1.enqueue(50);
q1.enqueue(60);
q1.dequeue();
q1.dequeue();
q1.dequeue();
q1.dequeue();
q1.dequeue();
q1.dequeue();
q1.enqueue(30);
q1.enqueue(40);
q1.enqueue(50);
q1.enqueue(60);
q1.enqueue(30);
q1.enqueue(40);
q1.enqueue(50);
q1.enqueue(60);
q1.dequeue();

}


}

DACSKishanKandu
Автор

Please explain the notations as well. Thanks for the teaching

sainathvallampatla
Автор

naveen isnt ur enqueue wrong??see the step where( rear==capacity-1) u arent putting any data just directly making it as rear=0 ; shouldnt u write queuearr[rear]=data and then make rear=0 like how u have done for dequeue

brandyplays
Автор

tried to iterate Queue using while loop and Iterator but it didn;'t worked. has anyone tried to iterate?

rahilkumar
Автор

Hi sir, I think You forgot the method of Full Queue

josephcarcedo
visit shbcf.ru