123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- type MyCircularQueue struct {
- queue []int
- size int
- length int
- beg int
- end int
- }
- func Constructor(k int) (queue MyCircularQueue) {
- queue.queue = make([]int, k)
- queue.size = k
- return
- }
- func (this *MyCircularQueue) EnQueue(value int) bool {
- if this.IsFull() {
- return false
- }
- this.queue[this.end] = value
- this.end = (this.end + 1) % this.size
- this.length++
- return true
- }
- func (this *MyCircularQueue) DeQueue() bool {
- if this.IsEmpty() {
- return false
- }
- this.beg = (this.beg + 1) % this.size
- this.length--
- return true
- }
- func (this *MyCircularQueue) Front() int {
- if this.IsEmpty() {
- return -1
- }
- return this.queue[this.beg]
- }
- func (this *MyCircularQueue) Rear() int {
- if this.IsEmpty() {
- return -1
- }
- return this.queue[(this.end+this.size-1)%this.size]
- }
- func (this *MyCircularQueue) IsEmpty() bool {
- return this.length == 0
- }
- func (this *MyCircularQueue) IsFull() bool {
- return this.length == this.size
- }
|