上一篇写了如何来生成链表,这一篇介绍链表的基本操作。
1.求表长
表长就是链表中结点的个数
type Teacher struct{ //定义链表类型 Name string Age int Next *Teacher } func printList(h *Teacher){ //循环打印每个元素 for h != nil{ fmt.Printf("Name:%v Age:%v ",h.Name,h.Age) h = h.Next } } func createInHeader(h *Teacher, name string, age int) (*Teacher) { p := &Teacher{} p.Age = age p.Name = name p.Next = h return p } func LenLinkList(h *Teacher) int {
//求表长 var cnt int if h == nil{ //如果h的值为空则cnt为0,否则为1, return cnt }else{ cnt=1 } for h.Next != nil{ h= h.Next //每后移一个结点,cnt加1 cnt++ } return cnt } func testCreateInHeader() { var header *Teacher header = createInHeader(header, "a", 18) header = createInHeader(header, "b", 19) header = createInHeader(header, "c", 20) printList(header) cnt := LenLinkList(header) fmt.Printf("链表的长度是:%d ",cnt) } //输出结果: Name:c Age:20 Name:b Age:19 Name:a Age:18 链表的长度是:3
2.读表元素
通常给定一个序号i,查找表的第i个元素。在链表中,任何相邻的两个结点通过一个指针相连,一个结点的位置包含在前边结点的next域中。所以,必须从头指针出发,一直往后移动,直到第i个结点
type Teacher struct{ Name string Age int Next *Teacher } func createInHeader(h *Teacher, name string, age int) (*Teacher) { p := &Teacher{} p.Age = age p.Name = name p.Next = h return p } func printList(h *Teacher){ for h != nil{ fmt.Printf("Name:%v Age:%v ",h.Name,h.Age) h = h.Next } } func GetLinkList(h *Teacher,i int)*Teacher{ //读表元素 c:= 1 for c<i && h != nil{//当未到第i结点且未到尾结点时继续后移 h = h.Next c++ } if i==c{ //找到第i个结点 return h }else{ return nil //i<1或i>n,i值不合法,查找失败 } } func testCreateInHeader() { var header *Teacher header = createInHeader(header, "a", 18) header = createInHeader(header, "b", 19) header = createInHeader(header, "c", 20) printList(header) cnt := LenLinkList(header) fmt.Printf("链表的长度是:%d ",cnt) fmt.Printf("链表的第二个元素是:%v ",*(GetLinkList(header,2)))//读表元素 fmt.Printf("链表的第三个元素是:%v ",*(GetLinkList(header,3))) // fmt.Printf("链表的第四个元素是:%v ",*(GetLinkList(header,4)))//panic } //运行结果 Name:c Age:20 Name:b Age:19 Name:a Age:18 链表的长度是:3 链表的第二个元素是:{b 19 0xc0420023e0} 链表的第三个元素是:{a 18 <nil>}
3.定位
定位运算,就是给定表元素的值,找出这个元素的位置,若找到返回元素是链表的第几个结点,未找到返回0
func LocateLinkList(h *Teacher,name string, age int) int{ i:=0 for h !=nil && (h.Name != name || h.Age != age){ i++ h = h.Next } if h != nil{ return i+1 }else{ return 0 } }
4.插入
插入运算是将给定值的元素插入第i个结点之前。先找到链表的第i-1个结点q,然后,生成一个新结点p,p的指针指向q的后一个结点,q指向p,这样完成插入运算。
type Teacher struct{ Name string Age int Next *Teacher } func createInHeader(h *Teacher, name string, age int) (*Teacher) { p := &Teacher{} p.Age = age p.Name = name p.Next = h return p } func printList(h *Teacher){ for h != nil{ fmt.Printf("Name:%v Age:%v ",h.Name,h.Age) h = h.Next } } func GetLinkList(h *Teacher,i int)*Teacher{ //读表元素 c:= 1 for c<i && h != nil{//当未到第i结点且未到尾结点时继续后移 h = h.Next c++ } if i==c{ //找到第i个结点 return h }else{ return nil //i<1或i>n,i值不合法,查找失败 } } func InserLinkList(h *Teacher,name string,age int,i int){ var q *Teacher var p *Teacher if i ==1 { q = h }else{ q = GetLinkList(h,i-1)//查找第i-1个元素结点 } if q == nil{ //第i-1个结点不存在 return }else{ p = new(Teacher) //生成新的结点 p.Name = name p.Age = age p.Next = q.Next //新结点的指针指向第i-1个结点的指针指向的值 q.Next = p //第i-1个结点指向新生成的结点 } } func testCreateInHeader() { var header *Teacher header = createInHeader(header, "a", 18) header = createInHeader(header, "b", 19) header = createInHeader(header, "c", 20) printList(header) InserLinkList(header,"d",21,2) fmt.Println("插入之后") printList(header) } //运行结果 Name:c Age:20 Name:b Age:19 Name:a Age:18 插入之后 Name:c Age:20 Name:d Age:21 Name:b Age:19 Name:a Age:18