package main
import "fmt"
type Node struct {
data interface{}
next *Node
}
func CreateList() *Node {
a := new(Node)
//a.data = 1
a.next = nil
return a
}
func CreateNode(data interface{}) *Node {
a := new(Node)
a.data = data
a.next = nil
return a
}
func PrintList(node *Node) {
for {
fmt.Println(node.data)
if node.next == nil {
break
}
node = node.next
}
}
func InsertList(headNode *Node, data interface{}) {
a := CreateNode(data)
a.next = headNode.next
headNode.next = a
}
func SearchData(node *Node, data interface{}) *Node {
for {
fmt.Println(node.data)
if node.data == data {
fmt.Println("存在数据:", data)
return node
}
if node.next == nil {
fmt.Println("不存在数据:", data)
return nil
//break
}
node = node.next
}
//return false
}
func SearchPreData(node *Node, data interface{}) *Node {
a := new(Node)
for {
fmt.Println(node.data)
if node.data == data {
fmt.Println("存在数据:", data)
break
}
if node.next == nil {
fmt.Println("不存在数据:", data)
return nil
//break
}
a = node
node = node.next
}
return a
}
func DeleteData(node *Node, data interface{}) bool {
a := SearchPreData(node, data)
if a == nil {
fmt.Println("删除失败,数据不存在:", data)
return false
}
b := a.next
a.next = b.next
fmt.Println("删除成功:", data)
b = nil
return true
}
func main() {
head := CreateList()
head.next = CreateNode(12)
InsertList(head, 232)
InsertList(head, 555)
PrintList(head)
//fmt.Println(SearchData(head, 22))
fmt.Println(DeleteData(head, 232))
fmt.Println("删除后:")
PrintList(head)
}