Pure Programmer
Blue Matrix


Cluster Map

Abstract Data Types (ADT)

L1

This page is under construction. Please come back later.

ADTStack.swift
#!/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)
HelloWorld.swift
#!/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)
HelloWorld.swift
#!/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)
swift

Questions

Projects

More ★'s indicate higher difficulty level.

References