indentation fixed

pull/4/head
0xAX 10 years ago
parent 466ab31277
commit 0726d6c260

@ -1,26 +1,26 @@
package queue package queue
type Queue struct { type Queue struct {
queue []interface{} queue []interface{}
len int len int
} }
func New() *Queue { func New() *Queue {
queue := &Queue{} queue := &Queue{}
queue.queue = make([]interface{}, 0) queue.queue = make([]interface{}, 0)
queue.len = 0 queue.len = 0
return queue return queue
} }
func (queue *Queue) Length() int { func (queue *Queue) Length() int {
return queue.len return queue.len
} }
func (q *Queue) Remove() interface{} { func (q *Queue) Remove() interface{} {
tmp := q.queue[0] tmp := q.queue[0]
q.queue = q.queue[1:] q.queue = q.queue[1:]
q.len -= 1 q.len -= 1
return tmp return tmp
} }
func (q *Queue) Peek() interface{} { func (q *Queue) Peek() interface{} {
@ -28,6 +28,6 @@ func (q *Queue) Peek() interface{} {
} }
func (q *Queue) Add(value interface{}) { func (q *Queue) Add(value interface{}) {
q.len += 1 q.len += 1
q.queue = append(q.queue, value) q.queue = append(q.queue, value)
} }

@ -3,27 +3,27 @@ package queue
import "testing" import "testing"
func Test_Queue(t *testing.T) { func Test_Queue(t *testing.T) {
queue := New() queue := New()
queue.Add(1) queue.Add(1)
queue.Add(2) queue.Add(2)
queue.Add(3) queue.Add(3)
if queue.Length() != 3 { if queue.Length() != 3 {
t.Error("[Error] queue length is wrong") t.Error("[Error] queue length is wrong")
} }
element := queue.Remove() element := queue.Remove()
if element != 1 { if element != 1 {
t.Error("[Error] remove is wrong") t.Error("[Error] remove is wrong")
} }
if queue.Length() != 2 { if queue.Length() != 2 {
t.Error("[Error] queue length is wrong after pop") t.Error("[Error] queue length is wrong after pop")
} }
if queue.Peek() != 2 { if queue.Peek() != 2 {
t.Error("[Error] queue Peek is wrong") t.Error("[Error] queue Peek is wrong")
} }
} }

@ -1,24 +1,24 @@
package stack package stack
type Stack struct { type Stack struct {
st []interface{} st []interface{}
len int len int
} }
func New() *Stack { func New() *Stack {
stack := &Stack{} stack := &Stack{}
stack.st = make([]interface{}, 1) stack.st = make([]interface{}, 1)
stack.len = 0 stack.len = 0
return stack return stack
} }
func (stack *Stack) Length() int { func (stack *Stack) Length() int {
return stack.len return stack.len
} }
func (stack *Stack) Pop() { func (stack *Stack) Pop() {
stack.st = stack.st[1:] stack.st = stack.st[1:]
stack.len -= 1 stack.len -= 1
} }
func (stack *Stack) Peek() interface{} { func (stack *Stack) Peek() interface{} {
@ -26,36 +26,36 @@ func (stack *Stack) Peek() interface{} {
} }
func (stack *Stack) IsEmpty() bool { func (stack *Stack) IsEmpty() bool {
return (stack.len == 0) return (stack.len == 0)
} }
func (stack *Stack) Push(value interface{}) { func (stack *Stack) Push(value interface{}) {
stack.len += 1 stack.len += 1
add(stack, value) add(stack, value)
} }
func add(slice *Stack, value interface{}) { func add(slice *Stack, value interface{}) {
var tmpSlice []interface{} = make([]interface{}, slice.len) var tmpSlice []interface{} = make([]interface{}, slice.len)
if slice.len == 0 { if slice.len == 0 {
slice.st[0] = value slice.st[0] = value
return return
} }
for i:=0; i < slice.len; i++ { for i:=0; i < slice.len; i++ {
tmpSlice[i] = 0 tmpSlice[i] = 0
} }
for i:=0; i < slice.len; i++ { for i:=0; i < slice.len; i++ {
if i == 0 { if i == 0 {
tmpSlice[0] = value tmpSlice[0] = value
} else { } else {
tmpSlice[i] = slice.st[i - 1] tmpSlice[i] = slice.st[i - 1]
} }
if i == slice.len - 1 { if i == slice.len - 1 {
break break
} }
} }
slice.st = tmpSlice slice.st = tmpSlice
} }

@ -3,23 +3,23 @@ package stack
import "testing" import "testing"
func Test_Stack(t *testing.T) { func Test_Stack(t *testing.T) {
stack := New() stack := New()
stack.Push(5) stack.Push(5)
stack.Push(6) stack.Push(6)
stack.Push(7) stack.Push(7)
if stack.Length() != 3 { if stack.Length() != 3 {
t.Error("[Error] stack length is wrong") t.Error("[Error] stack length is wrong")
} }
stack.Pop() stack.Pop()
if stack.Length() != 2 { if stack.Length() != 2 {
t.Error("[Error] stack length is wrong after pop") t.Error("[Error] stack length is wrong after pop")
} }
if stack.Peek() != 6 { if stack.Peek() != 6 {
t.Error("[Error] stack Peek is wrong") t.Error("[Error] stack Peek is wrong")
} }
} }

Loading…
Cancel
Save