package main
import (
"fmt"
"log"
)
type Node struct {
H *Node
E *Node
Value string
}
func MakeHead() *Node {
return &Node{nil, nil, ""}
}
func MakeNode(value string) *Node {
return &Node{nil, nil, value}
}
func (node *Node) add(n *Node) {
for node.E != nil {
node = node.E
}
if node.E == nil {
node.E = n
}
}
func (node *Node) print() {
for node.E != nil {
fmt.Printf("%s", node.Value)
node = node.E
}
fmt.Printf("%s\n", node.Value)
}
func (node *Node) hasMergeElemeent(n *Node) bool {
orgN := n
for node != nil {
n = orgN
for n != nil {
log.Println(node.Value, n.Value)
if node.Value == n.Value {
return true
} else {
n = n.E
}
}
node = node.E
}
return false
}
func main() {
first := MakeHead()
first.Value = "z"
first.add(MakeNode("y"))
first.add(MakeNode("x"))
first.add(MakeNode("b"))
first.add(MakeNode("a1"))
first.print()
second := MakeHead()
second.Value = "z1"
second.add(MakeNode("y"))
second.add(MakeNode("x1"))
second.add(MakeNode("f1"))
second.add(MakeNode("a"))
second.add(MakeNode("d1"))
second.add(MakeNode("b1"))
second.print()
has := first.hasMergeElemeent(second)
log.Println("hasMergeElement:", has)
}
评论