LinkedListDeque
Implement a generic deque as a circular doubly linked list with one sentinel: constant-time operations at both ends, iterative and recursive get, no leaks under ARC, and a randomized test suite of your own.
This is CS61B’s Project 1, in Swift. You will implement a deque as a circular doubly linked list with a single sentinel node, the design from Lesson 29. The next project implements the same interface with an array, and the two are tested the same way.
Getting started
Work in the lldeque folder of the
swift-projects repository:
cd swift-projects/lldeque
swift test
The package runs anywhere Swift does; it has no user interface.
Sources/LinkedListDeque/Deque.swiftis theDequeprotocol, provided. Read its documentation comments: they are the specification.Sources/LinkedListDeque/LinkedListDeque.swiftis yours.Tests/LinkedListDequeTests/LinkedListDequeTests.swiftholds the provided tests, andMyTests.swiftis for yours.
The rules
Your LinkedListDeque<Element> must follow these rules, which the tests
check where they can:
addFirst,addLast,removeFirst,removeLast, andcounttake constant time: the same few steps whether the deque holds ten items or ten million. No loops in them.getmay walk the list, but must start from whichever end is closer.- Use one circular sentinel, whose
nextis the first node and whoseprevis the last. An empty deque is a sentinel pointing at itself. - No leaks. When the deque is freed, every node and every item must be
freed. Removed items must be released right away. Lesson 29 shows how:
weakbackward links, and adeinitthat cuts the ring. - Do not use Swift’s
Array(or any collection) to store the items.toArraymay build an array to return.
Task 1: Adding
Declare a private, nested Node class, the sentinel, and a count. Then write
init, count, addFirst, and addLast. Draw the ring on paper for an empty
deque, then after one addLast, before writing code: the empty case is where
most bugs start.
Task 2: toArray
Write toArray(), which walks the ring from the sentinel’s next until it
comes back to the sentinel. Many tests use it, so do it early.
Task 3: get
Write get(_:), returning nil for any index outside 0..<count. Walk from
the front for indices in the first half, and from the back otherwise.
Task 4: getRecursive
Write getRecursive(_:), which does the same with recursion instead of a loop:
a private helper that takes a node and the number of steps left. As Lesson 28
explained, this version runs out of stack for very long lists; that is why
get is the one to use.
Task 5: Removing
Write removeFirst() and removeLast(). Each changes exactly two links (why
not four?) and the count. Removing from an empty deque returns nil and changes
nothing.
Task 6: Memory
The provided memory tests add items of a class Tracked, which counts how many
instances are alive, and check that removed items and freed deques release
everything. If freeingTheDequeFreesEverything fails, some cycle of strong
references survives: check that prev is weak, and that deinit cuts the
ring.
Task 7: Your own tests
The provided tests are not complete, and passing them is not the goal.
Write, in MyTests.swift:
- A randomized comparison (Lesson 27): thousands of random operations,
including
getat random indices, on your deque and on an[Int], checking that they agree after every step. Use a seeded generator so a failure can be reproduced. - A test that
countandisEmptystay right through a sequence that empties and refills the deque several times. - A test of
getRecursiveagainstgetfor every index of a large deque.