栈与LinkedList如何在芬兰湾的科特林中实现

  介绍

这篇文章将为大家详细讲解有关栈与LinkedList如何在芬兰湾的科特林中实现,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

<强>堆栈

Java堆栈中由列表实现,芬兰湾的科特林中有MutableList,堆栈类的基本定义如下,继承迭代器为了迭代遍历:

class  Stack祝辞(list : MutableList),:, Iterator

基本属性实现

//,堆栈的计数   ,var 人造台面:Int =0//,堆栈内部实现为MutableList   ,var 项目:MutableList=,列表//,判断堆栈是否为null   fun  isEmpty ():, Boolean =, this.items.isEmpty ()//,获取栈的items 女伯爵   count (): fun  Int =, this.items.count ()//,tostring操作   ,override  fun  toString ():, String  {   return 才能;this.items.toString ()   以前,}

基本操作实现

//,流行操作,弹出栈顶元素即链表最末端元素,可为空   ,fun 流行():T ?, {   ,if  (this.isEmpty ()), {   return 才能null   ,}else  {   val 才能;item =, this.items.count(),安康;1   return 才能this.items.removeAt(项)   ,}   }//,只读操作,不弹出   peek (): fun  T ?, {   ,if  (isEmpty ()), {   return 才能null   ,}else  {   return 才能;this.items [this.items.count(),安康;1]   ,}         }//,hasNext操作   override  fun  hasNext (): Boolean  {   ,val  hasNext =, itCounter  & lt;, count ()   ,if  (! hasNext), itCounter =0   return  hasNext   }//,取下一个元素   override  fun  next (): T  {   ,if  (hasNext ()) {   val 才能;topPos :, Int =, (count(),安康;1),安康;人造台面   ,人造台面+ +   return 才能this.items [topPos]   ,其他}{   throw 才能;NoSuchElementException (“No  such  element"),//,异常不用新的哦   ,}   }

<>强LinkedList

LinkedList的实现需要节点,然后实现第一,最后,以计数及附加等操作。

节点定义

class  Node(时间:value  T) {   ,var  value : T =, value //,价值可以是任意类型   ,var  next : Node ?,=, null //,未来可以为null   ,var  previous : Node ?,=, null //,之前也可以为null   }

基本操作一

//,头结点,引导性作用   var  head : Node ?=零//,取决于头是否为null   var  isEmpty : Boolean =, head ==零//,获取第一名   fun 第(),:,Node ?=,头//,获取最后结点,需要一直下才能到达最后结点   fun 去年(),:,Node ? {   ,var  node =,   ,if  (node  !=, null) {,   while 才能;(节点? .next  !=, null) {   ,,node =,节点?。第二   ,,}   return 才能;节点   ,其他}{   return 才能null   ,}   }

基本操作二

//,获取数,同样通过下一计算   fun  count (): Int  {   ,var  node =,   ,if  (node  !=, null) {   var 才能;counter =1   while 才能;(节点? .next  !=, null) {   ,,node =,节点?。第二   ,,counter  +=1   ,,}   return 才能;计数器   ,}else  {   ,return  0   ,}   }//,附加操作,在最后结点上追加   :fun 附加(value  T) {   ,var  newNode =,节点(值)   ,//获取当前节点的最后一个节点   ,var  lastNode =, this.last ()   ,if  (lastNode  !=, null) {   newNode.previous 才能=lastNode   lastNode.next 才能=newNode   ,其他}{   head 才能=newNode   ,}   }//,删除操作   时间:fun  removeNode (node  Node),:, T {   ,val  prev =node.previous   ,val  next =node.next      ,if  (prev  !=, null) {   prev.next 才能=,   ,其他}{   head 才能=,   ,}   接下来,? .previous =上一页=,,node.previous  null //,将断开的节点前后置空   ,node.next =零      ,return  node.value //,返回删除节点的值   }

关于堆栈与LinkedList如何在芬兰湾的科特林中实现就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看的到。

栈与LinkedList如何在芬兰湾的科特林中实现