online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
#include <iostream> #include <algorithm> template<typename T> class Forward_list { struct Node { T data_; Node* next_; Node(const T& data) : data_(data), next_(nullptr) {} }; public: class const_iterator; Forward_list() : head_(nullptr), last_(nullptr) {} ~Forward_list() { clear(); } void push_back(const T& value) { Node* new_node = new Node(value); if (!head_) { head_ = new_node; last_ = new_node; new_node->next_ = head_; // Указываем на себя } else { last_->next_ = new_node; new_node->next_ = head_; last_ = new_node; } } void clear() { if (!head_) return; Node* current = head_; Node* next_node; do { next_node = current->next_; delete current; current = next_node; } while (current != head_); head_ = nullptr; last_ = nullptr; } const_iterator begin() const { return const_iterator(head_, last_); } const_iterator end() const { return const_iterator(nullptr, last_); // Указываем на nullptr для конца } class const_iterator { public: const_iterator(Node* node, Node* last) : current_(node), last_(last) {} const T& operator*() const { return current_->data_; } const_iterator& operator++() { if (current_ && current_ != last_) { current_ = current_->next_; } else { current_ = nullptr; } return *this; } bool operator!=(const const_iterator& other) const { return current_ != other.current_; } bool operator==(const const_iterator& other) const { return current_ == other.current_; } private: Node* current_; Node* last_; // Храним указатель на последний элемент }; private: Node* head_; Node* last_; }; int main() { Forward_list<int> list; list.push_back(1); list.push_back(2); list.push_back(3); // Используем стандартный алгоритм std::for_each(list.begin(), list.end(), [](int value) { std::cout << value << " "; }); std::cout << std::endl; return 0; }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue