type MyStack struct {
list *list.List
}
/** Initialize your data structure here. */
func Constructor() *MyStack {
//New创建一个链表。
list := list.New()
return &MyStack{list}
}
/** Push element x onto stack. */
func (this *MyStack) Push(x int) {
//PushBack将一个值为v的新元素插入链表的最后一个位置,返回生成的新元素。
this.list.PushBack(x)
}
/** Removes the element on top of the stack and returns that element. */
func (this *MyStack) Pop() int {
//拿到最后一个元素
e:=this.list.Back()
if e != nil {
this.list.Remove(e)
q,ok:=e.Value.int()
return q
}
return nil
}
/** Get the top element. */
func (this *MyStack) Top() int {
e:=this.list.Front()
if e != nil {
stack.list.Remove(e)
q,ok:=e.Value.int()
return q
}
return nil
}
/** Returns whether the stack is empty. */
func (this *MyStack) Empty() bool {
return stack.list.Len() == 0
}
/**
* Your MyStack object will be instantiated and called as such:
* obj := Constructor();
* obj.Push(x);
* param_2 := obj.Pop();
* param_3 := obj.Top();
* param_4 := obj.Empty();
*/