Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sstream>
- #include "List.h"
- // NODE
- List::Node::Node(int value, Node* prev, Node* next)
- : value(value)
- , prev(prev)
- , next(next) {
- }
- int List::Node::getValue() const { return this->value; }
- void List::Node::setValue(int value) { this->value = value; }
- List::Node* List::Node::getNext() const { return next; }
- void List::Node::setNext(Node* next) { this->next = next; }
- List::Node* List::Node::getPrev() const { return prev; }
- void List::Node::setPrev(Node* prev) { this->prev = prev; }
- // LIST
- List::List()
- : head(nullptr)
- , tail(nullptr)
- , size(0) {
- }
- List::List(const List& other)
- : head(other.head)
- , tail(other.tail)
- , size(other.size) {
- }
- int List::first() const {
- return head->getValue();
- }
- void List::add(int value) {
- Node* it = tail;
- if (it == nullptr) {
- Node* newNode = new Node(value, nullptr, nullptr);
- head = tail = newNode;
- size++;
- return;
- }
- Node* currTail = tail;
- Node* newTail = new Node(value, currTail, nullptr);
- tail = newTail;
- currTail->setNext(tail);
- size++;
- }
- void List::addAll(const List& other) {
- auto ptrOther = &other;
- if (ptrOther->isEmpty()) {
- return;
- }
- Node* it = other.head;
- while (it != other.tail) {
- this->add(it->getValue());
- it = other.head->getNext();
- }
- }
- void List::removeFirst() {
- Node* oldHead = head;
- head = head->getNext();
- delete oldHead;
- oldHead = nullptr;
- }
- void List::removeAll() {
- while (head != nullptr) {
- removeFirst();
- }
- }
- size_t List::getSize() const {
- return size;
- }
- bool List::isEmpty() const {
- if (head != nullptr) {
- return false;
- }
- return true;
- }
- static List getReversed(List l) {
- }
- std::string List::toString() const {
- std::ostringstream stream;
- Node* it = head;
- while (it != nullptr) {
- stream << it->getValue() << " ";
- it = it->getNext();
- }
- return stream.str();
- }
- List& List::operator<<(const int& value) {
- this->add(value);
- return *this;
- }
- List& List::operator<<(const List& other) {
- this->addAll(other);
- return *this;
- }
- List& List::operator=(const List& other) {
- this->head = other.head;
- this->tail = other.tail;
- this->size = other.size;
- return *this;
- }
- List::~List() {
- /*
- Node* it = head;
- while (it != nullptr) {
- Node* nextIt = it->getNext();
- delete it;
- it = nextIt;
- }
- head = tail = nullptr;
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement