Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <functional>
- using namespace std;
- struct Node
- {
- int value = 0;
- Node * prev = nullptr;
- Node * next = nullptr;
- };
- struct NodeList
- {
- Node* head = nullptr;
- Node* tail = nullptr;
- Node* curr = nullptr;
- void add(int val)
- {
- if (!head)
- {
- head = new Node{ val };
- tail = head;
- return;
- }
- if (!tail->next)
- {
- tail->next = new Node{ val, tail };
- tail = tail->next;
- return;
- }
- };
- void show()
- {
- Node* ptr = head;
- if (!ptr) std::cout << "Empty NodeList!" << std::endl;
- while (ptr)
- {
- std::cout << ptr->value << std::endl;
- ptr = ptr->next;
- }
- }
- void showReverse()
- {
- Node* ptr = tail;
- if (!ptr) std::cout << "Empty NodeList!" << std::endl;
- while (ptr)
- {
- std::cout << ptr->value << std::endl;
- ptr = ptr->prev;
- }
- }
- void deleteLast()
- {
- Node* ptr = nullptr;
- if (!tail) std::cout << "Empty NodeList!" << std::endl;
- ptr = tail;
- tail = tail->prev;
- tail->next = nullptr;
- std::cout << "Deleting.. " << ptr->value;
- delete ptr;
- std::cout << " Done!" << std::endl;
- }
- void deleteAll()
- {
- Node* ptr = nullptr;
- if (!tail) std::cout << "Empty NodeList!" << std::endl;
- while (tail)
- {
- ptr = tail;
- tail = tail->prev;
- std::cout << "Deleting.. " << ptr->value;
- delete ptr;
- std::cout << " Done!" << std::endl;
- }
- head = nullptr;
- tail = nullptr;
- }
- void forEach(std::function <void(int&)> funct)
- {
- Node* ptr = head;
- if (!ptr) std::cout << "Empty NodeList!" << std::endl;
- while (ptr)
- {
- funct(ptr->value);
- ptr = ptr->next;
- }
- }
- void forEachReverse(std::function <void(int&)> funct)
- {
- Node* ptr = tail;
- if (!ptr) std::cout << "Empty NodeList!" << std::endl;
- while (ptr)
- {
- funct(ptr->value);
- ptr = ptr->prev;
- }
- }
- int count()
- {
- Node* ptr = head;
- if (!ptr) return 0;
- int cnt = 0;
- while (ptr)
- {
- cnt++;
- ptr = ptr->next;
- }
- return cnt;
- }
- void begin()
- {
- curr = head;
- }
- void end()
- {
- curr = tail;
- }
- Node* get()
- {
- return curr;
- }
- Node* getNext()
- {
- curr = curr->next;
- return curr;
- }
- Node* getPrev()
- {
- curr = curr->prev;
- return curr;
- }
- };
- int main()
- {
- NodeList* list = new NodeList;
- list->show();
- list->add(2902);
- list->add(2407);
- list->add(2802);
- list->add(2607);
- list->show();
- std::cout << "####" << std::endl;
- list->showReverse();
- list->deleteAll();
- list->showReverse();
- list->add(29022);
- list->add(24073);
- list->add(28024);
- list->add(26075);
- list->show();
- std::cout << "####" << std::endl;
- list->deleteLast();
- list->forEach([](int& arg) {arg = arg + 1; });
- list->forEachReverse([](int& arg) {std::cout << arg << std::endl; });
- std::cout << "There are: " << list->count() << " nodes" << std::endl;
- list->begin();
- for (int i = 0; i < list->count(); i++)
- {
- std::cout << "Iteration " << i << " " << list->get()->value << std::endl;
- list->getNext();
- }
- std::cout << "####" << std::endl;
- list->end();
- for (int i = 0; i < list->count(); i++)
- {
- std::cout << "Iteration " << i << " " << list->get()->value << std::endl;
- list->getPrev();
- }
- list->deleteAll();
- list->show();
- }
Advertisement
Add Comment
Please, Sign In to add comment