update code format

This commit is contained in:
Zackratos 2019-06-19 01:27:35 +08:00
parent c9097e3181
commit cb1e9c1a8a

View File

@ -5,32 +5,32 @@
*/ */
class StackBasedOnLinkedList { class StackBasedOnLinkedList {
private var top: Node? = null
private var top: Node? = null fun push(value: Int) {
val newNode = Node(value, null)
// 不管 top 是不是 null都可以这么写
newNode.next = top
top = newNode
}
fun push(value: Int) { fun pop(): Int {
val newNode = Node(value, null) if (top == null) return -1
// 不管 top 是不是 null都可以这么写 val node = top
newNode.next = top top = top!!.next
top = newNode return node!!.data
} }
fun pop(): Int { fun printAll() {
if (top == null) return -1 var p = top
val node = top while (p != null) {
top = top!!.next print("${p.data} ")
return node!!.data p = p.next
} }
println()
fun printAll() { }
var p = top
while(p != null) {
print("${p.data} ")
p = p.next
}
println()
}
class Node(var data: Int, var next: Node?) class Node(var data: Int, var next: Node?)
} }