From 574bc4b99adfc01fac08a6c7f2cfbc029cfc97c4 Mon Sep 17 00:00:00 2001 From: leo Date: Wed, 3 Oct 2018 18:30:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E7=BB=84=E5=AE=9E=E7=8E=B0=20by=20gol?= =?UTF-8?q?ang?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go/05_array/array.go | 91 +++++++++++++++++++++++++++++++++++++++ go/05_array/array_test.go | 59 +++++++++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 go/05_array/array.go create mode 100644 go/05_array/array_test.go diff --git a/go/05_array/array.go b/go/05_array/array.go new file mode 100644 index 0000000..345cf3c --- /dev/null +++ b/go/05_array/array.go @@ -0,0 +1,91 @@ +package _5_array + +import ( + "errors" + "fmt" +) + +/** + * 1) 数组的插入、删除、按照下标随机访问操作; + * 2)数组中的数据是int类型的; + * + * Author: leo + */ + +type Array struct { + data []int + length uint +} + +//为数组初始化内存 +func NewArray(capacity uint) *Array { + if capacity == 0 { + return nil + } + return &Array{ + data: make([]int, capacity, capacity), + length: 0, + } +} + +func (this *Array) Len() uint { + return this.length +} + +func (this *Array) isIndexOutOfRange(index uint) bool { + if this.length != 0 && index > this.length { + return true + } + return false +} + +//通过索引查找数组,索引范围[0,n-1] +func (this *Array) Find(index uint) (int, error) { + if this.isIndexOutOfRange(index) { + return 0, errors.New("out of index range") + } + return this.data[index], nil +} + +//插入数值到索引index上 +func (this *Array) Insert(index uint, v int) error { + if this.Len() == uint(cap(this.data)) { + return errors.New("full array") + } + if this.isIndexOutOfRange(index) { + return errors.New("out of index range") + } + + for i := this.length; i > index; i-- { + this.data[i] = this.data[i-1] + } + this.data[index] = v + this.length++ + return nil +} + +func (this *Array) InsertToTail(v int) error { + return this.Insert(this.Len(), v) +} + +//删除索引index上的值 +func (this *Array) Delete(index uint) (int, error) { + if this.isIndexOutOfRange(index) { + return 0, errors.New("out of index range") + } + v := this.data[index] + for i := index; i < this.Len()-1; i++ { + this.data[i] = this.data[i+1] + } + this.length-- + return v, nil +} + +//打印数列 +func (this *Array) Print() { + var format string + for i := uint(0); i < this.Len(); i++ { + format += fmt.Sprintf("|%+v", this.data[i]) + } + fmt.Println(format) +} diff --git a/go/05_array/array_test.go b/go/05_array/array_test.go new file mode 100644 index 0000000..f3c5073 --- /dev/null +++ b/go/05_array/array_test.go @@ -0,0 +1,59 @@ +package _5_array + +import ( + "testing" +) + +func TestInsert(t *testing.T) { + capacity := 10 + arr := NewArray(uint(capacity)) + for i := 0; i < capacity-2; i++ { + err := arr.Insert(uint(i), i+1) + if nil != err { + t.Fatal(err.Error()) + } + } + arr.Print() + + arr.Insert(uint(6), 999) + arr.Print() + + arr.InsertToTail(666) + arr.Print() +} + +func TestDelete(t *testing.T) { + capacity := 10 + arr := NewArray(uint(capacity)) + for i := 0; i < capacity; i++ { + err := arr.Insert(uint(i), i+1) + if nil != err { + t.Fatal(err.Error()) + } + } + arr.Print() + + for i := 9; i >= 0; i-- { + _, err := arr.Delete(uint(i)) + if nil != err { + t.Fatal(err) + } + arr.Print() + } +} + +func TestFind(t *testing.T) { + capacity := 10 + arr := NewArray(uint(capacity)) + for i := 0; i < capacity; i++ { + err := arr.Insert(uint(i), i+1) + if nil != err { + t.Fatal(err.Error()) + } + } + arr.Print() + + t.Log(arr.Find(0)) + t.Log(arr.Find(9)) + t.Log(arr.Find(11)) +}