Just Eat Oleg Tsibulevskiy Mar 12, 2021

Implementing Doubly Linked Lists in Swift

Article Summary

Oleg Tsibulevskiy from Just Eat Takeaway breaks down why choosing the wrong data structure could be killing your iOS app's performance. Spoiler: Arrays aren't always the answer.

This deep dive explores doubly linked lists in Swift, comparing their performance characteristics against dynamic arrays. Tsibulevskiy walks through a complete implementation with practical code examples, from basic operations to Swift protocol conformance.

Key Takeaways

Critical Insight

Doubly linked lists outperform arrays for insertion/deletion operations but trade off direct index access, making them ideal for sequential navigation use cases.

The article reveals a clever trick using Swift's enumerated method that eliminates the need for helper properties when finding nodes.

About This Article

Problem

iOS developers often struggle to pick between doubly linked lists and dynamic arrays. Inserting at the beginning of an array costs O(n) time because every element has to shift, and the array might need to resize.

Solution

Oleg Tsibulevskiy built a generic Swift doubly linked list class with Head, Tail, Selected, and Length properties. It conforms to Sequence and IteratorProtocol so it works naturally with Swift's iteration features.

Impact

Developers can now pick the right data structure for what they're building. A doubly linked list gives O(1) insertion at the start while arrays keep O(1) index access. This matters for music players, browser caches, and card games where performance counts.