题解 | #两个队列实现栈#

两个队列实现栈

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
}

全部评论

相关推荐

05-16 11:16
已编辑
东华理工大学 Java
牛客737698141号:盲猜几十人小公司,庙小妖风大,咋不叫她去4️⃣呢😁
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务