Design Circular Queue

Design your implementation of the circular queue. The circular queue is a linear data structure in which the operations are performed 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 should 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 3
circularQueue.enQueue(1);  // return true
circularQueue.enQueue(2);  // return true
circularQueue.enQueue(3);  // return true
circularQueue.enQueue(4);  // return false, the queue is full
circularQueue.Rear();  // return 3
circularQueue.isFull();  // return true
circularQueue.deQueue();  // return true
circularQueue.enQueue(4);  // return true
circularQueue.Rear();  // return 4

算法就是:用front 和end分别表示头和尾,最重要的考点就是用size来表示里面有多少个元素。这样 front和rear只需要一直向前走就可以了。

每次enqueue和dequeue的时候,都用size,也就是count来进行判断,这样就省去了用双指针来计算空间的问题。

每次front和end都应该指向下一个,然后需要

front = (front+1)%qeueue.length 和end = (end+1)%qeueue.length一下。

注意: end init值为-1,是为了第一次进去,是从0开始的;

class MyCircularQueue {
    public int front;
    public int end;
    public int size;
    public int[] array;
    public MyCircularQueue(int k) {
        this.array = new int[k];
        this.front = 0;
        this.end = -1;
        this.size = 0;
    }
    
    public boolean enQueue(int value) {
        if(size < array.length) {
            end = (end + 1) % array.length;
            array[end] = value;
            size++;
            return true;
        } else {
            return false;
        }
    }
    
    public boolean deQueue() {
        if(size > 0) {
            front = (front + 1) % array.length;
            size--;
            return true;
        } else {
            return false;
        }
    }
    
    public int Front() {
        return size > 0 ? array[front] : -1;
    }
    
    public int Rear() {
        return size > 0 ? array[end] : -1;
    }
    
    public boolean isEmpty() {
        return size == 0;
    }
    
    public boolean isFull() {
        return size == array.length;
    }
}

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

As an AI language model, I am not capable of providing you with a complete implementation of circular queue using linked list in C language. However, I can provide you with the main() function of this program as an example: ``` #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node *next; }; struct Queue { struct Node *front, *rear; }; void enqueue(struct Queue *q, int data) { struct Node *temp = (struct Node*)malloc(sizeof(struct Node)); temp->data = data; if (q->front == NULL) { q->front = temp; } else { q->rear->next = temp; } q->rear = temp; q->rear->next = q->front; } int dequeue(struct Queue *q) { if (q->front == NULL) { printf("Queue is empty.\n"); return -1; } else if (q->front == q->rear) { int data = q->front->data; free(q->front); q->front = NULL; q->rear = NULL; return data; } else { int data = q->front->data; struct Node *temp = q->front; q->front = q->front->next; q->rear->next = q->front; free(temp); return data; } } void display(struct Queue *q) { struct Node *temp = q->front; printf("Queue: "); if (q->front == NULL) { printf("empty\n"); } else { while (temp->next != q->front) { printf("%d ", temp->data); temp = temp->next; } printf("%d ", temp->data); printf("\n"); } } int main() { struct Queue q; q.front = NULL; q.rear = NULL; enqueue(&q, 10); enqueue(&q, 20); enqueue(&q, 30); enqueue(&q, 40); enqueue(&q, 50); display(&q); int data = dequeue(&q); printf("Dequeued element: %d\n", data); display(&q); enqueue(&q, 60); enqueue(&q, 70); display(&q); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值