SoFunction
Updated on 2025-03-11

Example of implementation method of Stack and LinkedList in Kotlin

Preface

This article mainly introduces the basic data structures Stack and LinkedList implementation of Kotlin. We will share them for your reference and learning. I won’t say much below, let’s take a look at the detailed introduction together.

Stack

Stack is implemented by List in Java, and MutableList is available in Kotlin. The basic definition of the Stack class is as follows. Iterator is inherited for iterative traversal:

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

Basic attribute implementation

// stack count var itCounter: Int = 0

// The stack is internally implemented as MutableList var items: MutableList&lt;T&gt; = list

// Determine whether stack is nullfun isEmpty(): Boolean = ()

// Get stack items countfun count(): Int = ()

// tostring operation override fun toString(): String {
  return ()
 }

Basic operation implementation

// pop operation, pop the top element of the stack, that is, the end element of the linked list, can be null fun pop(): T? {
 if (()) {
  return null
 } else {
  val item = () - 1
  return (item)
 }
}

// Read-only operation, no pop-upfun peek(): T? {
 if (isEmpty()) {
  return null
 } else {
  return [() - 1]
 }


}

// hasNext operationoverride fun hasNext(): Boolean {
 val hasNext = itCounter &lt; count()
 if (!hasNext) itCounter = 0
 return hasNext
}

// Take next elementoverride fun next(): T {
 if (hasNext()){
  val topPos : Int = (count() - 1) - itCounter
  itCounter++
  return [topPos]
 }else{
  throw NoSuchElementException("No such element") // No need to new for exception }
}

LinkedList

The implementation of LinkedList requires Node, and then implements operations such as first, last, count, and append.

Node definition

class Node&lt;T&gt;(value : T){
 var value : T = value // value can be of any type var next : Node&lt;T&gt;? = null // next can be null var previous : Node&lt;T&gt;? = null // pre can also be null}

Basic Operation 1

// Head node, guiding rolevar head : Node&lt;T&gt;?= null

// Depends on whether the head is nullvar isEmpty : Boolean = head == null

// Get firstfun first() : Node&lt;T&gt;? = head

// Get the last node, you need to keep next to reach the last nodefun last() : Node&lt;T&gt;?{
 var node = head
 if (node != null){ 
  while (node?.next != null){
   node = node?.next
  }
  return node
 }else{
  return null
 }
}

Basic Operation Two

// Get count, also calculate through nextfun 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 operation, append on the last nodefun append(value : T){
 var newNode = Node(value)
 // Get the last node of the current node var lastNode = ()
 if (lastNode != null){
   = lastNode
   = newNode
 }else{
  head = newNode
 }
}

// Delete operationfun removeNode(node : Node&lt;T&gt;) : T{
 val prev = 
 val next = 

 if (prev != null){
   = next
 }else{
  head = next
 }
 next?.previous = prev

  = null // null the disconnected node before and after  = null

 return  // Return the value of the delete node}

Above, use kotlin to implement basic data structures stack and linkedlist.

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.