```
package main
import "fmt"
//(1) 线性表结构,使用go切片
type Liststruct {
val []interface{} // 可存放不同类型的值
length uint32 //线性表的长度
}
/* 初始化一个空线性表*/
func (L *List) InitList(vinterface{}, length uint32) {
L.length = length
if len(L.val) == 0 {
L.val = make([]interface{}, L.length)
}
for i := uint32(0);i < L.length; i +=1 {
L.val[i] = v
}
}
/* 销毁线性表*/
func (L *List) DestroyList() {
L.length =0
L.val = make([]interface{}, L.length)
}
/* 将线性表置为空表*/
func (L *List) ClearList() {
L.DestroyList()
}
/* 返回线性表的长度*/
func (L *List) ListLength() uint32 {
return uint32(L.length)
}
/* 返回第i个数据元素*/
func (L *List) GetElem(e int)interface{} {
return L.val[e-1]
}
/* 返回L中第1个与e满足compare函数的数据元素的位序.若这样的数据元素不存在,则返回0 */
func (L *List) LocateElem(vinterface{}, ffunc(a ...interface{}) bool) uint32 {
w := uint32(0)
for i := uint32(0);i < L.length;i +=1 {
if f(L.val[i], v) ==true {
w = i
break
}
//if v == L.val[i] {
// w = i
// break
//}
}
return w
}
/* 返回要查找值的前驱,如果没有前驱,则返回空*/
func (L *List) PriorElem(vinterface{})interface{} {
w := uint32(0)
for i := uint32(0);i < L.length;i +=1 {
if v == L.val[i] {
w = i +1
break
}
}
if w ==0 || w ==1{
return nil
} else {
return L.val[w-2]
}
}
/* 返回要查找值的后驱,如果没有后驱,则返回空*/
func (L *List) NextElem(vinterface{})interface{} {
w := uint32(0)
for i := uint32(0);i < L.length;i +=1 {
if v == L.val[i] {
w = i +1
break
}
}
if w ==0 || w == L.length {
return nil
}else {
return L.val[w]
}
}
/* 在线性表中的第i个位置插入一个数据*/
func (L *List) ListInsert(i uint32, vinterface{}) {
if i <=1 {
a := make([]interface{},1)
a[0] = v
L.val = append(a, L.val...)
} else if i >= L.length {
L.val = append(L.val, v)
} else {
// rear := L.val[i-1:],这种方式是引用,而不是创建新副本
rear := make([]interface{},0)
rear = append(rear, L.val[i-1:]...)
L.val = append(L.val[:i-1], v)
L.val = append(L.val, rear...)
}
}
/* 删除第i个值,线性表长度减一,并用e返回其值*/
func (L *List) ListChange(i uint32, vinterface{}) {
if i <=1 {
L.val[i-1] = v
} else if i >= L.length {
L.val[L.length-1] = v
} else {
L.val[i-1] = v
}
}
/* 删除第i个值,线性表长度减一,并用e返回其值*/
func (L *List) ListDelete(i uint32)interface{} {
e := L.val[i-1]
L.val = append(L.val[:i-1], L.val[i:]...)
return e
}
/* 对线性表的每一个函数都调用visit()函数*/
func (L *List) ListTraverse(ffunc(a ...interface{})) {
for i :=range L.val {
f(L.val[i])
}
}
func print(v ...interface{}) {
fmt.Print(v[0]," ")
}
func main() {
var l = List{}
var i int
var c1, c2, v string
for true {
fmt.Println("---------------------------------------------------")
fmt.Println("1) 管理线性表")
fmt.Println("2) 增删改")
fmt.Println("3) 查找")
fmt.Println("4) 打印")
fmt.Scan(&c1)
if c1 !="1" && c1 !="2" && c1 !="3" && c1 !="4" {
break
}
switch c1 {
case "1":
for true {
fmt.Println("-------------------------------------")
fmt.Println("1) 初始化线性表")
fmt.Println("2) 清空线性表")
fmt.Println("3) 销毁线性表")
fmt.Println("其他-退出")
fmt.Scan(&c2)
if c2 !="1" && c2 !="2" && c2 !="3" {
break
}
switch c2 {
case "1":l.InitList("1",10);
case "2":l.ClearList();
case "3":l.DestroyList();
}
}
case "2":
for true {
fmt.Println("-------------------------------------------")
fmt.Println("1) 添加一个数据")
fmt.Println("2) 删除一个数据")
fmt.Println("3) 修改数据")
fmt.Println("其他-退出")
fmt.Scan(&c2)
if c2 !="1" && c2 !="2" && c2 !="3" {
break
}
switch c2 {
case "1":
fmt.Print("输入位置, 值: ")
fmt.Scan(&i, &v)
l.ListInsert(uint32(i), v)
case "2":
fmt.Print("输入位置: ")
fmt.Scan(&i)
l.ListDelete(uint32(i))
case "3":
fmt.Print("输入位置, 值: ")
fmt.Scan(&i, &v)
l.ListChange(uint32(i), v)
}
}
;
case "3":
for true {
fmt.Println("----------------------------")
fmt.Println("1) 获取第i个值")
fmt.Println("2) 获取值第一次出现的位置")
fmt.Println("3) 获取值第一次出现的位置的前继值")
fmt.Println("4) 获取值第一次出现的位置的后继值")
fmt.Println("其他-退出")
fmt.Scan(&c2)
if c2 !="1" && c2 !="2" && c2 !="3" && c2 !="4" {
break
}
switch c2 {
case "1":;
fmt.Scanf("%d", &i)
val := l.GetElem(i)
fmt.Println("第", i,"个值为: ", val)
case "2":
fmt.Scan(&v)
fmt.Println("值", v,"出现的位置: ", l.LocateElem(v,func(a ...interface{}) bool {
if a[0] == a[1] {
return true
}else {
return false
}
}))
case "3":
fmt.Scan(&v)
fmt.Println("值", v,"出现的前缀值: ", l.PriorElem(v))
case "4":
fmt.Scan(&v)
fmt.Println("值", v,"出现的后缀值: ", l.NextElem(v))
}
}
;
case "4":
l.ListTraverse(print)
}
}
}
```