package Queue
type Queue struct {
first *node
last *node
n int
}
type node struct {
item interface{}
next *node
}
func NewQueue() Queue {
return Queue{}
}
func (q Queue) IsEmpty() bool {
return q.n == 0
}
func (q Queue) Size() int {
return q.n
}
func (q *Queue) EnQueue(item interface{}) {
oldlast := q.last
q.last = &node{}
q.last.item = item
q.last.next = nil
if q.IsEmpty() {
q.first = q.last
} else {
oldlast.next = q.last
}
q.n++
}
func (q *Queue) DeQueue() interface{} {
if q.IsEmpty() {
return nil
}
item := q.first.item
q.first = q.first.next
if q.IsEmpty() {
q.last = nil
}
q.n--
return item
}
实现起来很简单,Golang这门语言我非常喜爱,它特别照顾代码洁癖症患者,在vscode中安装相应的插件之后只要command+s保存代码的时候文件会自动格式化,非常整齐舒服,go的代码简洁高效,并且内存安全,容易写出优秀的代码。