571
社区成员
发帖
与我相关
我的任务
分享
type Node struct{
Value int
Next *Node
}
type List struct {
Length int
Head *Node
}
func Init()List{
var list List
list.Head=nil
list.Length=0
return list
}
func Insert(list *List,value int,location int)bool{
if location<0||location>list.Length{
return false
}
var node=new(Node)
node.Value=value
if location==0{
node.Next=list.Head
list.Head=node
list.Length++
return true
}
var p=list.Head
for i:=1;i<location;i++{
p=p.Next
}
node.Next=p.Next
p.Next=node
list.Length++
return true
}
func Delete(list *List,location int)bool{
if location<0||location>list.Length{
return false
}
if location==0 {
list.Length--
list.Head=list.Head.Next
return true
}
var node=list.Head
for i:=1;i<location;i++{
node=node.Next
}
if node.Next.Next==nil{
node.Next=nil
}else {
node.Next=node.Next.Next
}
list.Length--
return true
}
func Search(list *List,value int)*Node{
var node=list.Head
for i:=0;i<list.Length;i++{
if node.Value==value{
return node
}
node=node.Next
}
return nil
}
