type DoubleList struct {
Elem []interface{}
len int
}
func NewDoubleList() *DoubleList {
return &DoubleList{
Elem: make([]interface{},0),
len: 0,
}
}
func (d *DoubleList) AddFirst(value interface{}) {
d.Elem = append([]interface{}{value},d.Elem...)
d.len++
}
func (d *DoubleList) PollFirst() interface{} {
res := d.Elem[0]
d.Elem = d.Elem[1:]
d.len--
return res
}
func (d *DoubleList) PeekFirst() interface{} {
return d.Elem[0]
}
func (d *DoubleList) PeekLast() interface{} {
return d.Elem[len(d.Elem)-1]
}
func (d *DoubleList) AddLast(value interface{}) {
d.Elem = append(d.Elem,value)
d.len++
}
func (d *DoubleList) PollLast() interface{} {
res := d.Elem[len(d.Elem)-1]
d.Elem = d.Elem[:len(d.Elem)-1]
d.len--
return res
}
func (d *DoubleList)IsEmpty() bool {
return d.len == 0
}
func TestDoubleList(t *testing.T) {
dl := NewDoubleList()
dl.AddFirst(100)
dl.AddLast(1000)
t.Log(dl.Elem)
t.Log(dl.PollFirst())
t.Log(dl.PollFirst())
t.Log(dl.Elem)
dl.AddFirst(100)
dl.AddLast(1000)
t.Log(dl.PollLast())
t.Log(dl.PollLast())
t.Log(dl.Elem)
dl.AddFirst(100)
dl.AddLast(1000)
t.Log(dl.PollFirst())
t.Log(dl.Elem)
t.Log(dl.PollLast())
t.Log(dl.Elem)
}
双端队列的简单实现
最新推荐文章于 2023-05-05 23:39:31 发布