线性表时n(n>=0)个元素构成的有序序列
* 线性表的的基本操作
1. func MakeEmpty(maxlen int) List :初始化一个线性表
2. func (this *List) FindE(K int) ElementType :根据位序K,返回响应元素
3. func (this *List) FindFristEleIndex(ele ElementType) int:在线性表L中查找E的第一次出现的位置
4. func (this *List) Insert(ele ElementType, i int) :在位序I前插入一个新元素E
5. func (this *List) Delete(i int) :删除指定位序I的元素
6. func (this *List) Length() int: 返回线性表L的长度
package main
import (
"errors"
"time"
)
/**
线性表的的数据单元 数据类型可以随便定义 当然可以是一些简单的基本数据类型 例如 int float string
以下是例子 我将存储一些人员信息
*/
type ElementType struct {
Brithday time.Time //生日 golang 自带的time包中的一个结构体
Name string //姓名
IDNumber string //身份证号
Sex bool //性别
}
type List struct {
MaxLen int //最大长度
CurrentLen int //当前长度
Man []ElementType //数据单元为ElementType的切片 在golang中 切片与数组的底层就是一个线性表
}
//初始化一个线性表
func MakeEmpty(maxlen int) List {
return List{MaxLen: maxlen, CurrentLen: 0, Man: make([]ElementType, maxlen)}
}
//返回线性表中序号为K的元素
func (this *List) Find(K int) ElementType {
return this.Man[K]
}
//在线性表L中查找E的第一次出现的位置
func (this *List) FindFristEleIndex(ele ElementType) int {
for index, val := range this.Man {
if val == ele {
return index
}
}
return -1
}
//在位序I前插入一个新元素E
func (this *List) Insert(ele ElementType, i int) error {
if this.CurrentLen == this.MaxLen {
return errors.New("满了")
}
if i < 0 || i > this.MaxLen {
return errors.New("请检查i值")
}
for j := this.CurrentLen - 1; j >= i; j-- {
this.Man[j+1] = this.Man[j]
}
this.CurrentLen++
this.Man[i] = ele
return nil
}
//删除指定位序I的元素
func (this *List) Delete(i int) error {
if i >= this.CurrentLen {
return errors.New("请检查 i 值")
}
for j := i; j < this.CurrentLen-1; j++ {
this.Man[j] = this.Man[j+1]
}
this.Man[this.CurrentLen-1] = ElementType{}
this.CurrentLen--
return nil
}
func (this *List) Length() int {
return this.CurrentLen
}
func main() {
}