Skip to content

Commit a9a6eb8

Browse files
committed
Add very basic deque
1 parent 55bc61b commit a9a6eb8

File tree

6 files changed

+195
-1
lines changed

6 files changed

+195
-1
lines changed

Deque/Deque.playground/Contents.swift

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//: Playground - noun: a place where people can play
2+
3+
public struct Deque<T> {
4+
private var array = [T]()
5+
6+
public var isEmpty: Bool {
7+
return array.isEmpty
8+
}
9+
10+
public var count: Int {
11+
return array.count
12+
}
13+
14+
public mutating func enqueue(element: T) {
15+
array.append(element)
16+
}
17+
18+
public mutating func enqueueFront(element: T) {
19+
array.insert(element, atIndex: 0)
20+
}
21+
22+
public mutating func dequeue() -> T? {
23+
if isEmpty {
24+
return nil
25+
} else {
26+
return array.removeFirst()
27+
}
28+
}
29+
30+
public mutating func dequeueBack() -> T? {
31+
if isEmpty {
32+
return nil
33+
} else {
34+
return array.removeLast()
35+
}
36+
}
37+
38+
public func peekFront() -> T? {
39+
return array.first
40+
}
41+
42+
public func peekBack() -> T? {
43+
return array.last
44+
}
45+
}
46+
47+
var deque = Deque<Int>()
48+
deque.enqueue(1)
49+
deque.enqueue(2)
50+
deque.enqueue(3)
51+
deque.enqueue(4)
52+
53+
deque.dequeue() // 1
54+
deque.dequeueBack() // 4
55+
56+
deque.enqueueFront(5)
57+
deque.dequeue() // 5
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='osx'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Timeline
3+
version = "3.0">
4+
<TimelineItems>
5+
</TimelineItems>
6+
</Timeline>

Deque/Deque.swift

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Deque (pronounced "deck"), a double-ended queue
3+
4+
This particular implementation is simple but not very efficient. Several
5+
operations are O(n). A more efficient implementation would use a doubly
6+
linked list or a circular buffer.
7+
*/
8+
public struct Deque<T> {
9+
private var array = [T]()
10+
11+
public var isEmpty: Bool {
12+
return array.isEmpty
13+
}
14+
15+
public var count: Int {
16+
return array.count
17+
}
18+
19+
public mutating func enqueue(element: T) {
20+
array.append(element)
21+
}
22+
23+
public mutating func enqueueFront(element: T) {
24+
array.insert(element, atIndex: 0)
25+
}
26+
27+
public mutating func dequeue() -> T? {
28+
if isEmpty {
29+
return nil
30+
} else {
31+
return array.removeFirst()
32+
}
33+
}
34+
35+
public mutating func dequeueBack() -> T? {
36+
if isEmpty {
37+
return nil
38+
} else {
39+
return array.removeLast()
40+
}
41+
}
42+
43+
public func peekFront() -> T? {
44+
return array.first
45+
}
46+
47+
public func peekBack() -> T? {
48+
return array.last
49+
}
50+
}

Deque/README.markdown

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Deque
2+
3+
A double-ended queue. For some reason this is pronounced as "deck".
4+
5+
A regular [queue](../Queue/) adds new elements to the back and dequeues from the front. The deque also allows enqueuing at the front and dequeuing from the back, and peeking at both ends.
6+
7+
Here is a very basic implementation of a deque in Swift:
8+
9+
```swift
10+
public struct Deque<T> {
11+
private var array = [T]()
12+
13+
public var isEmpty: Bool {
14+
return array.isEmpty
15+
}
16+
17+
public var count: Int {
18+
return array.count
19+
}
20+
21+
public mutating func enqueue(element: T) {
22+
array.append(element)
23+
}
24+
25+
public mutating func enqueueFront(element: T) {
26+
array.insert(element, atIndex: 0)
27+
}
28+
29+
public mutating func dequeue() -> T? {
30+
if isEmpty {
31+
return nil
32+
} else {
33+
return array.removeFirst()
34+
}
35+
}
36+
37+
public mutating func dequeueBack() -> T? {
38+
if isEmpty {
39+
return nil
40+
} else {
41+
return array.removeLast()
42+
}
43+
}
44+
45+
public func peekFront() -> T? {
46+
return array.first
47+
}
48+
49+
public func peekBack() -> T? {
50+
return array.last
51+
}
52+
}
53+
```
54+
55+
This uses an array internally. Enqueuing and dequeuing is simply a matter of adding and removing items from the front or back of the array.
56+
57+
An example of how to use it in a playground:
58+
59+
```swift
60+
var deque = Deque<Int>()
61+
deque.enqueue(1)
62+
deque.enqueue(2)
63+
deque.enqueue(3)
64+
deque.enqueue(4)
65+
66+
deque.dequeue() // 1
67+
deque.dequeueBack() // 4
68+
69+
deque.enqueueFront(5)
70+
deque.dequeue() // 5
71+
```
72+
73+
This particular implementation of `Deque` is simple but not very efficient. Several operations are **O(n)**, notably `enqueueFront()` and `dequeue()`. I've included it only to show the principle of what a deque does.
74+
75+
A more efficient implementation would use a [doubly linked list](../Linked List/), a [circular buffer](../Ring Buffer/), or two [stacks](../Stack/) facing opposite directions.
76+
77+
*Written for Swift Algorithm Club by Matthijs Hollemans*

README.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ Often just using the built-in `Array`, `Dictionary`, and `Set` types is sufficie
123123

124124
- [Stack](Stack/). Last-in, first-out!
125125
- [Queue](Queue/). First-in, first-out!
126-
- Deque
126+
- [Deque](Deque/). A double-ended queue.
127127
- Priority Queue
128128
- [Ring Buffer](Ring Buffer/). Also known as a circular buffer. An array of a certain size that conceptually wraps around back to the beginning.
129129

0 commit comments

Comments
 (0)