Stack Data Structure built in JavaScript.
Task1.js implements stackAlgorithm(stack) (Task1.js:5), a destructive reducer that consumes the input stack two elements at a time and pushes a single result until one value remains. main() (Task1.js:28) just seeds an example stack, runs the algorithm, and logs the result.
I used AI selectively and respectfully within this coursework to explain and understand core concepts, compile Git commit messages and refine the documentation below (for reference only, not submitted). All core algorithmic logic, data-structure implementation and problem solving decisions were designed and written by me.
Given the two top-most values l and s:
- Both even → push
l * s - Both odd → push
l + s - Mixed parity → push
|l - s|The loop repeats until the stack length is 1, then returns the mutated stack.
const stack = [12, 3, 5, 8];
const [result] = stackAlgorithm(stack); // stack mutated to [result]
console.log(result);Task2.js implements a hand-built singly linked list for storing the letters of a person’s full name (first, middle, last). Each node keeps the character, its name part, and its position. The exported helpers let you build the list interactively, inspect it, and render the stored name.
- Node (
Task2.js:1): wraps an individual letter with itsposition,letter,part("f","m", or"l"), andnextpointer. - LinkedList (
Task2.js:9): owns theheadpointer and provides the methods below.
| Method | Description |
|---|---|
length() |
Walks the list and returns the total number of nodes; used for validation. |
Get_element(pos) |
Returns the node at the supplied zero-based index. If pos exceeds length(), logs an error and returns null. |
Add_element(letter, part) |
Creates a new Node with position = length() + 1, appends it to the tail, and keeps head up to date. |
Add_word(word, part) |
Splits word into characters and calls Add_element for each, tagging nodes with the name section tracked by part. |
Search_element(letter) |
Traverses from head, returning the first node whose letter matches (case-sensitive) or null if absent. |
Print_list() |
Logs every node as { position, letter, part } so the full structure is visible for debugging. |
Print_full_name() |
Renders the linked list as a single string grouped by part: e.g., Luke James Pring. Useful after interactive entry. |
- Collect first/middle/last names (see the commented
readlineblock at the bottom ofTask2.js). - For each name, call
Full_Name.Add_word(name, "<part>"). - Inspect with
Full_Name.Print_list()or simply renderFull_Name.Print_full_name(). - Developers can also call
buildExampleLinkedList()to obtain a fully populated demo list (Example_Full_Name) for testing.
const list = new LinkedList();
list.Add_word("Ada", "f");
list.Add_word("Lovelace", "l");
list.Print_full_name(); // Ada Lovelace