题目链接
list包中List是一个双向链表,可以任意插入删除元素
type CQueue struct {
stack1,stack2 *list.List
}
func Constructor() CQueue {
return CQueue{
//New返回指针类型
stack1:list.New(),
stack2:list.New(),
}
}
func (this *CQueue) AppendTail(value int) {
this.stack1.PushBack(value)
}
func (this *CQueue) DeleteHead() int {
if this.stack2.Len()==0{
for this.stack1.Len()>0{
this.stack2.PushBack(this.stack1.Remove(this.stack1.Back()))
}
}
if this.stack2.Len()!=0{
//返回指针类型
elem:=this.stack2.Back()
//按照指针类型删除
this.stack2.Remove(elem)
return elem.Value.(int)
}
return -1
}
/**
* Your CQueue object will be instantiated and called as such:
* obj := Constructor();
* obj.AppendTail(value);
* param_2 := obj.DeleteHead();
*/
主要注意go的语法