Last active
May 30, 2019 17:13
-
-
Save davidinga/89856809212a51b78d73c98e25e851fb to your computer and use it in GitHub Desktop.
Doubly Linked List
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DoublyLinkedList<Element> { | |
private var head: DoublyLinkedListNode<Element>? | |
private var tail: DoublyLinkedListNode<Element>? | |
public var first: DoublyLinkedListNode<Element>? { | |
return head | |
} | |
public var last: DoublyLinkedListNode<Element>? { | |
return tail | |
} | |
public var isEmpty: Bool { | |
return head == nil | |
} | |
public var count: Int { | |
var node = head | |
var count = 0 | |
while node != nil { | |
count += 1 | |
node = node!.next | |
} | |
return count | |
} | |
public func append(_ element: Element) { | |
let node = DoublyLinkedListNode(element) | |
if isEmpty { | |
head = node | |
} else { | |
node.prev = tail | |
tail!.next = node | |
} | |
tail = node | |
} | |
public func index(of element: Int) -> DoublyLinkedListNode<Element>? { | |
if element < 0, head == nil { | |
return nil | |
} | |
var node = head | |
for _ in 0..<element { | |
node = node!.next | |
} | |
return node | |
} | |
public func removeAll() { | |
head = nil | |
tail = nil | |
} | |
public func remove(at index: Int) -> DoublyLinkedListNode<Element>? { | |
var node = head | |
for i in 0...index { | |
if i == index { | |
if index == 0 { | |
head = node!.next | |
head!.prev = nil | |
} | |
else if node!.next == nil { | |
tail = node!.prev | |
tail!.next = nil | |
} | |
else { | |
node!.prev!.next = node!.next | |
node!.next!.prev = node!.prev | |
} | |
} | |
node = node!.next | |
} | |
return node | |
} | |
public func remove(node: DoublyLinkedListNode<Element>) -> DoublyLinkedListNode<Element>? { | |
let prev = node.prev | |
let next = node.next | |
// Removing element inbetween head and tail | |
if let prev = prev { | |
prev.next = next | |
} else { | |
// Removing head | |
head = next | |
} | |
next?.prev = prev | |
// Removing tail | |
if next == nil { | |
tail = prev | |
} | |
node.prev = nil | |
node.next = nil | |
return node | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DoublyLinkedListNode<Element> { | |
public var prev: DoublyLinkedListNode<Element>? | |
public var next: DoublyLinkedListNode<Element>? | |
public var element: Element | |
init(_ element: Element) { | |
self.element = element | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment