Skip to content

Commit 55e2ece

Browse files
committed
约瑟夫环问题解决
1 parent cacef1d commit 55e2ece

File tree

3 files changed

+84
-54
lines changed

3 files changed

+84
-54
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* 循环链表:无头节点
3+
*/
4+
5+
package list
6+
7+
import (
8+
"fmt"
9+
)
10+
11+
// 节点对象,存储循环链表上某个节点数据
12+
type node struct {
13+
Data interface{} // 数据域
14+
Next *node // 指针域
15+
}
16+
17+
// 循环链表对象:存储头指针与长度
18+
type NHRingList struct {
19+
Head *node
20+
Length int
21+
}
22+
23+
// 创建循环链表
24+
func New() *NHRingList {
25+
head := &node{nil, nil} // 头节点存储链表中元素的个数
26+
// head.Next = head // 循环指向自己
27+
return &NHRingList{
28+
head,
29+
0,
30+
}
31+
}
32+
33+
// 增加:末尾添加
34+
func (rl *NHRingList) Append(data interface{}){
35+
36+
insertNode := &node{data, rl.Head} // 要插入的节点
37+
38+
if rl.Length == 0 { // 第一次添加
39+
rl.Head = insertNode
40+
rl.Head.Next = insertNode
41+
} else { // 不是第一次添加
42+
tailNode := rl.Head
43+
for tailNode.Next != rl.Head {
44+
tailNode = tailNode.Next // 找到最后一个节点
45+
}
46+
tailNode.Next = insertNode
47+
}
48+
49+
rl.Length++
50+
return
51+
}
52+
53+
// 打印链表
54+
func (rl *NHRingList) Show() {
55+
56+
if rl.Length == 0 {
57+
fmt.Println("list is empty")
58+
return
59+
}
60+
61+
currentNode := rl.Head
62+
for i := 0; i <= rl.Length - 1; i++ {
63+
fmt.Print(currentNode.Data)
64+
if i == rl.Length - 1 {
65+
fmt.Print(" \n")
66+
} else {
67+
fmt.Print(" ")
68+
}
69+
70+
currentNode = currentNode.Next
71+
}
72+
}

sources/go/list/RingList/RingList.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,18 @@
192192
}
193193

194194
// 获取头结点
195-
func (rl *RingList)GetHead() *node{
195+
func (rl *RingList)GetHead() *node {
196196
return rl.head
197197
}
198198

199-
// 获取下一个节点
200-
func (rl *RingList)GetNext(n *node) *node {
201-
return n.next
199+
// 获取尾节点
200+
func (rl *RingList)GetTail() *node {
201+
if rl.Length() == 0 {
202+
return nil
203+
}
204+
currentNode := rl.GetHead()
205+
for currentNode.next != rl.GetHead() {
206+
currentNode = currentNode.next
207+
}
208+
return currentNode
202209
}

sources/go/main.go

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
SequenList "algorithm/list/SequenList"
66
LinkedList "algorithm/list/LinkedList"
77
RingList "algorithm/list/RingList"
8+
NHRingList "algorithm/list/NHRingList"
89
DoublyList "algorithm/list/DoublyList"
910
"fmt"
1011
)
@@ -98,54 +99,6 @@ func testDoublyList() {
9899
dl.Show()
99100
}
100101

101-
// 约瑟夫环
102-
func testJosephus(){
103-
104-
// 生成循环链表
105-
rl := RingList.New()
106-
for i := 0; i < 41; i++ { // 多个人组成的循环链表
107-
rl.Append(i + 1) // 每个人都对应自己的原始编号
108-
}
109-
fmt.Print("生成的循环链表为:")
110-
rl.Show()
111-
112-
// 每次读取3个元素,读取位置从startNode开始
113-
startNode := rl.GetHead() // 起始开始报数的元素
114-
delIndex := 0 // 要删除元素的索引
115-
116-
num := 0
117-
for {
118-
119-
for i:= 1; i <= 3; i++ {
120-
121-
if rl.GetNext(startNode) == rl.GetHead() || startNode == rl.GetHead() {
122-
delIndex = 1 // 每次从头结点开始时,删除索引变为0,重新记录
123-
num = 0
124-
startNode = rl.GetNext(rl.GetNext(startNode))
125-
} else {
126-
startNode = rl.GetNext(startNode)
127-
delIndex ++
128-
}
129-
130-
}
131-
132-
133-
// 删除该元素
134-
val, _ := rl.Node(delIndex - num)
135-
fmt.Println("删除数据为:", delIndex - num, " ", val)
136-
rl.Delete(delIndex)
137-
num ++
138-
139-
// 如果只有一个元素
140-
if rl.Length() < 3 {
141-
break;
142-
}
143-
}
144-
145-
fmt.Print("最终的循环链表为:")
146-
rl.Show()
147-
return
148-
}
149102

150103
func main() {
151104

@@ -159,8 +112,6 @@ func main() {
159112

160113
// testRingList() // 测试循环链表
161114

162-
testJosephus() // 测试约瑟夫环
163-
164115
// testDoublyList() // 测试双向链表
165116

166117
}

0 commit comments

Comments
 (0)