type Node struct { Data int Next *Node Priv *Node }
type MyCircularDeque struct { Front *Node Last *Node length int maxLen int }
/** Initialize your data structure here. Set the size of the deque to be k. */ funcConstructor(k int)MyCircularDeque { return MyCircularDeque{ length: 0, maxLen: k, Front: nil, Last: nil, } }
/** Adds an item at the front of Deque. Return true if the operation is successful. */ func(this *MyCircularDeque)InsertFront(value int)bool { if this.length == this.maxLen { returnfalse } if this.length == 0 { this.Last = &Node{ Data: value, } this.Front = this.Last } else { this.Front.Priv = &Node{ Data: value, Next: this.Front, } this.Front = this.Front.Priv } this.length++ returntrue }
/** Adds an item at the Last of Deque. Return true if the operation is successful. */ func(this *MyCircularDeque)InsertLast(value int)bool { if this.length == this.maxLen { returnfalse } if this.length == 0 { this.Front = &Node{ Data: value, } this.Last = this.Front } else { this.Last.Next = &Node{ Data: value, Priv: this.Last, } this.Last = this.Last.Next } this.length++ returntrue }
/** Deletes an item from the front of Deque. Return true if the operation is successful. */ func(this *MyCircularDeque)DeleteFront()bool { if this.length == 0 { returnfalse } this.Front = this.Front.Next if this.Front != nil { this.Front.Priv = nil } this.length-- returntrue }
/** Deletes an item from the Last of Deque. Return true if the operation is successful. */ func(this *MyCircularDeque)DeleteLast()bool { if this.length == 0 { returnfalse } this.Last = this.Last.Priv if this.Last != nil { this.Last.Next = nil } this.length-- returntrue }
/** Get the front item from the deque. */ func(this *MyCircularDeque)GetFront()int { if this.length == 0 { return-1 } return this.Front.Data }
/** Get the last item from the deque. */ func(this *MyCircularDeque)GetLast()int { if this.length == 0 { return-1 } return this.Last.Data }
// Alias of GetLast for the foolish leetcode func(this *MyCircularDeque)GetRear()int { if this.length == 0 { return-1 } return this.Last.Data }
/** Checks whether the circular deque is empty or not. */ func(this *MyCircularDeque)IsEmpty()bool { return (this.length == 0) }
/** Checks whether the circular deque is full or not. */ func(this *MyCircularDeque)IsFull()bool { return (this.length == this.maxLen) }
/** * Your MyCircularDeque object will be instantiated and called as such: * obj := Constructor(k); * param_1 := obj.InsertFront(value); * param_2 := obj.InsertLast(value); * param_3 := obj.DeleteFront(); * param_4 := obj.DeleteLast(); * param_5 := obj.GetFront(); * param_6 := obj.GetLast(); * param_7 := obj.IsEmpty(); * param_8 := obj.IsFull(); */
type MyCircularDeque struct { data []int pFront int pAfterLast int maxLen int }
/** Initialize your data structure here. Set the size of the deque to be k. */ funcConstructor(k int)MyCircularDeque { return MyCircularDeque{ maxLen: k+1, pFront: 0, pAfterLast: 0, data: make([]int, k+1), } }
/** Adds an item at the front of Deque. Return true if the operation is successful. */ func(this *MyCircularDeque)InsertFront(value int)bool { if this.IsFull() { returnfalse } this.pFront = (this.pFront - 1 + this.maxLen) % this.maxLen // notice to promise >= 0 this.data[this.pFront] = value returntrue }
/** Adds an item at the Last of Deque. Return true if the operation is successful. */ func(this *MyCircularDeque)InsertLast(value int)bool { if this.IsFull() { returnfalse } this.data[this.pAfterLast] = value this.pAfterLast = (this.pAfterLast + 1) % this.maxLen returntrue }
/** Deletes an item from the front of Deque. Return true if the operation is successful. */ func(this *MyCircularDeque)DeleteFront()bool { if this.IsEmpty() { returnfalse } this.pFront = (this.pFront + 1) % this.maxLen returntrue }
/** Deletes an item from the Last of Deque. Return true if the operation is successful. */ func(this *MyCircularDeque)DeleteLast()bool { if this.IsEmpty() { returnfalse } this.pAfterLast = (this.pAfterLast - 1 + this.maxLen) % this.maxLen returntrue }
/** Get the front item from the deque. */ func(this *MyCircularDeque)GetFront()int { if this.IsEmpty() { return-1 } return this.data[this.pFront] }
/** Get the last item from the deque. */ func(this *MyCircularDeque)GetLast()int { if this.IsEmpty() { return-1 } return this.data[(this.pAfterLast - 1 + this.maxLen) % this.maxLen] }
// Alias of GetLast for the foolish leetcode func(this *MyCircularDeque)GetRear()int { if this.IsEmpty() { return-1 } return this.data[(this.pAfterLast - 1 + this.maxLen) % this.maxLen] }
/** Checks whether the circular deque is empty or not. */ func(this *MyCircularDeque)IsEmpty()bool { return (this.pFront == this.pAfterLast) }
/** Checks whether the circular deque is full or not. */ func(this *MyCircularDeque)IsFull()bool { return (this.pAfterLast + 1) % this.maxLen == this.pFront }