diff --git a/collections/queue/queue.go b/collections/queue/queue.go index 4457963..6658a32 100644 --- a/collections/queue/queue.go +++ b/collections/queue/queue.go @@ -1,26 +1,26 @@ package queue type Queue struct { - queue []interface{} + queue []interface{} len int } func New() *Queue { - queue := &Queue{} - queue.queue = make([]interface{}, 0) + queue := &Queue{} + queue.queue = make([]interface{}, 0) queue.len = 0 return queue } func (queue *Queue) Length() int { - return queue.len + return queue.len } func (q *Queue) Remove() interface{} { - tmp := q.queue[0] - q.queue = q.queue[1:] - q.len -= 1 - return tmp + tmp := q.queue[0] + q.queue = q.queue[1:] + q.len -= 1 + return tmp } func (q *Queue) Peek() interface{} { @@ -28,6 +28,6 @@ func (q *Queue) Peek() interface{} { } func (q *Queue) Add(value interface{}) { - q.len += 1 - q.queue = append(q.queue, value) + q.len += 1 + q.queue = append(q.queue, value) } diff --git a/collections/queue/queue_test.go b/collections/queue/queue_test.go index f96041b..32361cf 100644 --- a/collections/queue/queue_test.go +++ b/collections/queue/queue_test.go @@ -3,27 +3,27 @@ package queue import "testing" func Test_Queue(t *testing.T) { - queue := New() + queue := New() - queue.Add(1) - queue.Add(2) - queue.Add(3) + queue.Add(1) + queue.Add(2) + queue.Add(3) - if queue.Length() != 3 { - t.Error("[Error] queue length is wrong") - } + if queue.Length() != 3 { + t.Error("[Error] queue length is wrong") + } element := queue.Remove() - if element != 1 { - t.Error("[Error] remove is wrong") - } + 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") - } + if queue.Length() != 2 { + t.Error("[Error] queue length is wrong after pop") + } + + if queue.Peek() != 2 { + t.Error("[Error] queue Peek is wrong") + } } diff --git a/collections/stack/stack.go b/collections/stack/stack.go index 3e020cf..cc2af21 100644 --- a/collections/stack/stack.go +++ b/collections/stack/stack.go @@ -1,24 +1,24 @@ package stack type Stack struct { - st []interface{} + st []interface{} len int } func New() *Stack { - stack := &Stack{} - stack.st = make([]interface{}, 1) + stack := &Stack{} + stack.st = make([]interface{}, 1) stack.len = 0 return stack } func (stack *Stack) Length() int { - return stack.len + return stack.len } func (stack *Stack) Pop() { - stack.st = stack.st[1:] - stack.len -= 1 + stack.st = stack.st[1:] + stack.len -= 1 } func (stack *Stack) Peek() interface{} { @@ -26,36 +26,36 @@ func (stack *Stack) Peek() interface{} { } func (stack *Stack) IsEmpty() bool { - return (stack.len == 0) + return (stack.len == 0) } func (stack *Stack) Push(value interface{}) { - stack.len += 1 - add(stack, value) + stack.len += 1 + add(stack, value) } func add(slice *Stack, value interface{}) { - var tmpSlice []interface{} = make([]interface{}, slice.len) - if slice.len == 0 { - slice.st[0] = value - return - } - - for i:=0; i < slice.len; i++ { - tmpSlice[i] = 0 - } - - for i:=0; i < slice.len; i++ { - if i == 0 { - tmpSlice[0] = value - } else { - tmpSlice[i] = slice.st[i - 1] - } - - if i == slice.len - 1 { - break - } - } - - slice.st = tmpSlice + var tmpSlice []interface{} = make([]interface{}, slice.len) + if slice.len == 0 { + slice.st[0] = value + return + } + + for i:=0; i < slice.len; i++ { + tmpSlice[i] = 0 + } + + for i:=0; i < slice.len; i++ { + if i == 0 { + tmpSlice[0] = value + } else { + tmpSlice[i] = slice.st[i - 1] + } + + if i == slice.len - 1 { + break + } + } + + slice.st = tmpSlice } diff --git a/collections/stack/stack_test.go b/collections/stack/stack_test.go index bc97850..f5c87fd 100644 --- a/collections/stack/stack_test.go +++ b/collections/stack/stack_test.go @@ -3,23 +3,23 @@ package stack import "testing" func Test_Stack(t *testing.T) { - stack := New() + stack := New() stack.Push(5) stack.Push(6) stack.Push(7) - - if stack.Length() != 3 { - t.Error("[Error] stack length is wrong") - } + + if stack.Length() != 3 { + t.Error("[Error] stack length is wrong") + } stack.Pop() - if stack.Length() != 2 { - t.Error("[Error] stack length is wrong after pop") - } - - if stack.Peek() != 6 { - t.Error("[Error] stack Peek is wrong") - } + if stack.Length() != 2 { + t.Error("[Error] stack length is wrong after pop") + } + + if stack.Peek() != 6 { + t.Error("[Error] stack Peek is wrong") + } }