15-数据结构之数组

91 阅读3分钟

数组

  • 数组的标准定义是:一个存储元素的线性集合(collection),元素可以通过索引来任意存 取,索引通常是数字,用来计算元素之间存储位置的偏移量。

效率

  • 查找 O(1)
  • 插入 O(n)
  • 删除 O(n)

构建动态数组

  • ArrayList/ArrayList.go
package ArrayList
import (
    "errors"
    "fmt"
)

// 接口(实现里面所有的函数)
type List interface {//接口定义
    Size() int//数组的大小
    Get(index int)(interface{}, error)// 抓取第几个元素 泛型
    Set(index int, newVal interface{})error//修改数据
    Insert(index int, newVal interface{})error//插入数据
    Append(newVal interface{})//追加数据
    Clear()//清空数组
    Delete(index int)error//删除
    String()string//返回字符串
} 

// 数据结构
type ArrayList struct {
    dataStore [] interface{}//泛型数组 
    TheSize int//数组的大小
}

// 创建数组
func NewArrayList()* ArrayList{
    list := new(ArrayList)//初始化结构体
    list.dataStore = make([]interface{}, 0, 10)//开辟空间10个
    list.TheSize = 0
    return list
}

// 扩容
func (list *ArrayList)checkIsFull() {
    // 判断内存的使用
    if list.TheSize == cap(list.dataStore) {
        // 开辟双倍内存(扩容)
        newDateStore := make([]interface{}, list.TheSize,2*list.TheSize)
        // copy因为上面重新给值得时候必须给初始得长度不然拷贝有问题使用 或者使用 for循环
        copy(newDateStore, list.dataStore)//拷贝一份 原始数据
        // for i := 0; i < list.TheSize; i++ {
        // 	newDateStore[i] = list.dataStore[i]
        // }
        list.dataStore = newDateStore
    }
}

func (list *ArrayList)Size() int {
    return list.TheSize
}
// 获取指定数据
func (list *ArrayList)Get(index int) (interface{}, error) {
    // 1.边界判断
    if index < 0 || index > list.TheSize {
        return nil,errors.New("索引越界")
    }
    return list.dataStore[index],nil
}

// 追加数据
func (list *ArrayList) Append(newVal interface{}) {
    list.dataStore = append(list.dataStore, newVal)//利用append函数
    list.TheSize++
}

func (list *ArrayList) String()string {
    return fmt.Sprint(list.dataStore)
}

func (list *ArrayList) Set(index int, newVal interface{})error {
    // 1.边界判断
    if index < 0 || index > list.TheSize {
        return errors.New("索引越界")
    }
    list.dataStore[index] = newVal
    return nil
}

func (list *ArrayList) Insert(index int, newVal interface{})error {
    // 1.边界判断
    if index < 0 || index > list.TheSize {
        return errors.New("索引越界")
    }
    list.checkIsFull()
    list.dataStore = list.dataStore[:list.TheSize+1]
    for i := list.TheSize; i > index; i-- {
        list.dataStore[i] = list.dataStore[i - 1]
    }
    list.dataStore[index] = newVal//插入数据
    list.TheSize++
    //数组满了需要扩容 
    return nil
}

func (list *ArrayList) Clear() {
    list.dataStore = make([]interface{}, 0, 10)
    list.TheSize = 0
}
func (list *ArrayList) Delete(index int)error {
    // 1.边界判断
    if index < 0 || index > list.TheSize {
        return errors.New("索引越界")
    }
    list.dataStore = append(list.dataStore[:index], list.dataStore[index+1:]...)
    list.TheSize--
    return nil
}
  • 应用
package main
import (
    "godemo01/ArrayList"
    "fmt"
)
func main() {
    list := ArrayList.NewArrayList()
    list.Append(1)
    list.Append(2)
    list.Append(3)
    for i := 0; i < 10; i++ {
        list.Insert(1, i)
    }	
    list.Delete(2)
    fmt.Println(list)// [1 9 7 6 5 4 3 2 1 0 2 3]
    fmt.Println(list.Size())// 12
    list.Clear();
    fmt.Println(list.TheSize)// 0 小写私有内部使用,大写共有可外部使用
    list.String()
}

小技巧 数组迭代器

  • 解决数组访问方便得问题迭代器模式-常见操作可能有多个类的嵌套
  • 增加Iterator
package ArrayList
import (
    "errors"
    "fmt"
)

// 接口(实现里面所有的函数)
type List interface {//接口定义
    Size() int//数组的大小
    Get(index int)(interface{}, error)// 抓取第几个元素 泛型
    Set(index int, newVal interface{})error//修改数据
    Insert(index int, newVal interface{})error//插入数据
    Append(newVal interface{})//追加数据
    Clear()//清空数组
    Delete(index int)error//删除
    String()string//返回字符串
    Iterator() Iterator//迭代器 增加了
} 

