go用链表实现线性表

```

package main

import "fmt"

type LinkedListstruct {

l *slist

length uint32

}

// 单向列表

type sliststruct {

valinterface{}

next *slist

}

/* 初始化一个空线性表*/

func (L *LinkedList) InitList(vinterface{}, length uint32) {

L.l = new(slist)

L.l.val = v

var n*slist = L.l

L.length = length

for i := uint32(1);i < L.length; i +=1 {

n.next = new(slist)

n.next.val = v

n = n.next

}

// 最末尾的一个节点指向空

  n.next = nil

}

/* 销毁线性表*/

func (L *LinkedList) DeleteList() {

L.length =0

  L.l = nil

}

/* 将线性表置为空表*/

func (L *LinkedList) ClearList() {

L.DeleteList()

}

/* 返回线性表的长度*/

func (L *LinkedList) ListLength() uint32 {

return uint32(L.length)

}

/* 返回第i个数据元素*/

func (L *LinkedList) GetElem(e uint32)interface{} {

if e > L.length {

e = L.length

}

p := L.l

for i := uint32(1);i < e;i +=1 {

p = p.next

}

return p.val

}

/* 返回L中第1个与e满足compare函数的数据元素的位序.若这样的数据元素不存在,则返回0 */

func (L *LinkedList) LocateElem(vinterface{}, ffunc(a ...interface{}) bool) uint32 {

w := uint32(0)

var p*slist= L.l

for i := uint32(0);i < L.length;i +=1 {

if f(p.val, v) ==true {

w = i

break

      }

p = p.next

}

return w

}

/* 返回要查找值的前驱,如果没有前驱,则返回空*/

func (L *LinkedList) PriorElem(vinterface{})interface{} {

w := uint32(0)

var p*slist = L.l

var front*slist = L.l

for i := uint32(0);i < L.length;i +=1 {

if v == p.val {

w = i +1

        break

      }

front = p

p = p.next

}

if w ==0 || w ==1 {

return nil

}else {

return front.val

}

}

/* 返回要查找值的后驱,如果没有后驱,则返回空*/

func (L *LinkedList) NextElem(vinterface{})interface{} {

var p*slist = L.l

var next*slist = L.l.next

for next != nil {

if v == p.val {

break

      }

p = next

next = p.next

}

if next == nil {

return nil

}else {

return next.val

}

}

/* 在线性表中的第i个位置插入一个数据*/

func (L *LinkedList) ListInsert(i uint32, vinterface{})  {

// 线性为空

  if L.length ==0 {

L.l = new(slist)

L.l.val = v

L.l.next = nil

L.length =1

      return

  }

p := &slist{val: v}

var front*slist = L.l

var next*slist = L.l

if i <=1 {

p.next = L.l

L.l = p

}else if i > L.length {

for front.next != nil {

front = front.next

}

front.next = p

p.next = nil

}else {

for j := uint32(2);j < i;j +=1 {

front = front.next

}

next = front.next

front.next = p

p.next = next

}

L.length +=1

  p = L.l

for p.next != nil {

fmt.Print(p.next," ")

p = p.next

}

}

/* 删除第i个值,线性表长度减一,并用e返回其值*/

func (L *LinkedList) ListDelete(i uint32)interface{} {

if L.length ==0 {

return nil

}

var resinterface{}

if i > L.length {

i = L.length

}

if i <=1 {

res = L.l.val

L.l = L.l.next

}else {

var front *slist

var next *slist

front, next = L.l, L.l.next

for j := uint32(2);j < i;j +=1 {

front = next

next = next.next

}

res = next.val

front.next = next.next

}

L.length -=1

  return res

}

/* 修改第i个值*/

func (L *LinkedList) ListChange(i uint32, vinterface{}) {

if i <=1 {

L.l.val = v

return

  }

if i > L.length {

i = L.length

}

var p*slist = L.l

for j := uint32(1);j < i;j +=1 {

p = p.next

}

p.val = v

}

/* 对线性表的每一个函数都调用visit()函数*/

func (L *LinkedList) ListTraverse(ffunc(a ...interface{})) {

p := L.l

for p != nil {

f(p)

p = p.next

}

}

func (L *LinkedList) Printf() {

p := L.l

for p != nil {

fmt.Print(p," ")

p = p.next

}

fmt.Println()

}

func Print(v ...interface{}) {

fmt.Print(v[0].(*slist).val," ")

}

func main()  {

var l = LinkedList{}

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":

var length uint32

fmt.Print("输入线性表的长度: ");fmt.Scan(&length)

l.InitList("1", length);

case "2":l.ClearList();

case "3":l.DeleteList();

}

}

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(uint32(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)

        l.Printf()

}

}

}

```

你可能感兴趣的:(go用链表实现线性表)