Implement Queue by Circular Array-LintCode

204 篇文章 0 订阅

Implement queue by circulant array. You need to support the following methods:
1. CircularQueue(n): initialize a circular array with size n to store elements
2. boolean isFull(): return true if the array is full
3. boolean isEmpty(): return true if there is no element in the array
4. void enqueue(element): add an element to the queue
5. int dequeue(): pop an element from the queue

 注意事项
it's guaranteed in the test cases we won't call enqueue if it's full and we also won't call dequeue if it's empty. So it's ok to raise exception in scenarios described above.

样例

CircularQueue(5)
isFull()   => false
isEmpty() => true
enqueue(1)
dequeue()  => 1

思路

#ifndef C955_H
#define C955_H
#include<iostream>
#include<vector>
using namespace std;
class CircularQueue {
public:
    /**
    * @return:  return true if the array is full
    */
    CircularQueue(int n) {
        // do intialization if necessary
        v = vector<int>(N);
        size = n;
    }
    bool isFull() {
        // write your code here
        return (rear - front + N) % N == size;
    }

    /**
    * @return: return true if there is no element in the array
    */
    bool isEmpty() {
        // write your code here
        return front == rear;
    }

    /**
    * @param element: the element given to be added
    * @return: nothing
    */
    void enqueue(int element) {
        // write your code here
        v[rear++] = element;
        if (rear == N)
            rear = 0;
    }

    /**
    * @return: pop an element from the queue
    */
    int dequeue() {
        // write your code here
        int val = v[front];
        front++;
        if (front == N)
            front = 0;
        return val;
    }
    vector<int> v;//表示循环数组
    int front = 0, rear = 0;//front,rear分别表示头部和尾部
    int N = 1000;//循环数组初始大小
    int size;//存放队列大小
};
#endif
请按以下描述,自定义数据结构,实现一个circular bufferIn computer science, a circular buffer, circular queue, cyclic buffer or ring buffer is a data structure that uses a single,fixed-size buffer as if it were connected end-to-end. This structure lends itself easily to bufering data streams. There were earlycircular buffer implementations in hardware. A circular buffer first starts out empty and has a set length. In the diagram below is a 7-element buffeiAssume that 1 is written in the center of a circular buffer (the exact starting locatiorimportant in a circular buffer)8Then assume that two more elements are added to the circulal2buffers use FIFOlf two elements are removed, the two oldest values inside of the circulal(first in, first out) logic. n the example, 1 8 2 were the first to enter the cremoved,leaving 3 inside of the buffer.If the buffer has 7 elements, then it is completely full6 7 8 5 3 4 5A property of the circular buffer is that when it is full and a subsequent write is performed,overwriting the oldesdata. In the current example, two more elements - A & B - are added and theythe 3 & 475 Alternatively, the routines that manage the buffer could prevent overwriting the data and retur an error or raise an exceptionWhether or not data is overwritten is up to the semantics of the buffer routines or the application using the circular bufer.Finally, if two elements are now removed then what would be retured is not 3 & 4 but 5 8 6 because A & B overwrote the 3 &the 4 yielding the buffer with:
最新发布
05-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值