#Queues

1 messages · Page 1 of 1 (latest)

slim venture
#

My implementation:

public class Queue {
   private int capacity;
   private int currentSize;
   private int[] arr;
   
   public Queue(int capacity) {
      this.capacity = capacity;
      this.arr = new int[capacity];
      this.currentSize = -1;
   }
   
   public int size() {
      return currentSize + 1;
   }
   
   public boolean isEmpty() {
      return currentSize == -1;
   }
   
   public boolean isFull() {
      return currentSize == capacity - 1;
   }
   
   public void enqueue(int data) {
      if(isFull()) {
         System.out.println("Queue is full!");
      } else {
         arr[++currentSize] = data;
      }
   }
   
   public int dequeue() {
      int temp = arr[0];
      if(!isEmpty()) {
         for(int i = 0; i < size()-1; i++) {
            arr[i] = arr[i+1];
         }
         currentSize--;
      }
      return temp;
   }
//...

Other implementation:

public class Queue {
    private int front, rear, size;
    private int capacity;
    private int[] array;

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

    // Method to add an element to the queue
    public void enqueue(int item) {
        if (isFull()) {
            System.out.println("Queue is full, cannot enqueue");
            return;
        }
        this.rear = (this.rear + 1) % this.capacity;
        this.array[this.rear] = item;
        this.size++;
        System.out.println(item + " enqueued to queue");
    }

    // Method to remove an element from the queue
    public int dequeue() {
        if (isEmpty()) {
            System.out.println("Queue is empty, cannot dequeue");
            return Integer.MIN_VALUE;
        }
        int item = this.array[this.front];
        this.front = (this.front + 1) % this.capacity;
        this.size--;
        return item;
    }
//...
golden hazelBOT
#

<@&987246883653156906> please have a look, thanks.

golden hazelBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

this.currentSize = 0;
this.arr = new int[capacity];
}

public void enqueue(int item) {
if (currentSize == capacity) {
System.out.println("Queue is full. Cannot enqueue item.");
return;
}

  arr[currentSize] = item;
  currentSize++;

}

public int dequeue() {
if (currentSize == 0) {
System.out.println("Queue is empty. Cannot dequeue item.");
return -1;
}

  int item = arr[0];
  
  for (int i = 0; i < currentSize - 1; i++) {
     arr[i] = arr[i+1];
  }
  
  currentSize--;
  
  return item;

}

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

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


Test:
```java
public class Main {
   public static void main(String[] args) {
       Queue queue = new Queue(5);
       
       queue.enqueue(1);
       queue.enqueue(2);
       queue.enqueue(3);
       
       System.out.println(queue.dequeue()); // Output: 1
       
       queue.enqueue(4);
       queue.enqueue(5);
       
       System.out.println(queue.isFull()); // Output: true
       
       while (!queue.isEmpty()) {
           System.out.println(queue.dequeue());
       }
       
       System.out.println(queue.isEmpty()); // Output: true
   }
}

This implementation of a queue uses an array to store the elements. The enqueue method adds an element to the end of the array, and the dequeue method removes and returns the first element in the array. The isEmpty method checks if the queue is empty, and the isFull method checks if the queue is full. The capacity of the queue is set in the constructor.

slim venture
grim tree
grim tree
# slim venture Is my queue implementation effective? I often see other implementations like the...

Is my queue implementation effective? I often see other implementations like the second block of code I sent, but it seems confusing to me.
As you may have seen in the second implementation, there is no loop, dequeue doesn't do any loop and so is way faster than yours

Should I stick to my own implementation, or is it better to follow the second block of code?
Wdym, why can't you use java Deque?

slim venture
lean flume
#

There's a built in Deque interface with implementations like ArrayDeque and LinkedList

grim tree
slim venture
jolly snow
jolly snow
slim venture
# jolly snow It's good to implement it if you want to learn about it but in real projects jus...
class Queue
{
  private int maxSize;
  private long[] queArray;
  private int front;
  private int rear;
  private int nItems;
  
  public Queue(int s) // constructor
  {
    maxSize = s;
    queArray = new long[maxSize];
    front = 0;
    rear = -1;
    nItems = 0;
  }
  
  public void insert(long j) // put item at rear of queue
  {
    if(rear == maxSize-1) // deal with wraparound
      rear = -1;
    queArray[++rear] = j; // increment rear and insert
    nItems++; // one more item
  }
  
  public long remove() // take item from front of queue
  {
    long temp = queArray[front++]; // get value and incr front
    if(front == maxSize) // deal with wraparound
      front = 0;
    nItems--; // one less item
    return temp;
  }
//...

How about this? Do you think this is more effective than the 2nd block of code I sent before this?

golden hazelBOT
grim tree
bold pilot
jolly snow
grim tree