iterator_final

 avatar
Alexmegawin
c_cpp
a year ago
3.7 kB
1
Indexable
Never
#include <iostream>
using namespace std;

class List
{
private:
    class Node
    {
        friend class List;
        friend class Iterator;
    public:
        Node(int value)
        {
            this->value = value;
            this->next_node = nullptr;
        }
        Node(int value, Node* next_node)
        {
            this->value = value;
            this->next_node = next_node;
        }
    private:
        int value;
        Node* next_node;
    };

public:
    class Iterator
    {
        friend class List;
    public:
        Iterator(Node* current, List* list)
        {
            this->current = current;
            this->list = list;
        }
    private:
        List* list;
        Node* current;
    public:
        Node* getNextNode()
        {
            return current->next_node;
        }

        void setNextNode(Node* node)
        {
            current->next_node = node;
        }

        int getValue()
        {
            return current->value;
        }

        void goNext()
        {
            current = current->next_node;
        }

        bool isValid()
        {
            return current != nullptr;
        }
    };

public:
    List()
    {
        head = nullptr;
        lenght = 0;
    }
private:
    Node* head;
    int lenght;
public:
    Iterator begin()
    {
        Iterator iterator(head, this);
        return iterator;
    }
    void addToHead(int value)
    {
        Node* new_head = new Node(value, head);
        head = new_head;
        lenght++;
    }

    int getLenght()
    {
        return lenght;
    }

    void addNode(int value, Iterator current)
    {
        Node* new_nod = new Node(value, current.getNextNode());
        current.setNextNode(new_nod);
        lenght++;
    }

    int deleteNode(Iterator current) 
    {
        int deleted_value;
        Node* pre_node = current.getNextNode();
        current.setNextNode(pre_node->next_node);
        
        deleted_value = pre_node->value;

        delete pre_node;
        lenght--;
        
        return deleted_value;
    }

    bool deleteFromHead(int* result)
    {
        if (lenght == 0)
        {
            return false;
        }

        Node* pre_head = head;
        head = head->next_node;

        *result = pre_head->value;

        delete pre_head;
        lenght--;

        return true;
    }

    void printList()
    {
        Node* current = head;
        while (current != nullptr)
        {
            cout << current->value << " ";
            current = current->next_node;
        }
    }
};


int main() {
    int num;

    List list;
    for (int i = 0; i < 5; i++)
    {
        list.addToHead(i);
    }

    list.printList();

    cout << endl;

    List::Iterator iterator = list.begin();

    //cout << iterator.getValue() << " ";
    list.addNode(100, iterator);
    //cout << iterator.getValue() << " ";
    iterator.goNext();
    //cout << iterator.getValue() << " ";

    iterator = list.begin();

    for (int i = 0; i < list.getLenght(); i++)
    {
        cout << iterator.getValue() << " ";
        iterator.goNext();
    }
    cout << endl;

    iterator = list.begin();
    for (int i = 0; i < 3; i++)
    {
        cout << iterator.getValue() << " ";
        iterator.goNext();
    }
    num = list.deleteNode(iterator);
    for (int i = 0; i < 2; i++)
    {
        cout << iterator.getValue() << " ";
        iterator.goNext();
    }
    cout << endl << num;

    return 0;
}