golang实现单链表

  1 package main
  2 
  3 import "fmt"
  4 
  5 type Object interface{}
  6 
  7 type Node struct {
  8     data Object
  9     next *Node
 10 }
 11 
 12 type List struct {
 13     headNode *Node
 14 }
 15 
 16 func NewNode(data Object, next *Node) *Node {
 17     return &Node{data, next}
 18 }
 19 
 20 func (list *List) IsEmpty() bool {
 21     return list.headNode == nil
 22 }
 23 
 24 func (list *List) Add(node *Node) *List {
 25     headNode := list.headNode  
 26     if headNode.next != nil {
 27         node.next = headNode.next 
 28     } 
 29     headNode.next = node
 30     return list
 31 }
 32 
 33 func (list *List) Append(node *Node) *List {
 34     if list.IsEmpty() {
 35         list.headNode = node
 36         return list
 37     }
 38     curNode := list.headNode
 39     for curNode.next != nil {
 40         curNode = curNode.next
 41     }
 42     curNode.next = node
 43     return list
 44 }
 45 
 46 func (list *List) Insert(index int, data Object) {
 47     if (index >= 0 && index < list.Length()) {
 48         count := 0
 49         if !list.IsEmpty() {
 50             curNode := list.headNode
 51             for curNode != nil && count < index {
 52                 count++
 53                 curNode = curNode.next
 54             }
 55             node := NewNode(data, curNode.next)
 56             curNode.next = node
 57         }
 58     } 
 59 }
 60 
 61 func (list *List) Remove(index int) {
 62     if (index >= 0 && index < list.Length()) {
 63         count := 0
 64         if !list.IsEmpty() {
 65             curNode := list.headNode
 66             for curNode != nil && count < index-1 {
 67                 count++
 68                 curNode = curNode.next
 69             }
 70             curNode.next = curNode.next.next
 71         }
 72     } 
 73 }
 74 
 75 func PrintList(list *List) {
 76     cur := list.headNode 
 77     for cur != nil {
 78         fmt.Println(cur.data)
 79         cur = cur.next
 80     }
 81 }
 82 
 83 func (list *List) Length() int {
 84     var length int
 85     curNode := list.headNode
 86     for curNode != nil {
 87         length++
 88         curNode = curNode.next
 89     }
 90     return length
 91 }
 92 
 93 func ReverseList(head *Node) *Node {
 94     cur := head
 95     var pre *Node = nil
 96     for cur != nil {
 97         pre, cur, cur.next = cur, cur.next, pre
 98     }
 99     return cur
100 }
101 
102 func main(){
103     fmt.Println("NewNode======================")
104     node := NewNode(1, nil)
105     fmt.Println(node.data)
106     list := &List{node}
107     PrintList(list)
108     node2 := NewNode("a", nil)
109     list.Append(node2)
110     fmt.Println("Add======================")
111     PrintList(list)
112     node1 := NewNode(2, nil)
113     list.Add(node1)
114     fmt.Println("Length======================")
115     PrintList(list)
116     fmt.Println(list.Length())
117     fmt.Println("Insert======================")
118     list.Insert(1, 4)
119     PrintList(list)
120     fmt.Println("Remove======================")
121     list.Remove(2)
122     PrintList(list)
123     fmt.Println("ReverseList======================")
124     ReverseList(node)
125     PrintList(list)
126 }

猜你喜欢

转载自www.cnblogs.com/zhengze/p/12298659.html