Look a hundred times beauty, beauty is not necessarily yours. But if you run the algorithm a hundred times, the knowledge is yours

Who can have nine floors? No need to get up!

Title address

The title

Design your loop queue implementation. A circular queue is a linear data structure that operates on a FIFO (first-in, first-out) basis and the tail of the queue is connected behind the head of the queue to form a loop. It is also known as a “ring buffer”.

One of the nice things about a circular queue is that we can use the space that the queue has previously used. In a normal queue, once a queue is full, we cannot insert the next element, even though there is still space in front of the queue. But with circular queues, we can use that space to store new values.

Your implementation should support the following:

  • MyCircularQueue(k)Constructor that sets the queue length tok.
  • Front: Get elements from team leader. If the queue is empty, return- 1.
  • Rear: Gets the last element of the queue. If the queue is empty, return- 1.
  • enQueue(value): Inserts an element into the loop queue. Return true on successful insertion.
  • deQueue(): Removes an element from the loop queue. Return true if deleted successfully.
  • isEmpty(): Checks whether the loop queue is empty.
  • isFull(): Checks whether the loop queue is full.

Example:

MyCircularQueue circularQueue = new MyCircularQueue(3); Circularqueue.enqueue (1); // Set the length to 3 circularqueue.enqueue (1); Circularqueue.enqueue (2); Circularqueue.enqueue (3); Circularqueue.enqueue (4); // Return false, the queue is full circularqueue.rear (); Circularqueue.isfull (); Circularqueue.dequeue (); Circularqueue.enqueue (4); // Return true circularqueue.rear (); / / return 4Copy the code

prompt

  • All the values are there0to1000To the extent of;
  • The operands will be in1to1000To the extent of;
  • Do not use the built-in queue library.

Their thinking

  • We define an array if the length of the array is the same as the required queue lengthisFull
  • If the length of the array is zero0, then meetisEmpty
  • enQueueIt’s just going into an arraypushThe element
  • deQueueIt’s just removing the first element of the array
  • RearGet the last item, which is the first item in the arraylength-1item

The problem solving code

/ * * *@param {number} k* /
var MyCircularQueue = function(k) {
    this.maxLen = k
    this.arr = []
};

/ * * *@param {number} value
 * @return {boolean}* /
MyCircularQueue.prototype.enQueue = function(value) {
    if(this.isFull()) return false
    this.arr.push(value)
    return true
};

/ * * *@return {boolean}* /
MyCircularQueue.prototype.deQueue = function() {
    if(this.isEmpty()) return false
    let [a,...args] = this.arr
    this.arr = [...args]
    return true
};

/ * * *@return {number}* /
MyCircularQueue.prototype.Front = function() {
    if(!this.arr.length) return -1
    return this.arr[0]};/ * * *@return {number}* /
MyCircularQueue.prototype.Rear = function() {
    if(!this.arr.length) return -1
    return this.arr[this.arr.length-1]};/ * * *@return {boolean}* /
MyCircularQueue.prototype.isEmpty = function() {
    return this.arr.length ==0
};

/ * * *@return {boolean}* /
MyCircularQueue.prototype.isFull = function() {
    return this.arr.length ==this.maxLen
};

/** * Your MyCircularQueue object will be instantiated and called as such: * var obj = new MyCircularQueue(k) * var param_1 = obj.enQueue(value) * var param_2 = obj.deQueue() * var param_3 = obj.Front() * var param_4 = obj.Rear() * var param_5 = obj.isEmpty() * var param_6 = obj.isFull() */
Copy the code

If you have any questions or suggestions, please leave a comment!