KotlinCS 124 LogoJava
PrevIndexNext
Kotlin
Java
  • Implementing a Map : 04/26/2024

  • map-reduce-filter : 04/25/2024

  • Generics : 04/24/2024

  • Hashing : 04/23/2024

  • Binary Search : 04/22/2024

  • MP3: Course Ratings : 04/19/2024

  • Quicksort : 04/18/2024

  • Merge Sort : 04/17/2024

  • Sorting Algorithms : 04/16/2024

  • MP Debugging Part 1 : 04/15/2024

  • MP2: Course Activity : 04/12/2024

  • Practice with Recursion : 04/11/2024

  • MP Debugging Part 0 : 04/10/2024

  • MP2: API Client : 04/09/2024

  • MP2: API Server : 04/08/2024

  • Trees and Recursion : 04/05/2024

  • Trees : 04/04/2024

  • Recursion : 04/03/2024

  • MP1: Filtering and Search : 04/02/2024

  • MP1: Loading and Sorting : 04/01/2024

  • Lists Review and Performance : 03/29/2024

  • Linked Lists : 03/28/2024

  • Algorithms and Lists : 03/27/2024

  • Continuing MP0 : 03/26/2024

  • Getting Started with MP0 : 03/25/2024

  • Lambda Expressions : 03/22/2024

  • Anonymous Classes : 03/21/2024

  • Practice with Interfaces : 03/20/2024

  • Implementing Interfaces : 03/19/2024

  • Using Interfaces : 03/18/2024

  • Working with Exceptions : 03/08/2024

  • Throwing Exceptions : 03/07/2024

  • Catching Exceptions : 03/06/2024

  • References and Polymorphism : 03/05/2024

  • References : 03/04/2024

  • Data Modeling 2 : 03/01/2024

  • Equality and Object Copying : 02/29/2024

  • Polymorphism : 02/28/2024

  • Inheritance : 02/27/2024

  • Data Modeling 1 : 02/26/2024

  • Companion Objects : 02/23/2024

  • Encapsulation : 02/22/2024

  • Constructors : 02/21/2024

  • Objects, Continued : 02/20/2024

  • Introduction to Objects : 02/19/2024

  • Compilation and Immutability : 02/16/2024

  • Practice with Collections : 02/15/2024

  • Maps and Sets : 02/14/2024

  • Lists and Type Parameters : 02/13/2024

  • Imports and Libraries : 02/12/2024

  • Multidimensional Arrays : 02/09/2024

  • Practice with Strings : 02/08/2024

  • null : 02/07/2024

  • Algorithms and Strings : 02/06/2024

  • Strings : 02/05/2024

  • Functions and Algorithms : 02/02/2024

  • Practice with Functions : 02/01/2024

  • More About Functions : 01/31/2024

  • Errors and Debugging : 01/30/2024

  • Functions : 01/29/2024

  • Practice with Loops and Algorithms : 01/26/2024

  • Algorithms : 01/25/2024

  • Loops : 01/24/2024

  • Arrays : 01/23/2024

  • Compound Conditionals : 01/22/2024

  • Conditional Expressions and Statements : 01/19/2024

  • Operations on Variables : 01/18/2024

  • Variables and Types : 01/17/2024

  • Welcome to CS 124 : 01/16/2024

Linked Lists

interface SimpleList {
fun get(index: Int): Any?
fun set(index: Int, value: Any?)
fun remove(index: Int): Any?
fun add(index: Int, value: Any?)
}
class SimpleLinkedList(values: Array<Any?>) : SimpleList {
private inner class Item(var value: Any?, var next: Item?)
private var start: Item? = null
private var size = 0
init {
for (i in values.indices.reversed()) {
add(0, values[i])
}

Welcome back! Next we’ll continue discussing lists, but explore a new approach: linked lists. No arrays required! Let’s get to it…

Linked Lists
Linked Lists

So far we’ve seen how to implement a list using an array to store the references. However, the array was also maintaining the order!

But this is not the only way! Let’s look at another approach that uses references to link items together into a chain. First we’ll explore it on paper, and then start our implementation, and check out a few diagrams along the way.

Linked List Constructor
Linked List Constructor

Next, let’s look at how to create our linked list constructor, and a simple version of add that only supports adding items to the front of the list.

// SimpleLinkedList constructor and add to front

Confused? OK! Let’s look at the same code but using a diagram!

Inner Classes
Inner Classes

We promised that as we continue building data structures and implementing and analyzing algorithms, we’d also meet some new Kotlin syntax along the way. You may have noticed our implementation uses something called an inner (or nested) class. Let’s examine that more carefully!

// Inner Classes

SimpleLinkedList get
SimpleLinkedList get

Next let’s go on and implement get. But first, we need to think about how we are actually going to walk the list! Let’s see that in a diagram first:

And now, we’ll complete our implementation of get and consider its performance.

// SimpleLinkedList get

But what about add and remove? We’ll get there! Next time…

Homework: SimpleLinkedList set

Created By: Geoffrey Challen
/ Version: 2020.10.0

Let's implement a list using a different strategy. Instead of an internal array, we'll link items together into a chain using references. Our list class will only maintain a reference to the start of the list and walk the list to perform list operations. This approach will have interesting tradeoffs compared to our implementation that used arrays.

Starting with the SimpleLinkedList class below, complete the code for set. You'll want to review get and the rest of the code to understand how this list implementation works and how to walk a linked list.

class SimpleLinkedList(values: Array<Any?>) {
private inner class Item(var value: Any?, var next: Item?)
private var start: Item? = null
private var size = 0
init {
for (value in values.reversed()) {
add(0, value)
}
}
fun size() = size
private fun add(
index: Int,
value: Any?,
) {
require(index == 0) { "Non-zero add not supported yet" }
start = Item(value, start)
size++
}
fun get(index: Int): Any? {

More Practice

Need more practice? Head over to the practice page.