这篇文章主要给大家介绍了关于Kotlin中Stack与LinkedList实现的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考借鉴,
这篇文章主要给大家介绍了关于Kotlin中Stack与LinkedList实现的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
这篇文章主要给大家介绍了关于Kotlin中Stack与LinkedList实现的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
前言
本文主要介绍的是关于Kotlin 实现基本的数据结构 Stack 和 LinkedList,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。
Stack
Java中Stack由List实现,Kotlin中有MutableList,Stack类的基本定义如下,继承Iterator为了迭代遍历:
|
1
|
class Stack<T : Comparable<T>>(list : MutableList<T>) : Iterator<T> |
基本属性实现
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
// stack的count var itCounter: Int = 0// stack内部实现为MutableList var items: MutableList<T> = list// 判断stack是否为nullfun isEmpty(): Boolean = this.items.isEmpty()// 获取stack的items countefun count(): Int = this.items.count()// tostring操作 override fun toString(): String { return this.items.toString() } |
基本操作实现
|
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
|
// pop操作,弹出栈顶元素即链表最末端元素,可为null fun pop(): T? { if (this.isEmpty()) { return null } else { val item = this.items.count() - 1 return this.items.removeAt(item) }}// 只读操作,不弹出fun peek(): T? { if (isEmpty()) { return null } else { return this.items[this.items.count() - 1] }}// hasNext操作override fun hasNext(): Boolean { val hasNext = itCounter < count() if (!hasNext) itCounter = 0 return hasNext}// 取next元素override fun next(): T { if (hasNext()){ val topPos : Int = (count() - 1) - itCounter itCounter++ return this.items[topPos] }else{ throw NoSuchElementException("No such element") // 异常不用new哦 }} |
LinkedList
LinkedList的实现需要Node,然后实现first、last、count以及append等操作。
Node 定义
|
1
2
3
4
5
|
class Node<T>(value : T){ var value : T = value // value可以是任意类型 var next : Node<T>? = null // next可以为null var previous : Node<T>? = null // pre也可以为null} |
基本操作一
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
// 头结点,引导性作用var head : Node<T>?= null// 取决于head是否为nullvar isEmpty : Boolean = head == null// 获取firstfun first() : Node<T>? = head// 获取last结点,需要一直next才能到达last结点fun last() : Node<T>?{ var node = head if (node != null){ while (node?.next != null){ node = node?.next } return node }else{ return null }} |
基本操作二
|
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
|
// 获取count,同样通过next计算fun count():Int { var node = head if (node != null){ var counter = 1 while (node?.next != null){ node = node?.next counter += 1 } return counter } else { return 0 }}// append操作,在last结点上appendfun append(value : T){ var newNode = Node(value) // 获取当前节点的最后一个节点 var lastNode = this.last() if (lastNode != null){ newNode.previous = lastNode lastNode.next = newNode }else{ head = newNode }}// 删除操作fun removeNode(node : Node<T>) : T{ val prev = node.previous val next = node.next if (prev != null){ prev.next = next }else{ head = next } next?.previous = prev node.previous = null // 将断开的节点前后置null node.next = null return node.value // 返回删除节点的value} |
以上,用kotlin实现基本的数据结构stack和linkedlist.
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对米米素材网的支持。
原文链接:https://allenwu.itscoder.com/datastructure-in-kotlin
发表评论