Skip to content

update: queue implementation, previous solution in go was basic stack #2380

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 31 additions & 19 deletions go/0225-implement-stack-using-queues.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,53 @@
type MyStack struct {
queue []int
Queue Queue
}

type Queue struct {
Elems []int
}

func (q *Queue) Push(e int) {
q.Elems = append(q.Elems, e)
}

func (q *Queue) Pop() int {
n := q.Elems[0]
q.Elems = q.Elems[1:]
return n
}

func Constructor() MyStack {
return MyStack{}
return MyStack{}
}


func (this *MyStack) Push(x int) {
this.queue = append(this.queue, x)
this.Queue.Push(x)
}


func (this *MyStack) Pop() int {
if this.Empty() {
return 0
}

elem := this.queue[len(this.queue) - 1]

this.queue = this.queue[:len(this.queue) - 1]

return elem
k := len(this.Queue.Elems)
for i:=0; i<k-1; i++ {
n := this.Queue.Pop()
this.Queue.Push(n)
}
return this.Queue.Pop()
}


func (this *MyStack) Top() int {
if this.Empty() {
return 0
}

return this.queue[len(this.queue) - 1]
k := len(this.Queue.Elems)
for i:=0; i<k-1; i++ {
n := this.Queue.Pop()
this.Queue.Push(n)
}
n := this.Queue.Pop()
this.Queue.Push(n)
return n
}


func (this *MyStack) Empty() bool {
return len(this.queue) == 0
return len(this.Queue.Elems) == 0
}