[Leetcode] design circular queue design ring queue

Source: Internet
Author: User

 

Design your implementation of the Circular queue. the circular queue is a linear data structure in which the operations are performed med Based on FIFO (first in first out) principle and the last position is connected back to the first position to make a circle. it is also called "Ring buffer ".

One of the benefits of the circular queue is that we can make use of the spaces in front of the queue. in a normal queue, once the queue becomes full, we cannot insert the next element even if there is a space in front of the queue. but using the circular queue, we can use the space to store new values.

Your implementation shocould support following operations:

  • MyCircularQueue(k): Constructor, set the size of the queue to be K.
  • Front: Get the front item from the queue. If the queue is empty, return-1.
  • Rear: Get the last item from the queue. If the queue is empty, return-1.
  • enQueue(value): Insert an element into the circular queue. Return true if the operation is successful.
  • deQueue(): Delete an element from the circular queue. Return true if the operation is successful.
  • isEmpty(): Checks whether the circular queue is empty or not.
  • isFull(): Checks whether the circular queue is full or not.

 

Example:

MyCircularQueue circularQueue = new MyCircularQueue(3); // set the size to be 3circularQueue.enQueue(1);  // return truecircularQueue.enQueue(2);  // return truecircularQueue.enQueue(3);  // return truecircularQueue.enQueue(4);  // return false, the queue is fullcircularQueue.Rear();  // return 3circularQueue.isFull();  // return truecircularQueue.deQueue();  // return truecircularQueue.enQueue(4);  // return truecircularQueue.Rear();  // return 4

 

Note:

  • All values will be in the range of [0, 1000].
  • The number of operations will be in the range of [1, 1000].
  • Please do not use the built-in queue library.

 

S

 

[Leetcode] design circular queue design ring queue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.