forward_list


1. Implementing a Stack with Forward Lists

struct Node {
    int value;
    Node* next;
};

class Stack {
public:
    Stack() : head(nullptr) {}
    void push(int value) {
        Node* new_node = new Node{value, head};
        head = new_node;
    }
    int pop() {
        if (head == nullptr) throw std::runtime_error("Empty stack");
        int value = head->value;
        Node* next_node = head->next;
        delete head;
        head = next_node;
        return value;
    }
    int peek() const {
        if (head == nullptr) throw std::runtime_error("Empty stack");
        return head->value;
    }
    bool empty() const { return head == nullptr; }

private:
    Node* head;
};

2. Implementing a Queue with Forward Lists

3. Creating a Doubly Linked List with Forward Lists

4. Implementing a Priority Queue with Forward Lists

5. Creating a Circular Forward List