闲来无事,更新一下,线性表的次序存储,go语言版本,结果都已经测试过,下面给出各部分细节
- 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语言敲的,只是对语法进行简单了解
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |