溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Stack與LinkedList如何在Kotlin中實現(xiàn)

發(fā)布時間:2021-04-07 17:40:21 來源:億速云 閱讀:221 作者:Leah 欄目:移動開發(fā)

這篇文章將為大家詳細講解有關(guān)Stack與LinkedList如何在Kotlin中實現(xiàn),文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

Stack

Java中Stack由List實現(xiàn),Kotlin中有MutableList,Stack類的基本定義如下,繼承Iterator為了迭代遍歷:

class Stack<T : Comparable<T>>(list : MutableList<T>) : Iterator<T>

基本屬性實現(xiàn)

// stack的count
 var itCounter: Int = 0

// stack內(nèi)部實現(xiàn)為MutableList
 var items: MutableList<T> = list

// 判斷stack是否為null
fun isEmpty(): Boolean = this.items.isEmpty()

// 獲取stack的items counte
fun count(): Int = this.items.count()

// tostring操作
 override fun toString(): String {
  return this.items.toString()
 }

基本操作實現(xiàn)

// 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的實現(xiàn)需要Node,然后實現(xiàn)first、last、count以及append等操作。

Node 定義

class Node<T>(value : T){
 var value : T = value // value可以是任意類型
 var next : Node<T>? = null // next可以為null
 var previous : Node<T>? = null // pre也可以為null
}

基本操作一

// 頭結(jié)點,引導性作用
var head : Node<T>?= null

// 取決于head是否為null
var isEmpty : Boolean = head == null

// 獲取first
fun first() : Node<T>? = head

// 獲取last結(jié)點,需要一直next才能到達last結(jié)點
fun last() : Node<T>?{
 var node = head
 if (node != null){ 
  while (node?.next != null){
   node = node?.next
  }
  return node
 }else{
  return null
 }
}

基本操作二

// 獲取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結(jié)點上append
fun append(value : T){
 var newNode = Node(value)
 // 獲取當前節(jié)點的最后一個節(jié)點
 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 // 將斷開的節(jié)點前后置null
 node.next = null

 return node.value // 返回刪除節(jié)點的value
}

關(guān)于Stack與LinkedList如何在Kotlin中實現(xiàn)就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI