-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircular_linked_list.go
50 lines (44 loc) · 1.01 KB
/
circular_linked_list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package linkedlist
import (
"fmt"
"strings"
)
// CircularLinkedList 环形链表的实现
type CircularLinkedList struct {
*SingleLinkedList
}
// NewCll 返回一个空的环形链表
func NewCll() *CircularLinkedList {
l := &CircularLinkedList{
SingleLinkedList: NewSll(),
}
l.dummyHead.next = l.dummyHead
return l
}
func (l *CircularLinkedList) Delete(data interface{}) {
for pre := l.dummyHead; pre.next != l.dummyHead; pre = pre.next {
cur := pre.next
if cur.data == data {
pre.next = cur.next
cur.next = nil
l.size--
}
}
}
func (l *CircularLinkedList) Contains(data interface{}) bool {
for cur := l.dummyHead.next; cur != l.dummyHead; cur = cur.next {
if cur.data == data {
return true
}
}
return false
}
func (l *CircularLinkedList) String() string {
var builder strings.Builder
builder.WriteString("Head: ")
for cur := l.dummyHead.next; cur != l.dummyHead; cur = cur.next {
builder.WriteString(fmt.Sprintf("%v-->", cur.data))
}
builder.WriteString("Head")
return builder.String()
}