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

Поделиться
HTML-код
  • Опубликовано: 22 окт 2024

Комментарии • 19

  • @triangularthoughts
    @triangularthoughts 2 года назад +2

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

  • @testinginsights-muktasharma
    @testinginsights-muktasharma 4 года назад +1

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

  • @VipinDahiya89
    @VipinDahiya89 Год назад

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

  • @sandipvishwakarma8474
    @sandipvishwakarma8474 4 года назад +2

    Got clear concept and logic. Thanks Naveen

  • @sandeepis2874
    @sandeepis2874 3 года назад

    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 :)

  • @ehkalanephaung4659
    @ehkalanephaung4659 2 года назад

    It is a very cool tutorial, thank alot.

  • @sruthitenneti3821
    @sruthitenneti3821 3 года назад +2

    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.

  • @SarangHoley
    @SarangHoley 4 года назад +1

    Thanks for the video 👍😊

  • @sravanthibana728
    @sravanthibana728 3 года назад +1

    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.

  • @shadmairam2549
    @shadmairam2549 4 года назад +2

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

    • @mayankvarshney6014
      @mayankvarshney6014 3 года назад

      (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.

  • @brandyplays6134
    @brandyplays6134 4 года назад +2

    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

  • @goodnewsukaegbu8452
    @goodnewsukaegbu8452 2 года назад

    nice

  • @sainathvallampatla5135
    @sainathvallampatla5135 4 года назад

    Please explain the notations as well. Thanks for the teaching

  • @jaspalsingh-mv5xh
    @jaspalsingh-mv5xh 3 года назад

    Nice

  • @goldfan5523
    @goldfan5523 4 года назад

    Sir, capacity is 5..right?

  • @josephcarcedo83
    @josephcarcedo83 2 года назад

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

  • @rahilkumar3558
    @rahilkumar3558 3 года назад

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

  • @DACSKishanKandu
    @DACSKishanKandu 3 года назад

    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();
    }

    }