設計你的迴圈佇列實現。 迴圈佇列是一種線性資料結構,其操作表現基於 fifo(先進先出)原則並且隊尾被連線在隊首之後以形成乙個迴圈。它也被稱為「環形緩衝器」。
迴圈佇列的乙個好處是我們可以利用這個佇列之前用過的空間。在乙個普通佇列裡,一旦乙個佇列滿了,我們就不能插入下乙個元素,即使在佇列前面仍有空間。但是使用迴圈佇列,我們能使用這些空間去儲存新的值。
你的實現應該支援如下操作:
mycircularqueue(k): 構造器,設定佇列長度為 k 。
front: 從隊首獲取元素。如果隊列為空,返回 -1 。
rear: 獲取隊尾元素。如果隊列為空,返回 -1 。
enqueue(value): 向迴圈佇列插入乙個元素。如果成功插入則返回真。
dequeue(): 從迴圈佇列中刪除乙個元素。如果成功刪除則返回真。
isempty(): 檢查迴圈佇列是否為空。
isfull(): 檢查迴圈佇列是否已滿。
示例:mycircularqueue circularqueue = new mycircularqueue(3); // 設定長度為 3
circularqueue.enqueue(1); // 返回 true
circularqueue.enqueue(2); // 返回 true
circularqueue.enqueue(3); // 返回 true
circularqueue.enqueue(4); // 返回 false,佇列已滿
circularqueue.rear(); // 返回 3
circularqueue.isfull(); // 返回 true
circularqueue.dequeue(); // 返回 true
circularqueue.enqueue(4); // 返回 true
circularqueue.rear(); // 返回 4
所有的值都在 0 至 1000 的範圍內;
運算元將在 1 至 1000 的範圍內;
請不要使用內建的佇列庫。
typedef struct mycircularqueue;
/** initialize your data structure here. set the size of the queue to be k. */
mycircularqueue* mycircularqueuecreate(int k)
/** insert an element into the circular queue. return true if the operation is successful. */
bool mycircularqueueenqueue(mycircularqueue* obj, int value)
// obj->rear向後移一位
// % (obj->length)為obj->rear在陣列尾的情況,obj->rear向後移一位後,指向陣列的開頭
obj->rear = (obj->rear + 1) % (obj->length);
obj->queue[obj->rear] = value;
return true;
}/** delete an element from the circular queue. return true if the operation is successful. */
bool mycircularqueuedequeue(mycircularqueue* obj)
// obj->front向後移一位
// % (obj->length)為obj->front在陣列尾的情況,obj->front向後移一位後,指向陣列的開頭
obj->front = (obj->front + 1) % (obj->length);
return true;
}/** get the front item from the queue. */
int mycircularqueuefront(mycircularqueue* obj)
return obj->queue[(obj->front + 1) % obj->length];
}/** get the last item from the queue. */
int mycircularqueuerear(mycircularqueue* obj)
return obj->queue[obj->rear];
}/** checks whether the circular queue is empty or not. */
bool mycircularqueueisempty(mycircularqueue* obj)
return false;
}/** checks whether the circular queue is full or not. */
bool mycircularqueueisfull(mycircularqueue* obj)
return false;
}void mycircularqueuefree(mycircularqueue* obj)
/** * your mycircularqueue struct will be instantiated and called as such:
* mycircularqueue* obj = mycircularqueuecreate(k);
* bool param_1 = mycircularqueueenqueue(obj, value);
* bool param_2 = mycircularqueuedequeue(obj);
* int param_3 = mycircularqueuefront(obj);
* int param_4 = mycircularqueuerear(obj);
* bool param_5 = mycircularqueueisempty(obj);
* bool param_6 = mycircularqueueisfull(obj);
* mycircularqueuefree(obj);
*/
LeetCode 622 設計迴圈佇列
設計你的迴圈佇列實現。迴圈佇列是一種線性資料結構,其操作表現基於 fifo 先進先出 原則並且隊尾被連線在隊首之後以形成乙個迴圈。它也被稱為 環形緩衝器 迴圈佇列的乙個好處是我們可以利用這個佇列之前用過的空間。在乙個普通佇列裡,一旦乙個佇列滿了,我們就不能插入下乙個元素,即使在佇列前面仍有空間。但是...
Leetcode 622 設計迴圈佇列
設計你的迴圈佇列實現。迴圈佇列是一種線性資料結構,其操作表現基於 fifo 先進先出 原則並且隊尾被連線在隊首之後以形成乙個迴圈。它也被稱為 環形緩衝器 迴圈佇列的乙個好處是我們可以利用這個佇列之前用過的空間。在乙個普通佇列裡,一旦乙個佇列滿了,我們就不能插入下乙個元素,即使在佇列前面仍有空間。但是...
leetcode 622 設計迴圈佇列
題目鏈結 設計你的迴圈佇列實現。迴圈佇列是一種線性資料結構,其操作表現基於 fifo 先進先出 原則並且隊尾被連線在隊首之後以形成乙個迴圈。它也被稱為 環形緩衝器 迴圈佇列的乙個好處是我們可以利用這個佇列之前用過的空間。在乙個普通佇列裡,一旦乙個佇列滿了,我們就不能插入下乙個元素,即使在佇列前面仍有...