关于Go语言的链表
package main
import (
"fmt"
)
const maxsize = 100
type ElemType int
type SeqList struct {
elem[maxsize] ElemType
length int
}
//初始化链表
func InitList(list *SeqList) {
list.length = 0
}
//链表插入
func InsertList(L *SeqList,x ElemType,i int) bool{
var j int
if L.length == maxsize -1 {
fmt.Println("该表已满")
return false
}
if i <= 0 || i >= L.length + 1 {
fmt.Print("插入位置有误")
return false
}
for j = L.length ; j >= i ;j--{
L.elem[j + 1] = L.elem[j]
}
L.elem[i] = x
L.length++
return true
}
//链表删除并保存删除的元素
func DeleteList(L *SeqList,i int, e *ElemType) bool {
var j int
if i <= 0 || i > L.length{
fmt.Println("不存在该位置元素")
return false
}
*e = L.elem[i]
for j = i;j < L.length;j++ {
L.elem[j] = L.elem[j + 1]
}
L.length--
return true
}
//链表按值查找
func LocationList(L *SeqList,x ElemType)bool{
var i int
for i = 1;i < L.length && L.elem[i] != x;{
i++
}
if i > L.length {
return false
}else {
return true
}
}
func main(){
}
主函数里没有填充内容,需要可以自己添加
仅供参考