Skip to content

Commit 864f0ee

Browse files
committed
Linked List
1 parent 59e1112 commit 864f0ee

File tree

9 files changed

+5451
-1794
lines changed

9 files changed

+5451
-1794
lines changed

ProblemSolving.playground/Contents.swift

+209-1,601
Large diffs are not rendered by default.

ProblemSolving.playground/Sources/AnotherSolution.swift

+1,729-154
Large diffs are not rendered by default.
+23-27
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,20 @@
11
import Foundation
22

3-
public class Queue {
3+
public struct Queue<T> {
4+
private var items: [T] = []
45

5-
var elements: [Int] = []
6+
public mutating func enqueue(_ item: T) {
7+
items.append(item)
8+
}
69

7-
public init() { }
8-
9-
public func push(_ x: Int) {
10-
elements.append(x)
11-
}
12-
13-
public var head: Int? {
14-
return elements.first
15-
}
16-
17-
public func pop() -> Int {
18-
elements.popLast()!
19-
}
20-
21-
public var tail: Int? {
22-
return elements.last
23-
}
24-
25-
public var count: Int? {
26-
return elements.count
27-
}
28-
29-
public func isEmpty() -> Bool{
30-
return elements.isEmpty
31-
}
10+
public mutating func dequeue() -> T? {
11+
guard !items.isEmpty else { return nil }
12+
return items.removeFirst()
13+
}
3214

15+
public var isEmpty: Bool {
16+
return items.isEmpty
17+
}
3318
}
3419

3520
public struct MonotonicStack<T: Comparable> {
@@ -63,3 +48,14 @@ public struct MonotonicStack<T: Comparable> {
6348
}
6449

6550

51+
// Definition for doubly-linked list.
52+
public class DoublyLinkedListNode {
53+
public var data: Int
54+
public var next: DoublyLinkedListNode?
55+
public var prev: DoublyLinkedListNode?
56+
public init(data: Int, next: DoublyLinkedListNode? = nil, prev: DoublyLinkedListNode? = nil) {
57+
self.data = data
58+
self.next = next
59+
self.prev = prev
60+
}
61+
}

0 commit comments

Comments
 (0)