Abstract Data Types (ADT)

This page is under construction. Please come back later.
#!/usr/bin/env node;
/******************************************************************************
* This program demonstrates the Stack ADT
*
* Copyright © 2021 Richard Lesh. All rights reserved.
*****************************************************************************/
const Utils = require('./Utils');
class IntegerStack {
// Internal representation for the stack is a list
#stack = [];
constructor() {
this.#stack = [];
}
pop() {
if (this.#stack.length === 0) {
throw new Utils.RuntimeError("Can't pop on empty stack!");
}
return this.#stack.pop();
}
push(value) {
this.#stack.push(value);
}
peek() {
if (this.#stack.length === 0) {
throw new Utils.RuntimeError("Can't peek on empty stack!");
}
return this.#stack[this.#stack.length - 1];
}
}
const main = async () => {
let stack = new IntegerStack();
for (let x = 1; x <= 10; ++x) {
stack.push(x);
}
try {
console.log(stack.peek());
for (let x = 1; x <= 11; ++x) {
console.log(stack.pop());
}
} catch (ex) {
if (ex instanceof Utils.RuntimeError) {
console.log(String(ex));
} else if (ex instanceof Error) {
console.log("Unknown Exception!");
}
}
}
main().catch( e => { console.error(e) } );
Output
$ node ADTStack.js
10
10
9
8
7
6
5
4
3
2
1
RuntimeError: Can't pop on empty stack!
#!/usr/bin/env node;
/******************************************************************************
* Simple first program.
*
* Copyright © 2020 Richard Lesh. All rights reserved.
*****************************************************************************/
const main = async () => {
console.log("Hello, world!");
}
main().catch( e => { console.error(e) } );
Output
$ node HelloWorld.js
Hello, world!
#!/usr/bin/env node;
/******************************************************************************
* Simple first program.
*
* Copyright © 2020 Richard Lesh. All rights reserved.
*****************************************************************************/
const main = async () => {
console.log("Hello, world!");
}
main().catch( e => { console.error(e) } );
Output
$ node HelloWorld.js
Hello, world!
Questions
Projects
More ★'s indicate higher difficulty level.
References
- [[JavaScript Language Reference]], Mozilla Developer Network
- [[Mozilla Developer Network]]
- Download [[node.js]]
- [[w3schools.com]]
Pure Programmer


