-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented Queue data structure using array andstack techniques
- Loading branch information
1 parent
991a536
commit d0cf79b
Showing
7 changed files
with
349 additions
and
2 deletions.
There are no files selected for viewing
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
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
70 changes: 70 additions & 0 deletions
70
DevKit/DevKit/Classes/Data Structures/Queue/QueueArray.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// | ||
// QueueArray.swift | ||
// DevKit | ||
// | ||
// Created by Thibault Klein on 9/6/18. | ||
// Copyright © 2018 Jonathan Samudio. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// QueueArray. | ||
/// A queue is a collection of elements following a First-In-First-Out (FIFO) order. | ||
/// This implementation uses an array to store the elements. | ||
/// | ||
/// - Note: | ||
/// Use a Queue if you need a sequence ordered as FIFO. | ||
/// | ||
/// - The advantage of using an array is its simplicity and relative low memory impact. | ||
/// - The disadvantage is a O(n) complexity every time an element gets dequeued. | ||
/// Very large queues will have a bad performance using QueueArray. | ||
open class QueueArray<T>: Queueable { | ||
public typealias Element = T | ||
|
||
// MARK: - Public Properties | ||
|
||
/// `true` if the stack is empty. `false` if not. | ||
public var isEmpty: Bool { | ||
return peek() == nil | ||
} | ||
|
||
// MARK: - Private Properties | ||
|
||
private var storage: [Element] = [] | ||
|
||
// MARK: - Initialization | ||
|
||
public init() { } | ||
|
||
// MARK: - Public Functions | ||
// MARK: General Functions | ||
|
||
/// Returns the value at the end of the queue. | ||
/// | ||
/// - Complexity: O(1) | ||
/// - Returns: The value at the end of the queue. | ||
public func peek() -> Element? { | ||
return storage.last | ||
} | ||
|
||
// MARK: Adding Functions | ||
|
||
/// Enqueues an element at the beginning of the queue. | ||
/// | ||
/// - Complexity: O(1) | ||
/// - Parameter value: The value to enqueue. | ||
public func enqueue(_ value: Element) { | ||
storage.append(value) | ||
} | ||
|
||
// MARK: Removing Functions | ||
|
||
/// Dequeues the element at the end of the queue. | ||
/// | ||
/// - Complexity: O(n) | ||
/// - Returns: The dequeued element. | ||
public func dequeue() -> Element? { | ||
return isEmpty ? nil : storage.removeFirst() | ||
} | ||
|
||
} |
75 changes: 75 additions & 0 deletions
75
DevKit/DevKit/Classes/Data Structures/Queue/QueueStack.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// | ||
// QueueStack.swift | ||
// DevKit | ||
// | ||
// Created by Thibault Klein on 9/6/18. | ||
// Copyright © 2018 Jonathan Samudio. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// QueueStack. | ||
/// A queue is a collection of elements following a First-In-First-Out (FIFO) order. | ||
/// This implementation uses two stacks to store the elements. | ||
/// | ||
/// - Note: | ||
/// Use a Queue if you need a sequence ordered as FIFO. | ||
/// | ||
/// - The advantage of using two stack is constant complexity when enqueueing and dequeueing. | ||
/// QueueStack has better performance when working with large queues than QueueArray. | ||
open class QueueStack<T>: Queueable { | ||
public typealias Element = T | ||
|
||
// MARK: - Public Properties | ||
|
||
/// `true` if the stack is empty. `false` if not. | ||
public var isEmpty: Bool { | ||
return leftStack.isEmpty && rightStack.isEmpty | ||
} | ||
|
||
// MARK: - Private Properties | ||
|
||
private var leftStack: [T] = [] | ||
private var rightStack: [T] = [] | ||
|
||
// MARK: - Initialization | ||
|
||
public init() { } | ||
|
||
// MARK: - Public Functions | ||
// MARK: General Functions | ||
|
||
/// Returns the value at the end of the queue. | ||
/// | ||
/// - Complexity: O(1) | ||
/// - Returns: The value at the end of the queue. | ||
public func peek() -> T? { | ||
return !leftStack.isEmpty ? leftStack.last : rightStack.first | ||
} | ||
|
||
// MARK: Adding Functions | ||
|
||
/// Enqueues an element at the beginning of the queue. | ||
/// | ||
/// - Complexity: O(1) | ||
/// - Parameter value: The value to enqueue. | ||
public func enqueue(_ value: T) { | ||
rightStack.append(value) | ||
} | ||
|
||
// MARK: Removing Functions | ||
|
||
/// Dequeues the element at the end of the queue. | ||
/// | ||
/// - Complexity: O(1) | ||
/// - Returns: The dequeued element. | ||
public func dequeue() -> T? { | ||
if leftStack.isEmpty { | ||
leftStack = rightStack.reversed() | ||
rightStack.removeAll() | ||
} | ||
|
||
return leftStack.popLast() | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
DevKit/DevKit/Classes/Data Structures/Queue/Queueable.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// | ||
// Queueable.swift | ||
// DevKit | ||
// | ||
// Created by Thibault Klein on 9/6/18. | ||
// Copyright © 2018 Jonathan Samudio. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Defines how a queue can be implemented. | ||
/// There are different techniques you can use to create a queue (array, doubly linked list, ring buffer, double stack...) | ||
/// that can confirm to this protocol. | ||
public protocol Queueable { | ||
associatedtype Element | ||
|
||
var isEmpty: Bool { get } | ||
|
||
func peek() -> Element? | ||
func enqueue(_ value: Element) | ||
func dequeue() -> Element? | ||
|
||
} |
69 changes: 69 additions & 0 deletions
69
DevKit/DevKitTests/Classes/Data Structures/Queue/QueueArrayTests.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// | ||
// QueueArrayTests.swift | ||
// DevKitTests | ||
// | ||
// Created by Thibault Klein on 9/7/18. | ||
// Copyright © 2018 Jonathan Samudio. All rights reserved. | ||
// | ||
|
||
import DevKit | ||
import XCTest | ||
|
||
class QueueArrayTests: XCTestCase { | ||
|
||
func test_queueArray_IsEmpty_EmptyList() { | ||
// Given | ||
let queue = QueueArray<Int>() | ||
// Then | ||
XCTAssertTrue(queue.isEmpty) | ||
} | ||
|
||
func test_queueArray_IsEmpty_NonEmptyList() { | ||
// Given | ||
let queue = QueueArray<Int>() | ||
// When | ||
queue.enqueue(1) | ||
// Then | ||
XCTAssertFalse(queue.isEmpty) | ||
} | ||
|
||
func test_queueArray_Peek_EmptyList() { | ||
// Given | ||
let queue = QueueArray<Int>() | ||
// Then | ||
XCTAssertNil(queue.peek()) | ||
} | ||
|
||
func test_queueArray_Enqueue() { | ||
// Given | ||
let queue = QueueArray<Int>() | ||
XCTAssertNil(queue.peek()) | ||
// When | ||
queue.enqueue(1) | ||
// Then | ||
XCTAssertEqual(queue.peek()!, 1) | ||
} | ||
|
||
func test_queueArray_Dequeue_EmptyList() { | ||
// Given | ||
let queue = QueueArray<Int>() | ||
XCTAssertNil(queue.peek()) | ||
// When | ||
queue.enqueue(1) | ||
// Then | ||
XCTAssertEqual(queue.dequeue()!, 1) | ||
} | ||
|
||
func test_queueArray_Dequeue() { | ||
// Given | ||
let queue = QueueArray<Int>() | ||
XCTAssertNil(queue.peek()) | ||
// When | ||
queue.enqueue(1) | ||
queue.enqueue(2) | ||
queue.enqueue(3) | ||
// Then | ||
XCTAssertEqual(queue.dequeue()!, 1) | ||
} | ||
|
||
} |
Oops, something went wrong.