Abstract Data Types (ADT)
This page is under construction. Please come back later.
#!/usr/bin/env swift; /****************************************************************************** * This program demonstrates the Stack ADT * * Copyright © 2021 Richard Lesh. All rights reserved. *****************************************************************************/ import Foundation import Utils class IntegerStack { // Internal representation for the stack is a list private var stack:[Int] = [] public init() { stack = [] } public func pop() throws -> Int { if stack.count == 0 { throw RuntimeError("Can't pop on empty stack!") } return stack.removeLast() } public func push(_ value:Int) -> Void { stack.append(value) } public func peek() throws -> Int { if stack.count == 0 { throw RuntimeError("Can't peek on empty stack!") } return stack[stack.count - 1] } } // Begin Main var stack:IntegerStack = IntegerStack() for x:Int in 1...10 { stack.push(x) } do { print(String(try stack.peek())) for _:Int in 1...11 { print(String(try stack.pop())) } } catch let ex as RuntimeError { print(ex.localizedDescription) } exit(EXIT_SUCCESS)
Output
$ swiftc ADTStack.swift -I . -L . -lUtils
error: link command failed with exit code 1 (use -v to see invocation)
ld: library not found for -lUtils
clang: error: linker command failed with exit code 1 (use -v to see invocation)
#!/usr/bin/env swift; /****************************************************************************** * Simple first program. * * Copyright © 2020 Richard Lesh. All rights reserved. *****************************************************************************/ import Foundation // Begin Main print("Hello, world!") exit(EXIT_SUCCESS)
Output
$ swiftc HelloWorld.swift -I . -L . -lUtils
error: link command failed with exit code 1 (use -v to see invocation)
ld: library not found for -lUtils
clang: error: linker command failed with exit code 1 (use -v to see invocation)
#!/usr/bin/env swift; /****************************************************************************** * Simple first program. * * Copyright © 2020 Richard Lesh. All rights reserved. *****************************************************************************/ import Foundation // Begin Main print("Hello, world!") exit(EXIT_SUCCESS)
Output
$ swiftc HelloWorld.swift -I . -L . -lUtils
error: link command failed with exit code 1 (use -v to see invocation)
ld: library not found for -lUtils
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Questions
Projects
More ★'s indicate higher difficulty level.
References
- [[Swift Community]]
- [[Swift Language Guide]]
- [[Swift Language Reference]]
- [[Swift Programming Language]], Apple Inc.
- [[Swift Doc]]
- [[We Heart Swift]]
- [[Swift Cookbook]]
- [[Swift Playground]]
- [[Swift at TutorialsPoint]]
- [[Hacking with Swift]]