// 数据结构
type ArrayList struct {
    dataStore [] interface{}//泛型数组 
    TheSize int//数组的大小
}

// 创建数组
func NewArrayList()* ArrayList{
    list := new(ArrayList)//初始化结构体
    list.dataStore = make([]interface{}, 0, 10)//开辟空间10个
    list.TheSize = 0
    return list
}

// 扩容
func (list *ArrayList)checkIsFull() {
    // 判断内存的使用
    if list.TheSize == cap(list.dataStore) {
        // 开辟双倍内存(扩容)
        newDateStore := make([]interface{}, list.TheSize,2*list.TheSize)
        // copy因为上面重新给值得时候必须给初始得长度不然拷贝有问题使用 或者使用 for循环
        copy(newDateStore, list.dataStore)//拷贝一份 原始数据
        list.dataStore = newDateStore
    }
}

func (list *ArrayList)Size() int {
    return list.TheSize
}
// 获取指定数据
func (list *ArrayList)Get(index int) (interface{}, error) {
    // 1.边界判断
    if index < 0 || index > list.TheSize {
        return nil,errors.New("索引越界")
    }
    return list.dataStore[index],nil
}

// 追加数据
func (list *ArrayList) Append(newVal interface{}) {
    list.dataStore = append(list.dataStore, newVal)//利用append函数
    list.TheSize++
}

func (list *ArrayList) String()string {
    return fmt.Sprint(list.dataStore)
}

func (list *ArrayList) Set(index int, newVal interface{})error {
    // 1.边界判断
    if index < 0 || index > list.TheSize {
        return errors.New("索引越界")
    }
    list.dataStore[index] = newVal
    return nil
}

func (list *ArrayList) Insert(index int, newVal interface{})error {
    // 1.边界判断
    if index < 0 || index > list.TheSize {
        return errors.New("索引越界")
    }
    list.checkIsFull()
    list.dataStore = list.dataStore[:list.TheSize+1]
    for i := list.TheSize; i > index; i-- {
        list.dataStore[i] = list.dataStore[i - 1]
    }
    list.dataStore[index] = newVal//插入数据
    list.TheSize++
    //数组满了需要扩容 
    return nil
}

func (list *ArrayList) Clear() {
    list.dataStore = make([]interface{}, 0, 10)
    list.TheSize = 0
}
func (list *ArrayList) Delete(index int)error {
    // 1.边界判断
    if index < 0 || index > list.TheSize {
        return errors.New("索引越界")
    }
    list.dataStore = append(list.dataStore[:index], list.dataStore[index+1:]...)
    list.TheSize--
    return nil
}
  • 迭代器书写
// 数组迭代器
package ArrayList
import (
    "errors"
)

type Iterator interface {
    HasNext () bool//是否有下一个
    Next()(interface{}, error)//返回下一个
    Remove()//删除
    GetIndex() int//得到索引
}

type Iterable interface {
    Iterator() Iterator//构造初始化接口
}

// 构造指针访问数组
type ArrayListIterator struct {
    list *ArrayList//数组指针(类的包含)
    currentIndex int//当前得索引
}

func (list *ArrayList) Iterator() Iterator {
    it := new(ArrayListIterator)//构造迭代器
    it.currentIndex = 0
    it.list = list
    return it
}

//是否有下一个
func (it *ArrayListIterator)HasNext () bool{
    // 当前的索引 < 数组的长度
    return it.currentIndex < it.list.TheSize
}
//返回下一个
func (it *ArrayListIterator)Next()(interface{}, error){
    if !it.HasNext() {
        return nil, errors.New("没有下一项了")
    }
    value, err := it.list.Get(it.currentIndex)//抓取当前数据
    it.currentIndex++
    return value, err
}
//删除
func (it *ArrayListIterator)Remove(){
    it.currentIndex--
    it.list.Delete(it.currentIndex)
}
//得到索引
func (it *ArrayListIterator)GetIndex() int{
    return it.currentIndex
}
  • 使用迭代器
package main
import (
    "godemo01/ArrayList"
    "fmt"
)

func main() {
    var list ArrayList.List = ArrayList.NewArrayList()
    list.Append(1)
    list.Append(2)
    list.Append(3)
    list.Append(4)
    list.Append(5)
    // 使用迭代器访问
    for it := list.Iterator(); it.HasNext(); {
        item,_ := it.Next()
        if item == 3 {
            it.Remove()
        }
    }
    fmt.Println(list)// [1 2 4 5 6]
}