From 00eaaa6668d1c024b7bfd67d3cbded569b06ccbb Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Fri, 16 Dec 2022 10:03:42 +0330 Subject: 232 --- 232/go.mod | 5 ++++ 232/go.sum | 2 ++ 232/main.go | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 232/go.mod create mode 100644 232/go.sum create mode 100644 232/main.go diff --git a/232/go.mod b/232/go.mod new file mode 100644 index 0000000..8de9603 --- /dev/null +++ b/232/go.mod @@ -0,0 +1,5 @@ +module 232 + +go 1.19 + +require github.com/emirpasic/gods v1.18.1 diff --git a/232/go.sum b/232/go.sum new file mode 100644 index 0000000..b5ad666 --- /dev/null +++ b/232/go.sum @@ -0,0 +1,2 @@ +github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= +github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ= diff --git a/232/main.go b/232/main.go new file mode 100644 index 0000000..718e526 --- /dev/null +++ b/232/main.go @@ -0,0 +1,93 @@ +package main + +import ( + "fmt" + + "github.com/emirpasic/gods/stacks/arraystack" +) + +type MyQueue struct { + s1 *arraystack.Stack + s2 *arraystack.Stack +} + +func Constructor() MyQueue { + s1 := arraystack.New() + s2 := arraystack.New() + + q := MyQueue{s1: s1, s2: s2} + + return q +} + +func (this *MyQueue) Push(x int) { + this.s1.Push(x) +} + +func (this *MyQueue) Pop() int { + for { + if val, ok := this.s1.Pop(); ok { + this.s2.Push(val) + } else { + break + } + } + + val, _ := this.s2.Pop() + + for { + if val, ok := this.s2.Pop(); ok { + this.s1.Push(val) + } else { + break + } + } + + return val.(int) +} + +func (this *MyQueue) Peek() int { + for { + if val, ok := this.s1.Pop(); ok { + this.s2.Push(val) + } else { + break + } + } + + val, _ := this.s2.Pop() + this.s2.Push(val) + + for { + if val, ok := this.s2.Pop(); ok { + this.s1.Push(val) + } else { + break + } + } + + return val.(int) + +} + +func (this *MyQueue) Empty() bool { + return this.s1.Empty() +} + +/** + * Your MyQueue object will be instantiated and called as such: + * obj := Constructor(); + * obj.Push(x); + * param_2 := obj.Pop(); + * param_3 := obj.Peek(); + * param_4 := obj.Empty(); + */ + +func main() { + obj := Constructor() + obj.Push(1) + obj.Push(2) + fmt.Println(obj.Peek()) + fmt.Println(obj.Pop()) + fmt.Println(obj.Empty()) +} -- cgit v1.2.3