From b31c81e8004c5ccf4e903bf7c8d9bf2170085a03 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sat, 28 Sep 2024 16:01:35 +0200 Subject: [PATCH] =?UTF-8?q?go:=20add=20=C2=AB641.=20Design=20Circular=20De?= =?UTF-8?q?que=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- go/design-circular-deque.go | 86 +++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 go/design-circular-deque.go diff --git a/go/design-circular-deque.go b/go/design-circular-deque.go new file mode 100644 index 0000000..91a6822 --- /dev/null +++ b/go/design-circular-deque.go @@ -0,0 +1,86 @@ +package main + +type MyCircularDeque struct { + maxSize int + + deque []int + size int + i int +} + +func Constructor(k int) MyCircularDeque { + return MyCircularDeque{ + maxSize: k, + deque: make([]int, k), + size: 0, + i: 0, + } +} + +func (this *MyCircularDeque) InsertFront(value int) bool { + if this.IsFull() { + return false + } + + this.i = (this.i - 1 + this.maxSize) % this.maxSize + this.deque[this.i] = value + this.size++ + + return true +} + +func (this *MyCircularDeque) InsertLast(value int) bool { + if this.IsFull() { + return false + } + + this.deque[(this.i+this.size)%this.maxSize] = value + this.size++ + + return true +} + +func (this *MyCircularDeque) DeleteFront() bool { + if this.IsEmpty() { + return false + } + + this.size-- + this.i = (this.i + 1) % this.maxSize + + return true +} + +func (this *MyCircularDeque) DeleteLast() bool { + if this.IsEmpty() { + return false + } + + this.size-- + + return true +} + +func (this *MyCircularDeque) GetFront() int { + if this.IsEmpty() { + return -1 + } + + return this.deque[this.i] +} + +func (this *MyCircularDeque) GetRear() int { + if this.IsEmpty() { + return -1 + } + + return this.deque[(this.i+this.size-1)%this.maxSize] +} + +func (this *MyCircularDeque) IsEmpty() bool { + return this.size == 0 +} + +func (this *MyCircularDeque) IsFull() bool { + return this.size == this.maxSize +}