题解 | #两个队列实现栈#
两个队列实现栈
https://www.nowcoder.com/practice/9fc5ae0e203f4d079b68dee34818832a
package main import "container/list" var list1 list.List var list2 list.List func push(element int) { list1.PushBack(element) } func pop() int { if list1.Len() == 0 { for list2.Len() != 0 { e := list2.Front() list2.Remove(e) list1.PushBack(e.Value) } } for list1.Len() > 1 { e := list1.Front() list1.Remove(e) list2.PushBack(e.Value) } e := list1.Front() list1.Remove(e) return e.Value.(int) } func top() int { if list1.Len() == 0 { for list2.Len() != 0 { e := list2.Front() list2.Remove(e) list1.PushBack(e.Value) } } for list1.Len() > 1 { e := list1.Front() list1.Remove(e) list2.PushBack(e.Value) } e := list1.Front() return e.Value.(int) } func empty() bool { return list1.Len() == 0 && list2.Len() ==0 }