---
description: 🚧 under construction
---

# Stack

[data structure]() ⟩ Stack

{% tabs %}
{% tab title="👥 相關" %}
* [queue](queue "mention")
{% endtab %}

{% tab title="⬇️ 應用" %}
* [waitinglist](waitinglist "mention") - a Stack or a Queue.
{% endtab %}

{% tab title="📗 參考" %}
* [ ] Advanced Swift (2017.11) ⟩&#x20;
* [ ] Data Structures and Algorithms in Swift ⟩&#x20;
{% endtab %}
{% endtabs %}

## implementations

{% tabs %}
{% tab title="JS" %}
* replit ⟩ [WaitingList (js)](https://replit.com/@pegasusroe/StackOrQueue#DataStructure/StackOrQueue.js)

```javascript
// ⭐ Stack
//    2022.12.28 - ??:?? - first version
// ----------------------------
// 🔹 .push(a, b, c ...)
// 🔹 .pop()
// 🔹 .enqueue(a, b, c ...)    // speaks in Queue's language
// 🔹 .dequeue()               // (same)
// 🔸 .first
// 🔸 .length
// 🔸 .isEmpty
// ----------------------------
// 🔹 .toString()
// 🔹 .log()
//
class Stack {

    // 🔸 private members
    #elements = [];    // stack elements
    
    // ⭐ const q = new Queue(a, b, c ...)
    constructor(...elements) {
        this.push(...elements);
    }
    
    // 🔹 stack.push(a, b, c ...)
    push(...elements) {
        for (const element of elements) {
            this.#elements.push(element);
        }
    }
    
    // 🔹 stack.pop()
    pop() {
        if (this.isEmpty) return null;
        return this.#elements.pop();
    }
    
    // 🔸 stack.first
    get first() {
        if (this.isEmpty) return null;
        return this.#elements[this.length - 1];
    }
    
    // 🔸 stack.length
    get length() {
        return this.#elements.length;
    }
    
    // 🔸 stack.isEmpty
    get isEmpty() {
        return this.length === 0;
    }

    // 🔹 stack.toString()
    toString() {
        return `Stack: [${this.#elements} ->`;
    }

    // -----------------------------
    //   speak in Queue's language
    // -----------------------------

    enqueue(...elements) { this.push(...elements) }
    dequeue() { return this.pop() }

    // -------------
    //   dubugging
    // -------------

    // 🔹 .log()
    log() { console.log(this.toString()) }

    // 🔸 .debugInfo
    get debugInfo() { return '' }
}

module.exports = { Stack };
```
{% endtab %}

{% tab title="Swift" %}
* replit ⟩ [Stack (swift)](https://replit.com/@pegasusroe/Stack#Stack/Stack.swift)&#x20;

```swift
// code
```

* replit ⟩ StackProtocol (swift)

```swift
// code
```
{% endtab %}
{% endtabs %}
