|
| 1 | +/** |
| 2 | + * 线性表:顺序结构存储 |
| 3 | + */ |
| 4 | + |
| 5 | +package list |
| 6 | + |
| 7 | +import ( |
| 8 | + "errors" |
| 9 | + "fmt" |
| 10 | +) |
| 11 | + |
| 12 | +var arr [5]int |
| 13 | + |
| 14 | +// 线性表结构体对象 |
| 15 | +type ArrayList struct { |
| 16 | + size int // 该线性表最大容量 |
| 17 | + length int // 该线性表最大长度 |
| 18 | + data [5]int // 线性表内数据,这里为了演示默认设置为100长度的int数组,所有元素默认为0(Go的0值机制) |
| 19 | +} |
| 20 | + |
| 21 | +// 初始化线性表函数 |
| 22 | +func NewArrayList() *ArrayList { |
| 23 | + return &ArrayList{ |
| 24 | + size: 5, |
| 25 | + length: 0, |
| 26 | + data: arr, |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +// 打印线性表 |
| 31 | +func (sl *ArrayList)Show() { |
| 32 | + fmt.Println(sl) |
| 33 | +} |
| 34 | + |
| 35 | +// 插入元素:从末尾append一个数据 |
| 36 | +func (al *ArrayList)Append(data int) error{ |
| 37 | + |
| 38 | + // 判断空间是否已满 |
| 39 | + if al.IsFull() { |
| 40 | + return errors.New("SequenList overflow") |
| 41 | + } |
| 42 | + |
| 43 | + // 从末尾插入:由于数组中所有元素已经默认是0了,那么第一个不是0的元素就是最后一位 |
| 44 | + for i := 0; i <= al.size; i++ { |
| 45 | + if al.data[i] == 0 { |
| 46 | + al.data[i] = data |
| 47 | + al.length++ |
| 48 | + break |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return nil |
| 53 | +} |
| 54 | + |
| 55 | +// 插入元素:任意位置插入元素 |
| 56 | +func (al *ArrayList)Insert(index int, data int) error { |
| 57 | + |
| 58 | + if al.IsFull() { |
| 59 | + return errors.New("SequenList overflow") |
| 60 | + } |
| 61 | + |
| 62 | + if al.isOver(index) { |
| 63 | + return errors.New("index overflow") |
| 64 | + } |
| 65 | + |
| 66 | + // 情况一:在最后一位插入 |
| 67 | + if index == al.length - 1{ |
| 68 | + al.data[index] = data |
| 69 | + al.length++ |
| 70 | + return nil |
| 71 | + } |
| 72 | + |
| 73 | + // 情况二:在其他位置插入 |
| 74 | + currentItem := al.data[index] |
| 75 | + nextItem := al.data[index+1] |
| 76 | + al.data[index] = data // 插入数据 |
| 77 | + al.length++ |
| 78 | + |
| 79 | + for i := index + 1; i <= al.length; i++ { |
| 80 | + |
| 81 | + fmt.Println("i===",i) |
| 82 | + fmt.Println("值===",al.data[i]) |
| 83 | + |
| 84 | + al.data[i] = currentItem |
| 85 | + |
| 86 | + if i == al.length { // 循环到最后一位 |
| 87 | + break |
| 88 | + } |
| 89 | + |
| 90 | + currentItem = nextItem |
| 91 | + |
| 92 | + tmp := al.data[i + 1] |
| 93 | + al.data[i + 1] = nextItem // 后面的数据全部后移 |
| 94 | + nextItem = tmp |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | + return nil |
| 99 | +} |
| 100 | + |
| 101 | +// 删除元素:从末尾pop一个数据 |
| 102 | +func (al *ArrayList)Pop() (int, error) { |
| 103 | + |
| 104 | + if al.IsEmpty() { |
| 105 | + return 0, errors.New("object is empty") |
| 106 | + } |
| 107 | + |
| 108 | + e := al.data[al.length - 1] |
| 109 | + al.data[al.length - 1] = 0 |
| 110 | + al.length -- |
| 111 | + return e, nil |
| 112 | +} |
| 113 | + |
| 114 | +// // 删除元素:从任意位置删除一个数据 |
| 115 | +// func (l *SequenList)Delete(index int) (interface{}, error) { |
| 116 | +// if ok := l.isOver(index); ok { |
| 117 | +// return "", errors.New("索引不在线性表范围内") |
| 118 | +// } |
| 119 | +// if ok := l.IsEmpty(); ok { |
| 120 | +// return "", errors.New("空表没有课删除的数据") |
| 121 | +// } |
| 122 | +// deleteE := l.data[index - 1] |
| 123 | +// for j := index - 1; j < l.length - 1; j++ { |
| 124 | +// l.data[j] = l.data[ j + 1] |
| 125 | +// } |
| 126 | +// l.data = l.data[:l.length - 1] |
| 127 | +// l.length -- |
| 128 | +// return deleteE, nil |
| 129 | +// } |
| 130 | + |
| 131 | +// // 修改元素 |
| 132 | + |
| 133 | +// // 查询某个元素 |
| 134 | +// func (l *ArrayList)GetByIndex(index int) (interface{}, error) { |
| 135 | +// if ok := l.isOver(index); ok { |
| 136 | +// return "", errors.New("查询索引越界") |
| 137 | +// } |
| 138 | +// return l.data[index - 1], nil |
| 139 | +// } |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | +// 下列为常用工具方法 |
| 148 | + |
| 149 | +// 获取线性表长度 |
| 150 | +func (al *ArrayList)Length() int{ |
| 151 | + return al.length |
| 152 | +} |
| 153 | + |
| 154 | +// 判断线性表是否已满 |
| 155 | +func (sl *ArrayList)IsFull() bool { |
| 156 | + if sl.length == sl.size { |
| 157 | + return true |
| 158 | + } |
| 159 | + return false |
| 160 | +} |
| 161 | + |
| 162 | +// 判断线性表是否为空方法 |
| 163 | +func (al *ArrayList)IsEmpty() bool { |
| 164 | + if al.length == 0 { |
| 165 | + return true |
| 166 | + } |
| 167 | + return false |
| 168 | +} |
| 169 | + |
| 170 | +// 获取线性表容量 |
| 171 | +func (sl *ArrayList)Size() int{ |
| 172 | + return sl.size |
| 173 | +} |
| 174 | + |
| 175 | +// 判断线性表索引是否越界 |
| 176 | +func (al *ArrayList)isOver(index int) bool { |
| 177 | + if index < 0 || index > al.length - 1 { |
| 178 | + return true |
| 179 | + } |
| 180 | + return false |
| 181 | +} |
0 commit comments