Sha256: c3cd2aa46fb0770580f9594a9f00180b8184c8fd3a4645b20a6b0c887922fc57

Contents?: true

Size: 1.75 KB

Versions: 71

Compression:

Stored size: 1.75 KB

Contents

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

    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

71 entries across 71 versions & 1 rubygems

Version Path
trackler-2.0.3.5 tracks/swift/exercises/linked-list/LinkedListExample.swift
trackler-2.0.3.4 tracks/swift/exercises/linked-list/LinkedListExample.swift
trackler-2.0.3.3 tracks/swift/exercises/linked-list/LinkedListExample.swift
trackler-2.0.3.2 tracks/swift/exercises/linked-list/LinkedListExample.swift
trackler-2.0.3.1 tracks/swift/exercises/linked-list/LinkedListExample.swift
trackler-2.0.3.0 tracks/swift/exercises/linked-list/LinkedListExample.swift
trackler-2.0.2.0 tracks/swift/exercises/linked-list/LinkedListExample.swift
trackler-2.0.1.2 tracks/swift/exercises/linked-list/LinkedListExample.swift
trackler-2.0.1.1 tracks/swift/exercises/linked-list/LinkedListExample.swift
trackler-2.0.1.0 tracks/swift/exercises/linked-list/LinkedListExample.swift
trackler-2.0.0.10 tracks/swift/exercises/linked-list/LinkedListExample.swift
trackler-2.0.0.9 tracks/swift/exercises/linked-list/LinkedListExample.swift
trackler-2.0.0.8 tracks/swift/exercises/linked-list/LinkedListExample.swift
trackler-2.0.0.7 tracks/swift/exercises/linked-list/LinkedListExample.swift
trackler-2.0.0.6 tracks/swift/exercises/linked-list/LinkedListExample.swift
trackler-2.0.0.5 tracks/swift/exercises/linked-list/LinkedListExample.swift
trackler-2.0.0.4 tracks/swift/exercises/linked-list/LinkedListExample.swift
trackler-2.0.0.3 tracks/swift/exercises/linked-list/LinkedListExample.swift
trackler-2.0.0.2 tracks/swift/exercises/linked-list/LinkedListExample.swift
trackler-2.0.0.1 tracks/swift/exercises/linked-list/LinkedListExample.swift