Go语言实现链表的相关操作

package main

import (
    "fmt"
)

type Object interface{}
type Node struct {
    data Object //定义数据域
    next *Node   //定义地址域(指向下一个表的地址)
}
type List struct{
    headNode *Node //头结点

}
//判断是否为空的单链表
func (this *List) IsEmpty() bool{//创建IsEmpty方法,饭会bool类型。
    if this.headNode==nil{//判断单链表为空,只需要判断头节点为空即可。
        return true
    } else {
        return false
    }
}
//获取列表长度
func (this *List) Length() int{
    //获取链表头结点
    cur := this.headNode
    //定义一个计数器,初始值为0
    count :=0
    for cur!=nil{
        //如果头节点不为空,则count++
        count++
    }
    return count
}
//从链表头部添加元素
func (this *List) Add(data Object) *Node{
    node :=&Node{data: data}
    node.next=this.headNode
    this.headNode=node
    return node
}
//从链表尾部添加
func (this *List) Append(data Object){
    node:=&Node{data: data}
    if this.IsEmpty() {
        this.headNode=node
    }else{
        cur:=this.headNode
        for cur.next !=nil{
            cur=cur.next
        }
        cur.next=node
    }
}
//在链表中指定位置添加元素
func (this *List) Insert(index int,data Object) {
    if index<0{
        this.Add(data)
    }else if index> this.Length(){
        this.Append(data)
    }else{
        pre :=this.headNode
        count :=0
        for count<(index - 1){
            pre=pre.next
            count++
        }
        node:=&Node{data: data}
        node.next=pre.next
        pre.next=node
    }
}
//遍历链表中的所有结点
func (this *List) ShowList(){
    if !this.IsEmpty(){
        cur :=this.headNode
        for {
            fmt.Printf("\t%v",cur.data)
            if cur.next!=nil{
                cur=cur.next
            }else {
                break
            }
        }
    }
}
//查看链表中是否包含某个元素
func (this *List) Contain(data Object) bool{
    cur :=this.headNode
    for cur!=nil{
        if cur.data==data{
            return true
        }
        cur=cur.next
    }
    return false
}
// Go中的for 语句不一样
//删除指定位置的元素
func (this *List) RemoveAtIndex(index int){
    pre:=this.headNode
    if index<=0{//如何index为0或者小于0,那么删除头结点
        this.headNode=pre.next

    }else if index>this.Length(){
        fmt.Printf("超出链表长度")
        return
    }else {
        count:=0
        for count!=(index-1)&&pre.next!=nil{
            count++
            pre=pre.next
        }
        pre.next=pre.next.next
    }
}
//删除指定位置元素
func (this *List) Remove(data Object){
    pre :=this.headNode
    if pre.data==data{
        this.headNode=pre.next
    }else{
        for pre.next!=nil{
            if pre.next.data==data{
                pre.next=pre.next.next
            }else{
                pre=pre.next
            }
        }
    }
}
//func deletenode(head *listNode,val int)*ListNode {
//    if head == nil {
//        return head
//    }
//    return nil
//}
func main()  {
    list :=List{}
    list.Append(1)
    list.Append(2)
    list.Append(3)
    list.Append(4)
    list.Append(5)
    list.Append(6)
    list.Append(7)
    list.Append(8)
    //fmt.Print("链表%d\n",list.Length())
    fmt.Print("链表List当前值为:")
    list.ShowList()
    t:=list.Length()
    fmt.Print(t)
}

猜你喜欢

转载自blog.csdn.net/zhuiyunzhugang/article/details/109740208