Skip to content

Commit 7eb6ee9

Browse files
committed
06_linkedlist kotlin version
1 parent 9c6fdc7 commit 7eb6ee9

File tree

1 file changed

+293
-0
lines changed

1 file changed

+293
-0
lines changed
Lines changed: 293 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,293 @@
1+
/**
2+
* 1)单链表的插入、删除、查找操作;
3+
* 2)链表中存储的是int类型的数据;
4+
*
5+
* Author:Zackratos
6+
*/
7+
8+
class SinglyLinkedList {
9+
10+
private var head: Node? = null
11+
12+
companion object {
13+
@JvmStatic
14+
fun main(args: Array<String>) {
15+
16+
val link = SinglyLinkedList()
17+
println("hello")
18+
val data = intArrayOf(1, 2, 5, 3, 1)
19+
20+
for (i in data.indices) {
21+
//link.insertToHead(data[i]);
22+
link.insertTail(data[i])
23+
}
24+
25+
println("打印原始:")
26+
link.printAll()
27+
if (link.palindrome()) {
28+
println("回文")
29+
} else {
30+
println("不是回文")
31+
}
32+
}
33+
}
34+
35+
fun findByValue(value: Int): Node? {
36+
var p = head
37+
while (p != null && p.data != value) {
38+
p = p.next
39+
}
40+
41+
return p
42+
}
43+
44+
fun findByIndex(index: Int): Node? {
45+
var p = head
46+
var pos = 0
47+
while (p != null && pos != index) {
48+
p = p.next
49+
++pos
50+
}
51+
52+
return p
53+
}
54+
55+
//无头结点
56+
//表头部插入
57+
//这种操作将于输入的顺序相反,逆序
58+
fun insertToHead(value: Int) {
59+
val newNode = Node(value, null)
60+
insertToHead(newNode)
61+
}
62+
63+
fun insertToHead(newNode: Node) {
64+
if (head == null) {
65+
head = newNode
66+
} else {
67+
newNode.next = head
68+
head = newNode
69+
}
70+
}
71+
72+
//顺序插入
73+
//链表尾部插入
74+
fun insertTail(value: Int) {
75+
76+
val newNode = Node(value, null)
77+
//空链表,可以插入新节点作为head,也可以不操作
78+
if (head == null) {
79+
head = newNode
80+
81+
} else {
82+
var q = head
83+
while (q?.next != null) {
84+
q = q.next
85+
}
86+
newNode.next = q?.next
87+
q?.next = newNode
88+
}
89+
}
90+
91+
fun insertAfter(p: Node?, value: Int) {
92+
val newNode = Node(value, null)
93+
insertAfter(p, newNode)
94+
}
95+
96+
fun insertAfter(p: Node?, newNode: Node) {
97+
if (p == null) return
98+
99+
newNode.next = p.next
100+
p.next = newNode
101+
}
102+
103+
fun insertBefore(p: Node?, value: Int) {
104+
val newNode = Node(value, null)
105+
insertBefore(p, newNode)
106+
}
107+
108+
fun insertBefore(p: Node?, newNode: Node) {
109+
if (p == null) return
110+
if (head === p) {
111+
insertToHead(newNode)
112+
return
113+
}
114+
115+
var q = head
116+
while (q != null && q.next !== p) {
117+
q = q.next
118+
}
119+
120+
if (q == null) {
121+
return
122+
}
123+
124+
newNode.next = p
125+
q.next = newNode
126+
127+
}
128+
129+
fun deleteByNode(p: Node?) {
130+
if (p == null || head == null) return
131+
132+
if (p === head) {
133+
head = head?.next
134+
return
135+
}
136+
137+
var q = head
138+
while (q != null && q.next !== p) {
139+
q = q.next
140+
}
141+
142+
if (q == null) {
143+
return
144+
}
145+
146+
q.next = q.next?.next
147+
}
148+
149+
fun deleteByValue(value: Int) {
150+
if (head == null) return
151+
152+
var p = head
153+
var q: Node? = null
154+
while (p != null && p.data != value) {
155+
q = p
156+
p = p.next
157+
}
158+
159+
if (p == null) return
160+
161+
if (q == null) {
162+
head = head?.next
163+
} else {
164+
q.next = q.next?.next
165+
}
166+
}
167+
168+
fun printAll() {
169+
var p = head
170+
while (p != null) {
171+
print("${p.data} ")
172+
p = p.next
173+
}
174+
println()
175+
}
176+
177+
//判断true or false
178+
fun TFResult(left: Node?, right: Node?): Boolean {
179+
var l: Node? = left
180+
var r: Node? = right
181+
182+
println("left_:${l?.data}")
183+
println("right_:${r?.data}")
184+
while (l != null && r != null) {
185+
if (l.data == r.data) {
186+
l = l.next
187+
r = r.next
188+
continue
189+
} else {
190+
break
191+
}
192+
193+
}
194+
195+
println("什么结果")
196+
return l == null && r == null
197+
}
198+
199+
// 判断是否为回文
200+
fun palindrome(): Boolean {
201+
if (head == null) {
202+
return false
203+
} else {
204+
println("开始执行找到中间节点")
205+
var p = head
206+
var q = head
207+
if (p?.next == null) {
208+
println("只有一个元素")
209+
return true
210+
}
211+
while (q?.next != null && q.next?.next != null) {
212+
p = p?.next
213+
q = q.next?.next
214+
}
215+
216+
println("中间节点${p?.data}")
217+
println("开始执行奇数节点的回文判断")
218+
val leftLink: Node?
219+
val rightLink: Node?
220+
if (q?.next == null) {
221+
// p 一定为整个链表的中点,且节点数目为奇数
222+
rightLink = p?.next
223+
leftLink = inverseLinkList(p)?.next
224+
println("左边第一个节点${leftLink?.data}")
225+
println("右边第一个节点${rightLink?.data}")
226+
227+
} else {
228+
//p q 均为中点
229+
rightLink = p?.next
230+
leftLink = inverseLinkList(p)
231+
}
232+
return TFResult(leftLink, rightLink)
233+
234+
}
235+
}
236+
237+
//带结点的链表翻转
238+
fun inverseLinkList_head(p: Node): Node {
239+
// Head 为新建的一个头结点
240+
val Head = Node(9999, null)
241+
// p 为原来整个链表的头结点,现在Head指向 整个链表
242+
Head.next = p
243+
/*
244+
带头结点的链表翻转等价于
245+
从第二个元素开始重新头插法建立链表
246+
*/
247+
var Cur = p.next
248+
p.next = null
249+
var next: Node?
250+
251+
while (Cur != null) {
252+
next = Cur.next
253+
Cur.next = Head.next
254+
Head.next = Cur
255+
println("first " + Head.data)
256+
257+
Cur = next
258+
}
259+
260+
// 返回左半部分的中点之前的那个节点
261+
// 从此处开始同步像两边比较
262+
return Head
263+
264+
}
265+
266+
//无头结点的链表翻转
267+
fun inverseLinkList(p: Node?): Node? {
268+
269+
var pre: Node? = null
270+
var r = head
271+
println("z---${r?.data}")
272+
var next: Node?
273+
while (r !== p) {
274+
next = r?.next
275+
276+
r?.next = pre
277+
pre = r
278+
r = next
279+
}
280+
281+
r?.next = pre
282+
// 返回左半部分的中点之前的那个节点
283+
// 从此处开始同步像两边比较
284+
return r
285+
286+
}
287+
288+
fun createNode(value: Int): Node = Node(value, null)
289+
290+
291+
292+
class Node(var data: Int, var next: Node?)
293+
}

0 commit comments

Comments
 (0)