Skip to content

Commit

Permalink
Add linked list
Browse files Browse the repository at this point in the history
Removed singly linked list; this discussion covers both.
  • Loading branch information
hollance committed Feb 3, 2016
1 parent c760b9d commit da8048a
Show file tree
Hide file tree
Showing 18 changed files with 1,380 additions and 495 deletions.

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions Linked List/LinkedList Tests/LinkedList/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// AppDelegate.swift
// LinkedList
//
// Created by Matthijs Hollemans on 03-02-16.
// Copyright © 2016 Swift Algorithm Club. All rights reserved.
//

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

@IBOutlet weak var window: NSWindow!


func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application
}

func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}


}

Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
<customObject id="YLy-65-1bz" customClass="NSFontManager"/>
<menu title="Main Menu" systemMenu="main" id="AYu-sK-qS6">
<items>
<menuItem title="LinkedListTests" id="1Xt-HY-uBw">
<menuItem title="LinkedList" id="1Xt-HY-uBw">
<modifierMask key="keyEquivalentModifierMask"/>
<menu key="submenu" title="LinkedListTests" systemMenu="apple" id="uQy-DD-JDr">
<menu key="submenu" title="LinkedList" systemMenu="apple" id="uQy-DD-JDr">
<items>
<menuItem title="About LinkedListTests" id="5kV-Vb-QxS">
<menuItem title="About LinkedList" id="5kV-Vb-QxS">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="orderFrontStandardAboutPanel:" target="-1" id="Exp-CZ-Vem"/>
Expand All @@ -37,7 +37,7 @@
<menu key="submenu" title="Services" systemMenu="services" id="hz9-B4-Xy5"/>
</menuItem>
<menuItem isSeparatorItem="YES" id="4je-JR-u6R"/>
<menuItem title="Hide LinkedListTests" keyEquivalent="h" id="Olw-nP-bQN">
<menuItem title="Hide LinkedList" keyEquivalent="h" id="Olw-nP-bQN">
<connections>
<action selector="hide:" target="-1" id="PnN-Uc-m68"/>
</connections>
Expand All @@ -55,7 +55,7 @@
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="kCx-OE-vgT"/>
<menuItem title="Quit LinkedListTests" keyEquivalent="q" id="4sb-4s-VLi">
<menuItem title="Quit LinkedList" keyEquivalent="q" id="4sb-4s-VLi">
<connections>
<action selector="terminate:" target="-1" id="Te7-pn-YzF"/>
</connections>
Expand Down Expand Up @@ -656,7 +656,7 @@
<modifierMask key="keyEquivalentModifierMask"/>
<menu key="submenu" title="Help" systemMenu="help" id="F2S-fz-NVQ">
<items>
<menuItem title="LinkedListTests Help" keyEquivalent="?" id="FKE-Sm-Kum">
<menuItem title="LinkedList Help" keyEquivalent="?" id="FKE-Sm-Kum">
<connections>
<action selector="showHelp:" target="-1" id="y7X-2Q-9no"/>
</connections>
Expand All @@ -666,7 +666,7 @@
</menuItem>
</items>
</menu>
<window title="LinkedListTests" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" releasedWhenClosed="NO" animationBehavior="default" id="QvC-M9-y7g">
<window title="LinkedList" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" releasedWhenClosed="NO" animationBehavior="default" id="QvC-M9-y7g">
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES"/>
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
<rect key="contentRect" x="335" y="390" width="480" height="360"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2016 Mac Bellingrath. All rights reserved.</string>
<string>Copyright © 2016 Swift Algorithm Club. All rights reserved.</string>
<key>NSMainNibFile</key>
<string>MainMenu</string>
<key>NSPrincipalClass</key>
Expand Down
252 changes: 252 additions & 0 deletions Linked List/LinkedList Tests/LinkedListTests/LinkedListTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
import XCTest
@testable import LinkedList

