Golang单向链表实现
package main
type Object interface {}
type Node struct {
Data Object
Next *Node
}
type SingleLink struct {
HeadNode *Node
}
func NewSingleLink() *SingleLink {
return &SingleLink{}
}
func (s *SingleLink) IsEmpty() bool {
if s.HeadNode == nil {
return true
}
return false
}
func (s *SingleLink) Length() int {
cur := s.HeadNode
count := 0
for cur != nil {
count ++
cur = cur.Next
}
return count
}
func (s *SingleLink) PushFront(data Data) *Node {
node := &Node{Data: data,}
node.Next = s.HeadNode
s.HeadNode = node
return node
}
func (s *SingleLink) PushBack(data Data) *Node {
node := &Node{Data: data}
if s.IsEmpty() {
s.HeadNode = node
}else{
cur := s.HeadNode
for cur.Next != nil {
cur = cur.Next
}
cur.Next = node
}
return node
}