198
社区成员
发帖
与我相关
我的任务
分享实现栈的方式:
func main() {
stack := list.New()
stack.PushBack("One")
stack.PushBack("Two")
stack.PushBack("Three")
stack.PushBack("Four")
fmt.Println(stack.Len())
fmt.Println(stack.Back().Value)
for stack.Len() > 0 {
fmt.Printf("%#v ", stack.Remove(stack.Back()))
}
/*
4
Four
"Four" "Three" "Two" "One"
*/
}
实现队列的方式:
func main() {
queue := list.New()
queue.PushBack("One")
queue.PushBack("Two")
queue.PushBack("Three")
queue.PushBack("Four")
fmt.Println(queue.Len())
fmt.Println(queue.Back().Value)
for queue.Len() > 0 {
fmt.Printf("%#v ", queue.Remove(queue.Front()))
}
/*
4
Four
"One" "Two" "Three" "Four"
*/
}