class LinkedListTest: XCTestCase {
let numbers = [8, 2, 10, 9, 7, 5]

private func buildList() -> LinkedList<Int> {
let list = LinkedList<Int>()
for number in numbers {
list.append(number)
}
return list
}

func testEmptyList() {
let list = LinkedList<Int>()
XCTAssertTrue(list.isEmpty)
XCTAssertEqual(list.count, 0)
XCTAssertNil(list.first)
XCTAssertNil(list.last)
}

func testListWithOneElement() {
let list = LinkedList<Int>()
list.append(123)

XCTAssertFalse(list.isEmpty)
XCTAssertEqual(list.count, 1)

XCTAssertNotNil(list.first)
XCTAssertNil(list.first!.previous)
XCTAssertNil(list.first!.next)
XCTAssertEqual(list.first!.value, 123)

XCTAssertNotNil(list.last)
XCTAssertNil(list.last!.previous)
XCTAssertNil(list.last!.next)
XCTAssertEqual(list.last!.value, 123)

XCTAssertTrue(list.first === list.last)
}

func testListWithTwoElements() {
let list = LinkedList<Int>()
list.append(123)
list.append(456)

XCTAssertEqual(list.count, 2)

XCTAssertNotNil(list.first)
XCTAssertEqual(list.first!.value, 123)

XCTAssertNotNil(list.last)
XCTAssertEqual(list.last!.value, 456)

XCTAssertTrue(list.first !== list.last)

XCTAssertNil(list.first!.previous)
XCTAssertTrue(list.first!.next === list.last)
XCTAssertTrue(list.last!.previous === list.first)
XCTAssertNil(list.last!.next)
}

func testListWithThreeElements() {
let list = LinkedList<Int>()
list.append(123)
list.append(456)
list.append(789)

XCTAssertEqual(list.count, 3)

XCTAssertNotNil(list.first)
XCTAssertEqual(list.first!.value, 123)

let second = list.first!.next
XCTAssertNotNil(second)
XCTAssertEqual(second!.value, 456)

XCTAssertNotNil(list.last)
XCTAssertEqual(list.last!.value, 789)

XCTAssertNil(list.first!.previous)
XCTAssertTrue(list.first!.next === second)
XCTAssertTrue(second!.previous === list.first)
XCTAssertTrue(second!.next === list.last)
XCTAssertTrue(list.last!.previous === second)
XCTAssertNil(list.last!.next)
}

func testNodeAtIndexInEmptyList() {
let list = LinkedList<Int>()
let node = list.nodeAtIndex(0)
XCTAssertNil(node)
}

func testNodeAtIndexInListWithOneElement() {
let list = LinkedList<Int>()
list.append(123)

let node = list.nodeAtIndex(0)
XCTAssertNotNil(node)
XCTAssertEqual(node!.value, 123)
XCTAssertTrue(node === list.first)
}

func testNodeAtIndex() {
let list = buildList()

let nodeCount = list.count
XCTAssertEqual(nodeCount, numbers.count)

XCTAssertNil(list.nodeAtIndex(-1))
XCTAssertNil(list.nodeAtIndex(nodeCount))

let first = list.nodeAtIndex(0)
XCTAssertNotNil(first)
XCTAssertTrue(first === list.first)
XCTAssertEqual(first!.value, numbers[0])

let last = list.nodeAtIndex(nodeCount - 1)
XCTAssertNotNil(last)
XCTAssertTrue(last === list.last)
XCTAssertEqual(last!.value, numbers[nodeCount - 1])

for i in 0..<nodeCount {
let node = list.nodeAtIndex(i)
XCTAssertNotNil(node)
XCTAssertEqual(node!.value, numbers[i])
}
}

func testSubscript() {
let list = buildList()
for i in 0 ..< list.count {
XCTAssertEqual(list[i], numbers[i])
}
}

func testInsertAtIndexInEmptyList() {
let list = LinkedList<Int>()
list.insert(123, atIndex: 0)

XCTAssertFalse(list.isEmpty)
XCTAssertEqual(list.count, 1)

let node = list.nodeAtIndex(0)
XCTAssertNotNil(node)
XCTAssertEqual(node!.value, 123)
}

func testInsertAtIndex() {
let list = buildList()
let prev = list.nodeAtIndex(2)
let next = list.nodeAtIndex(3)
let nodeCount = list.count

list.insert(444, atIndex: 3)

let node = list.nodeAtIndex(3)
XCTAssertNotNil(node)
XCTAssertEqual(node!.value, 444)
XCTAssertEqual(nodeCount + 1, list.count)

XCTAssertFalse(prev === node)
XCTAssertFalse(next === node)
XCTAssertTrue(prev!.next === node)
XCTAssertTrue(next!.previous === node)
}

func testRemoveAtIndexOnListWithOneElement() {
let list = LinkedList<Int>()
list.append(123)

let value = list.removeAtIndex(0)
XCTAssertEqual(value, 123)

XCTAssertTrue(list.isEmpty)
XCTAssertEqual(list.count, 0)
XCTAssertNil(list.first)
XCTAssertNil(list.last)
}

func testRemoveAtIndex() {
let list = buildList()
let prev = list.nodeAtIndex(2)
let next = list.nodeAtIndex(3)
let nodeCount = list.count

list.insert(444, atIndex: 3)

let value = list.removeAtIndex(3)
XCTAssertEqual(value, 444)

let node = list.nodeAtIndex(3)
XCTAssertTrue(next === node)
XCTAssertTrue(prev!.next === node)
XCTAssertTrue(node!.previous === prev)
XCTAssertEqual(nodeCount, list.count)
}

func testRemoveLastOnListWithOneElement() {
let list = LinkedList<Int>()
list.append(123)

let value = list.removeLast()
XCTAssertEqual(value, 123)

XCTAssertTrue(list.isEmpty)
XCTAssertEqual(list.count, 0)
XCTAssertNil(list.first)
XCTAssertNil(list.last)
}

func testRemoveLast() {
let list = buildList()
let last = list.last
let prev = last!.previous
let nodeCount = list.count

let value = list.removeLast()
XCTAssertEqual(value, 5)

XCTAssertNil(last!.previous)
XCTAssertNil(last!.next)

XCTAssertNil(prev!.next)
XCTAssertTrue(list.last === prev)
XCTAssertEqual(nodeCount - 1, list.count)
}

func testRemoveAll() {
let list = buildList()
list.removeAll()
XCTAssertTrue(list.isEmpty)
XCTAssertEqual(list.count, 0)
XCTAssertNil(list.first)
XCTAssertNil(list.last)
}

func testReverseLinkedList() {
let list = buildList()
let first = list.first
let last = list.last
let nodeCount = list.count

list.reverse()

XCTAssertTrue(first === list.last)
XCTAssertTrue(last === list.first)
XCTAssertEqual(nodeCount, list.count)
}
}
Loading

0 comments on commit da8048a

Please sign in to comment.