golang学习|算法4(单链表的相关操作)

实现如下操作 【golang学习|算法4(单链表的相关操作)】1.判断是否为空的单链表
2.单链表的长度
3.获取头节点
4.从头部添加元素
5.从尾部添加元素
6.在指定位置添加元素
7.删除指定元素
8.删除指定位置的元素
9.判断是否包含某个元素
10.单链表的反转
linkedList包代码如下:

package linkedListimport "fmt"type Object interface{}type Node struct { Data Object Next *Node }type List struct { headNode *Node //头节点 }//判断是否为空的单链表 func (this *List) IsEmpty() bool { if this.headNode == nil { return true } else { return false } }//单链表的长度 func (this *List) Length() int { cur := this.headNode count := 0 for cur != nil { count++ cur = cur.Next } return count }//获取头部节点 func (this *List) GetHeadNode() *Node { return this.headNode }//从头部添加元素 func (this *List) Add(data Object) { node := &Node{Data: data} node.Next = this.headNode this.headNode = 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 { // fmt.Println("hello") pre := this.headNode count := 0 for count < (index - 1) { pre = pre.Next count++} //当循环退出后,pre指向index -1的位置 node := &Node{Data: data} node.Next = pre.Next pre.Next = node } }//删除指定元素 func (this *List) Remove(data Object) { pre := this.headNode if pre.Data =https://www.it610.com/article/= 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 (this *List) RemoveAtIndex(index int) { pre := this.headNode if index <= 0 { this.headNode = pre.Next } else if index>= this.Length() { fmt.Println("index out of range") //报错 err } else { count := 0 //index = 3 for count != (index-1) && pre.Next != nil { count++//2 pre = pre.Next //2 } pre.Next = pre.Next.Next } }//是否包含某个元素 func (this *List) Contain(data Object) bool { cur := this.headNode for cur != nil { if cur.Data =https://www.it610.com/article/= data { return true } cur = cur.Next } return false }//新建链表,采用头插法返转链表 func (this *List) ReverseList() List { pre := this.headNode list := List{} for pre != nil { list.Add(pre.Data) pre = pre.Next } return list }

测试代码在main.go中如下
package mainimport ( "fmt" "test/linkedList/linkedList" )func main() { list := linkedList.List{} //1.往单链表末尾追加元素2, 3, 4, 5 list.Append(1) list.Append(2) list.Append(3) list.Append(4) //2.从头部添加元素head_node list.Add("head_node") fmt.Println("长度======", list.Length()) //3.判断是否为空链表 bool := list.IsEmpty() fmt.Println(bool) //4.在指定位置2插入元素 2indexValue list.Insert(2, "2_index_value") travselLinkList(&list) //5.是否包含元素2_index_value isContain := list.Contain("2_index_value") fmt.Println("isContain[2_index_value]:", isContain) //6.删除元素2_index_value list.Remove("2_index_value") travselLinkList(&list) //7.从位置2删除元素 list.RemoveAtIndex(2) //8.单链表反转 listReverse := list.ReverseList() fmt.Println("长度======", list.Length()) travselLinkList(&list) travselLinkList(&listReverse)}func travselLinkList(list *linkedList.List) { //遍历 head := list.GetHeadNode() for head != nil { fmt.Println(head.Data) head = head.Next } fmt.Println("--------------------") }

    推荐阅读