Sha256: 25b623ba13364f0e731428686d24c06b1c3b611e429ed0745b1a81b9fc9f4849

Contents?: true

Size: 1.73 KB

Versions: 325

Compression:

Stored size: 1.73 KB

Contents

class Node<T> {
    var value: T?
    var next: Node?
    var prev: Node?

    init() {
    }

    init(value: T) {
        self.value = value
    }

}

class Deque<T> {
    var count: Int = 0
    var head: Node<T>
    var tail: Node<T>

    init() {
        self.head = Node<T>()
        self.tail = head

    }

    func isEmpty() -> Bool {
        return self.count == 0
    }

    func push(_ value: T) {
        let node = Node<T>(value: value)
        if self.isEmpty() {
            self.head = node
            self.tail = node
        } else {
            node.next = self.head
            self.head.prev = node
            self.head = node
        }
        self.count += 1
    }

    func unshift(_ value: T) {
        let node = Node<T>(value: value)
        if self.isEmpty() {
            self.head = node
            self.tail = node
        } else {
            node.prev = self.tail
            self.tail.next = node
            self.tail = node
        }
        self.count += 1
    }

    func pop() -> T? {
        if self.isEmpty() {
            return nil
        } else if self.count == 1 {
            let temp: Node<T> = self.head
            self.count -= 1
            return temp.value
        } else {
            let temp: Node<T> = self.head
            self.head = self.head.next!
            self.count -= 1
            return temp.value
        }
    }
    func shift() -> T? {
        if self.isEmpty() {
            return nil
        } else if self.count == 1 {
            let temp: Node<T> = self.tail
            self.count -= 1
            return temp.value
        } else {
            let temp: Node<T> = self.tail
            self.tail = self.tail.prev!
            self.count -= 1
            return temp.value
        }
    }
}

Version data entries

325 entries across 325 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/swift/exercises/linked-list/Sources/LinkedListExample.swift
trackler-2.2.1.179 tracks/swift/exercises/linked-list/Sources/LinkedListExample.swift
trackler-2.2.1.178 tracks/swift/exercises/linked-list/Sources/LinkedListExample.swift
trackler-2.2.1.177 tracks/swift/exercises/linked-list/Sources/LinkedListExample.swift
trackler-2.2.1.176 tracks/swift/exercises/linked-list/Sources/LinkedListExample.swift
trackler-2.2.1.175 tracks/swift/exercises/linked-list/Sources/LinkedListExample.swift
trackler-2.2.1.174 tracks/swift/exercises/linked-list/Sources/LinkedListExample.swift
trackler-2.2.1.173 tracks/swift/exercises/linked-list/Sources/LinkedListExample.swift
trackler-2.2.1.172 tracks/swift/exercises/linked-list/Sources/LinkedListExample.swift
trackler-2.2.1.171 tracks/swift/exercises/linked-list/Sources/LinkedListExample.swift
trackler-2.2.1.170 tracks/swift/exercises/linked-list/Sources/LinkedListExample.swift
trackler-2.2.1.169 tracks/swift/exercises/linked-list/Sources/LinkedListExample.swift
trackler-2.2.1.167 tracks/swift/exercises/linked-list/Sources/LinkedListExample.swift
trackler-2.2.1.166 tracks/swift/exercises/linked-list/Sources/LinkedListExample.swift
trackler-2.2.1.165 tracks/swift/exercises/linked-list/Sources/LinkedListExample.swift
trackler-2.2.1.164 tracks/swift/exercises/linked-list/Sources/LinkedListExample.swift
trackler-2.2.1.163 tracks/swift/exercises/linked-list/Sources/LinkedListExample.swift
trackler-2.2.1.162 tracks/swift/exercises/linked-list/Sources/LinkedListExample.swift
trackler-2.2.1.161 tracks/swift/exercises/linked-list/Sources/LinkedListExample.swift
trackler-2.2.1.160 tracks/swift/exercises/linked-list/Sources/LinkedListExample.swift