Golang 中的 ring.Link()函数用于连接两个环 r 和环 s ring.next()函数将环 r 的最后一个节点连接到环 s 的第一个节点。因此 r.next() 不能为空。否则,它会抛出错误。它的工作原理类似于循环链表。
句法:
func (r *Ring) Link(s *Ring) *Ring
它不返回任何东西。
示例 1:
// Golang program to illustrate
// the ring.Link() function
package main
import (
"container/ring"
"fmt"
)
// Main function
func main() {
// Create two rings, a and b, of size 2
a := ring.New(4)
b := ring.New(4)
// Get the length of the ring
m := a.Len()
n := b.Len()
// Initialize a with 0s
for j := 0; j < m; j++ {
a.Value = 0
a = a.Next()
}
// Initialize b with 1s
for i := 0; i < n; i++ {
b.Value = 1
b = b.Next()
}
// Link ring a and ring b
ab := a.Link(b)
ab.Do(func(p interface{}) {
fmt.Println(p.(int))
})
}
输出:
0
0
0
0
1
1
1
1
示例 2:
// Golang program to illustrate
// the ring.Link() function
package main
import (
"container/ring"
"fmt"
)
// Main function
func main() {
// Create two rings, r and s, of size 2
r := ring.New(2)
s := ring.New(2)
// Get the length of the ring
lr := r.Len()
ls := s.Len()
// Initialize r with "GFG"
for i := 0; i < lr; i++ {
r.Value = "GFG"
r = r.Next()
}
// Initialize s with "COURSE"
for j := 0; j < ls; j++ {
s.Value = "COURSE"
s = s.Next()
}
// Link ring r and ring s
rs := r.Link(s)
// Iterate through the combined
// ring and print its contents
rs.Do(func(p interface{}) {
fmt.Println(p.(string))
})
}
输出:
GFG
GFG
COURSE
COURSE