闲来无事,更新一下,线性表的顺序存储,go语言版本,效果都已经测试过,下面给出各部分细节
文章目录
- 1、生成一个线性表
- 2、查找
- 3、插入
- 4、求长度
- 5、改值
- 6、删除
- 7、遍历
- 8、测试程序
- 9、完整代码
- 总结
package main
import "fmt"
const MAXSIZE = 100
type LNode *List
type ElementType int
type List struct {
Data []ElementType
Last int
}
type L List
1、生成一个线性表
func MakeEmpty() *List {
PtrL := &List{
Data: make([]ElementType, MAXSIZE), // 初始化 Data 切片
Last: -1,
}
return PtrL
}
2、查找
func Find(x ElementType, PtrL *List) int {
i := 0
for i <= PtrL.Last && PtrL.Data[i] != x {
i++
}
if i > PtrL.Last {
return -1
} else {
return i + 1
}
}
func FindKth(K int, PtrL List) ElementType {
if K < 1 || K > PtrL.Last+1 {
return -1
} else {
return PtrL.Data[K+1]
}
}
3、插入
func Insert(x ElementType, i int, PtrL *List) {
j := 0
if PtrL.Last == MAXSIZE-1 {
fmt.Printf("Data Full")
return
}
if i < 1 || i > PtrL.Last+2 {
fmt.Printf("Position abnormal")
return
}
for j = PtrL.Last; j >= i-1; j-- {
PtrL.Data[j+1] = PtrL.Data[j]
}
PtrL.Data[i-1] = x
PtrL.Last++
return
}
4、求长度
func Length(PtrL List) int {
return PtrL.Last
}
5、改值
func Alter(x ElementType, i int, PtrL *List) {
if i < 1 || i > PtrL.Last+1 {
fmt.Printf("No find element:%d", i)
return
}
PtrL.Data[i] = x
}
6、删除
func Delete(i int, PtrL *List) {
j := 0
if i < 1 || i > PtrL.Last+1 {
fmt.Printf("Not Find Element,")
return
}
for j = i; j <= PtrL.Last; j++ {
PtrL.Data[j-1] = PtrL.Data[j]
}
PtrL.Last--
return
}
7、遍历
func PrintL(PtrL *List) {
for i := 0; i <= PtrL.Last; i++ {
fmt.Printf(" %d", PtrL.Data[i])
}
fmt.Println()
}
8、测试程序
func main() {
L := MakeEmpty()
Insert(1, 1, L)
Insert(2, 2, L)
Insert(3, 3, L)
Insert(4, 4, L)
Insert(5, 5, L)
Insert(6, 6, L)
PrintL(L) //打印
Delete(2, L)
PrintL(L)
Alter(2, 2, L)
PrintL(L)
x := Find(2, L)
fmt.Println("result:", x)
FindKth(2, *L)
length := Length(*L)
fmt.Println("len:", length)
}
9、完整代码
package main
import "fmt"
const MAXSIZE = 100
type LNode *List
type ElementType int
type List struct {
Data []ElementType
Last int
}
type L List
func MakeEmpty() *List {
PtrL := &List{
Data: make([]ElementType, MAXSIZE), // 初始化 Data 切片
Last: -1,
}
return PtrL
}
func Find(x ElementType, PtrL *List) int {
i := 0
for i <= PtrL.Last && PtrL.Data[i] != x {
i++
}
if i > PtrL.Last {
return -1
} else {
return i + 1
}
}
func Insert(x ElementType, i int, PtrL *List) {
j := 0
if PtrL.Last == MAXSIZE-1 {
fmt.Printf("Data Full")
return
}
if i < 1 || i > PtrL.Last+2 {
fmt.Printf("Position abnormal")
return
}
for j = PtrL.Last; j >= i-1; j-- {
PtrL.Data[j+1] = PtrL.Data[j]
}
PtrL.Data[i-1] = x
PtrL.Last++
return
}
func Length(PtrL List) int {
return PtrL.Last
}
func Alter(x ElementType, i int, PtrL *List) {
if i < 1 || i > PtrL.Last+1 {
fmt.Printf("No find element:%d", i)
return
}
PtrL.Data[i] = x
}
func FindKth(K int, PtrL List) ElementType {
if K < 1 || K > PtrL.Last+1 {
return -1
} else {
return PtrL.Data[K+1]
}
}
func Delete(i int, PtrL *List) {
j := 0
if i < 1 || i > PtrL.Last+1 {
fmt.Printf("Not Find Element,")
return
}
for j = i; j <= PtrL.Last; j++ {
PtrL.Data[j-1] = PtrL.Data[j]
}
PtrL.Last--
return
}
func PrintL(PtrL *List) {
for i := 0; i <= PtrL.Last; i++ {
fmt.Printf(" %d", PtrL.Data[i])
}
fmt.Println()
}
func main() {
L := MakeEmpty()
Insert(1, 1, L)
Insert(2, 2, L)
Insert(3, 3, L)
Insert(4, 4, L)
Insert(5, 5, L)
Insert(6, 6, L)
PrintL(L) //打印
Delete(2, L)
PrintL(L)
Alter(2, 2, L)
PrintL(L)
x := Find(2, L)
fmt.Println("result:", x)
FindKth(2, *L)
length := Length(*L)
fmt.Println("len:", length)
}
总结
看着c语言敲的,只是对语法进行简单了解