Skip to content

Commit bd35f4b

Browse files
Merge pull request wangzheng0822#1 from liutianyi1989/master
数组实现 by golang
2 parents 3cee274 + 574bc4b commit bd35f4b

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed

go/05_array/array.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package _5_array
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
)
7+
8+
/**
9+
* 1) 数组的插入、删除、按照下标随机访问操作;
10+
* 2)数组中的数据是int类型的;
11+
*
12+
* Author: leo
13+
*/
14+
15+
type Array struct {
16+
data []int
17+
length uint
18+
}
19+
20+
//为数组初始化内存
21+
func NewArray(capacity uint) *Array {
22+
if capacity == 0 {
23+
return nil
24+
}
25+
return &Array{
26+
data: make([]int, capacity, capacity),
27+
length: 0,
28+
}
29+
}
30+
31+
func (this *Array) Len() uint {
32+
return this.length
33+
}
34+
35+
func (this *Array) isIndexOutOfRange(index uint) bool {
36+
if this.length != 0 && index > this.length {
37+
return true
38+
}
39+
return false
40+
}
41+
42+
//通过索引查找数组,索引范围[0,n-1]
43+
func (this *Array) Find(index uint) (int, error) {
44+
if this.isIndexOutOfRange(index) {
45+
return 0, errors.New("out of index range")
46+
}
47+
return this.data[index], nil
48+
}
49+
50+
//插入数值到索引index上
51+
func (this *Array) Insert(index uint, v int) error {
52+
if this.Len() == uint(cap(this.data)) {
53+
return errors.New("full array")
54+
}
55+
if this.isIndexOutOfRange(index) {
56+
return errors.New("out of index range")
57+
}
58+
59+
for i := this.length; i > index; i-- {
60+
this.data[i] = this.data[i-1]
61+
}
62+
this.data[index] = v
63+
this.length++
64+
return nil
65+
}
66+
67+
func (this *Array) InsertToTail(v int) error {
68+
return this.Insert(this.Len(), v)
69+
}
70+
71+
//删除索引index上的值
72+
func (this *Array) Delete(index uint) (int, error) {
73+
if this.isIndexOutOfRange(index) {
74+
return 0, errors.New("out of index range")
75+
}
76+
v := this.data[index]
77+
for i := index; i < this.Len()-1; i++ {
78+
this.data[i] = this.data[i+1]
79+
}
80+
this.length--
81+
return v, nil
82+
}
83+
84+
//打印数列
85+
func (this *Array) Print() {
86+
var format string
87+
for i := uint(0); i < this.Len(); i++ {
88+
format += fmt.Sprintf("|%+v", this.data[i])
89+
}
90+
fmt.Println(format)
91+
}

go/05_array/array_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package _5_array
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestInsert(t *testing.T) {
8+
capacity := 10
9+
arr := NewArray(uint(capacity))
10+
for i := 0; i < capacity-2; i++ {
11+
err := arr.Insert(uint(i), i+1)
12+
if nil != err {
13+
t.Fatal(err.Error())
14+
}
15+
}
16+
arr.Print()
17+
18+
arr.Insert(uint(6), 999)
19+
arr.Print()
20+
21+
arr.InsertToTail(666)
22+
arr.Print()
23+
}
24+
25+
func TestDelete(t *testing.T) {
26+
capacity := 10
27+
arr := NewArray(uint(capacity))
28+
for i := 0; i < capacity; i++ {
29+
err := arr.Insert(uint(i), i+1)
30+
if nil != err {
31+
t.Fatal(err.Error())
32+
}
33+
}
34+
arr.Print()
35+
36+
for i := 9; i >= 0; i-- {
37+
_, err := arr.Delete(uint(i))
38+
if nil != err {
39+
t.Fatal(err)
40+
}
41+
arr.Print()
42+
}
43+
}
44+
45+
func TestFind(t *testing.T) {
46+
capacity := 10
47+
arr := NewArray(uint(capacity))
48+
for i := 0; i < capacity; i++ {
49+
err := arr.Insert(uint(i), i+1)
50+
if nil != err {
51+
t.Fatal(err.Error())
52+
}
53+
}
54+
arr.Print()
55+
56+
t.Log(arr.Find(0))
57+
t.Log(arr.Find(9))
58+
t.Log(arr.Find(11))
59+
}

0 commit comments

Comments
 (0)