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){ System.out.println(queueArr[front-1] + " 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 :)
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.
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.
(rear== capacity-1 ) is not the correct logic. It replaces the front element when when you add the last element in the Queue, before anything is de queued from the Queue.
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
I have seen your dsa videos(logics) i crack the software development engineer -1 Thank you Naveen sir.
Please come LIVE. 🙏
Looking forward to see you! Have many doubts to clear from.
Informative video🙏
Thank you!
Thanks Naveen, for this awesome content, can we have this series continues,
i have implemented queue using Node class, feels good.
Kudos to you :)
Got clear concept and logic. Thanks Naveen
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){
System.out.println(queueArr[front-1] + " 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 :)
It is a very cool tutorial, thank alot.
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.
Thanks for the video 👍😊
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.
what is the use of setting rear=0 if(rear==capacity-1); please explain.
(rear== capacity-1 ) is not the correct logic. It replaces the front element when when you add the last element in the Queue, before anything is de queued from the Queue.
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
nice
Please explain the notations as well. Thanks for the teaching
Nice
Sir, capacity is 5..right?
Hi sir, I think You forgot the method of Full Queue
tried to iterate Queue using while loop and Iterator but it didn;'t worked. has anyone tried to iterate?
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);
System.out.println(q1.isFull());
System.out.println(q1.isEmpty());
System.out.println("--------------------");
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();
}
}