collections queue added
parent
bcca7df5f8
commit
466ab31277
@ -0,0 +1,33 @@
|
||||
package queue
|
||||
|
||||
type Queue struct {
|
||||
queue []interface{}
|
||||
len int
|
||||
}
|
||||
|
||||
func New() *Queue {
|
||||
queue := &Queue{}
|
||||
queue.queue = make([]interface{}, 0)
|
||||
queue.len = 0
|
||||
return queue
|
||||
}
|
||||
|
||||
func (queue *Queue) Length() int {
|
||||
return queue.len
|
||||
}
|
||||
|
||||
func (q *Queue) Remove() interface{} {
|
||||
tmp := q.queue[0]
|
||||
q.queue = q.queue[1:]
|
||||
q.len -= 1
|
||||
return tmp
|
||||
}
|
||||
|
||||
func (q *Queue) Peek() interface{} {
|
||||
return q.queue[0]
|
||||
}
|
||||
|
||||
func (q *Queue) Add(value interface{}) {
|
||||
q.len += 1
|
||||
q.queue = append(q.queue, value)
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package queue
|
||||
|
||||
import "testing"
|
||||
|
||||
func Test_Queue(t *testing.T) {
|
||||
queue := New()
|
||||
|
||||
queue.Add(1)
|
||||
queue.Add(2)
|
||||
queue.Add(3)
|
||||
|
||||
if queue.Length() != 3 {
|
||||
t.Error("[Error] queue length is wrong")
|
||||
}
|
||||
|
||||
element := queue.Remove()
|
||||
|
||||
if element != 1 {
|
||||
t.Error("[Error] remove is wrong")
|
||||
}
|
||||
|
||||
if queue.Length() != 2 {
|
||||
t.Error("[Error] queue length is wrong after pop")
|
||||
}
|
||||
|
||||
if queue.Peek() != 2 {
|
||||
t.Error("[Error] queue Peek is wrong")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue