HTMLify

LeetCode - Implement Queue using Stacks - Go
Views: 14 | Author: abh
// Stack implimentatin
type MyStack struct {
    values []int
	len int
}

func (this *MyStack) Push(x int)  {
    this.values = append(this.values, x)
	this.len++
}


func (this *MyStack) Pop() int {
	l := this.values[this.len-1]
	this.values = this.values[:this.len-1]
    this.len--
	return l
}

func (this *MyStack) Top() int {
    return this.values[this.len-1]
}

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

// Queue impimentiotn
type MyQueue struct {
    values MyStack
}


func Constructor() MyQueue {
    var queue MyQueue
	return queue
}


func (this *MyQueue) Push(x int)  {
    this.values.Push(x)
}


func (this *MyQueue) Pop() int {
    var es MyStack
	for ;!this.values.Empty(); {
		es.Push(this.values.Pop())
	}
	e := es.Pop()
	for ;!es.Empty(); {
		this.values.Push(es.Pop())
	}
	return e
}


func (this *MyQueue) Peek() int {
    return this.values.values[0]
}


func (this *MyQueue) Empty() bool {
    return this.values.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();
 */

Comments