Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // !voteban Dmitrii Stasenko :( --
- // ++
- #include <iostream>
- template<class T>
- class LinkedList {
- class Node {
- T x;
- Node* left;
- Node* right;
- };
- struct Iterator {
- Node* curNode = nullptr;
- Iterator();
- Iterator(Node*);
- T& operator* ();
- ~Iterator() = default;
- Iterator& operator++ ();
- Iterator& operator-- ();
- bool operator== (const Iterator&) const;
- bool operator!= (const Iterator&) const;
- };
- public:
- ~LinkedList();
- Iterator begin() const;
- Iterator begin();
- Iterator end() const;
- Iterator end();
- private:
- Node* begin_node;
- Node* end_node;
- public:
- /*LinkedList();
- Iterator push_front(const T& elem);
- T pop_front();*/
- Iterator push_back(const T& elem);
- /*T pop_back();
- [[nodiscard]] size_t size() const;
- Iterator insert(const T& elem, size_t index);
- T front() const;
- T& front();
- T back() const;
- T& back();
- Iterator begin() const;
- Iterator end() const;
- const Iterator begin();
- const Iterator end();*/
- };
- // напиши рут нод head
- template<class T>
- LinkedList<T>::Iterator LinkedList<T>::Iterator::begin() {
- return Iterator(begin_node);
- }
- template<class T>
- LinkedList<T>::Iterator LinkedList<T>::Iterator::end() {
- return Iterator(end_node->right);
- }
- template<class T>
- LinkedList<T>::Iterator::Iterator() {
- curNode = nullptr;
- }
- template<class T>
- LinkedList<T>::Iterator::Iterator(Node* x) {
- curNode = x;
- }
- template<class T>
- T& LinkedList<T>::Iterator::operator* () {
- return (*curNode); // curNode->x ?
- }
- template<class T>
- bool LinkedList<T>::Iterator::operator== (const Iterator& elem) {
- return elem.curNode == curNode;
- }
- template<class T>
- bool LinkedList<T>::Iterator::operator!= (const Iterator& elem) {
- return !(*this == elem);
- }
- template<class T>
- LinkedList<T>::Iterator& LinkedList<T>::Iterator::operator++ () {
- curNode = curNode->right;
- return *this;
- }
- template<class T>
- LinkedList<T>::Iterator& LinkedList<T>::Iterator::operator-- () {
- curNode = curNode->left;
- return *this;// curNode = curNode->right; return *this
- }
- template<class T>
- LinkedList<T>::Iterator LinkedList<T>::push_back(const T& elem) {
- Node* cur = new Node(elem);
- end_node->right = cur;
- cur->left = end_node;
- end_node = cur;
- return Iterator(end_node);
- }
- template<class T>
- LinkedList<T>::~LinkedList() {
- Node* ptr = begin_node;
- Node* was;
- while (ptr != nullptr) {
- was = ptr;
- ptr = ptr->right;
- delete was;
- }
- }
- //напиши !=
- int main(){
- LinkedList<int> l;
- for (int i = 0; i < 10; ++i) {
- l.push_back(i);
- }
- int count = 0;
- for (auto it = l.begin(); it != l.end(); ++it) {
- ++count;
- }
- std::cout << count << '\n';
- std::cout << "Nice\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement