From 22745559d7dbc0394d71d6b14a587f88f8c5fdd0 Mon Sep 17 00:00:00 2001 From: CardinalPrefect Date: Tue, 15 Dec 2020 17:58:31 +0300 Subject: [PATCH 1/3] The solution of task list --- module-1/homework/List/list.cpp | 250 +++++++++++++++++++++++++++++++- module-1/homework/List/list.h | 39 ++++- 2 files changed, 282 insertions(+), 7 deletions(-) diff --git a/module-1/homework/List/list.cpp b/module-1/homework/List/list.cpp index 15252d3e..39c1c4f7 100644 --- a/module-1/homework/List/list.cpp +++ b/module-1/homework/List/list.cpp @@ -1,3 +1,251 @@ #include "list.h" +#include +#include +#include +//#include -// Your code goes here... \ No newline at end of file +using namespace task; + +list::list() { } + +list::list(const list& other) { + Node* x = other.head; + while (x) { + push_back(x->value); + x = x->next; + } +} + +list::list(list&& other) noexcept { + head = other.head; + tail = other.tail; + list_size = other.list_size; + other.head = other.tail = nullptr; + other.list_size = 0; +} + +list::list(size_t count, const int& value) { + for (std::size_t i = 0; i < count; ++i) + push_back(value); +} + +list::~list() { + clear(); +} + +list& list::operator=(const list& other) { + if (this == &other) + return *this; + + clear(); + Node* x = other.head; + while (x) { + push_back(x->value); + x = x->next; + } + return *this; +} + +list& list::operator=(list&& other) noexcept { + if (this == &other) + return *this; + + clear(); + head = other.head; + tail = other.tail; + list_size = other.list_size; + other.head = other.tail = nullptr; + other.list_size = 0; + return *this; +} + + +int& list::front() { + return head->value; +} + +const int& list::front() const { + return head->value; +} + +int& list::back() { + return tail->value; +} + +const int& list::back() const { + return tail->value; +} + +bool list::empty() const { + return list_size == 0; +} + +size_t list::size() const { + return list_size; +} + +void list::clear() { + Node* x = head; + Node* nx = nullptr; + while (x) { + nx = x->next; + delete x; + x = nx; + } + + head = tail = nullptr; + list_size = 0; +} + +void list::push_back(const int& value) { + if (list_size == 0) + head = tail = new Node(value, nullptr, nullptr); + else { + tail->push_back(new Node(value, tail, nullptr)); + tail = tail->next; + } + ++list_size; +} + +void list::pop_back() { + if (list_size == 1) { + delete head; + head = tail = nullptr; + --list_size; + } else if (list_size > 1) { + Node* x = tail; + tail = tail->prev; + tail->next = nullptr; + delete x; + --list_size; + } +} + +void list::push_front(const int& value) { + if (list_size == 0) + head = tail = new Node(value, nullptr, nullptr); + else { + head->push_front(new Node(value, nullptr, head)); + head = head->prev; + } + ++list_size; +} + +void list::pop_front() { + if (list_size == 1) { + delete head; + head = tail = nullptr; + --list_size; + } else if (list_size > 1) { + Node* x = head; + head = head->next; + head->prev = nullptr; + delete x; + --list_size; + } +} + +void list::resize(size_t count) { + if (count > list_size) { + while (count - list_size != 0) + push_back(0); + } else { + while (count - list_size != 0) + pop_back(); + } +} + +void list::swap(list& other) { + std::swap(head, other.head); + std::swap(tail, other.tail); + std::swap(list_size, other.list_size); +} + +void list::remove(const int& value) { + int val = value; + Node* x = head; + + while(x) { + if (x->value == val) { + if (x == head) { + if (head == tail) { + head = tail = nullptr; + } else { + head = head->next; + if (head) + head->prev = nullptr; + } + } else { + x->prev->push_back(x->next); + if (x == tail) + tail = x->prev; + } + + auto dx = x; + + x = x->next; + + delete dx; + --list_size; + } else { + x = x->next; + } + } +} + +void list::unique() { + if (list_size < 2) + return; + int pred = head->value; + Node* x = head->next; + Node* delx = nullptr; + + while (x) { + if (x->value == pred) { + delx = x; + x->prev->push_back(x->next); + x = x->next; + delete delx; + --list_size; + } else { + pred = x->value; + x = x->next; + } + } +} + +void list::sort() { + std::vector values; + + Node* x = head; + while (x) { + values.push_back(x->value); + x = x->next; + } + + std::sort(values.begin(), values.end()); + + clear(); + + for (int& d: values) + push_back(d); +} + +//int main() { +// task::list l; +// std::vector a = {0, 0, 0, 1, 0, 0, 1, 0, 3, 3}; + +// for (auto i: a) +// l.push_back(i); + +// l.remove(l.front()); + +// Node* x = l.head; + +// while (x) { +// std::cout << x->value << std::endl; +// x = x->next; +// } + +// return 0; +//} diff --git a/module-1/homework/List/list.h b/module-1/homework/List/list.h index a5cbaa35..41867062 100644 --- a/module-1/homework/List/list.h +++ b/module-1/homework/List/list.h @@ -3,17 +3,45 @@ namespace task { - +class Node { +public: + Node(int value, Node* prev, Node* next): + value(value), prev(prev), next(next) { } + + void push_back(Node* node) { + next = node; + if (node) + node->prev = this; + } + + void push_front(Node* node) { + prev = node; + if (node) + node->next = this; + } + + ~Node() { } +//private: + int value = 0; + Node* prev = nullptr; + Node* next = nullptr; + + friend class list; +}; class list { + public: list(); + list(const list& other); + list(list&& other) noexcept; list(size_t count, const int& value = int()); ~list(); list& operator=(const list& other); + list& operator=(list&& other) noexcept; int& front(); @@ -40,11 +68,10 @@ class list { void unique(); void sort(); - // Your code goes here?.. - -private: - - // Your code goes here... +//private: + Node* head = nullptr; + Node* tail = nullptr; + std::size_t list_size = 0; }; From af06c2deeeb3e9bbaaa50b05ee4e6313a929d045 Mon Sep 17 00:00:00 2001 From: CardinalPrefect Date: Tue, 15 Dec 2020 18:02:18 +0300 Subject: [PATCH 2/3] Deleted comments --- module-1/homework/List/list.cpp | 20 -------------------- module-1/homework/List/list.h | 4 ++-- 2 files changed, 2 insertions(+), 22 deletions(-) diff --git a/module-1/homework/List/list.cpp b/module-1/homework/List/list.cpp index 39c1c4f7..fdd1236a 100644 --- a/module-1/homework/List/list.cpp +++ b/module-1/homework/List/list.cpp @@ -2,7 +2,6 @@ #include #include #include -//#include using namespace task; @@ -230,22 +229,3 @@ void list::sort() { for (int& d: values) push_back(d); } - -//int main() { -// task::list l; -// std::vector a = {0, 0, 0, 1, 0, 0, 1, 0, 3, 3}; - -// for (auto i: a) -// l.push_back(i); - -// l.remove(l.front()); - -// Node* x = l.head; - -// while (x) { -// std::cout << x->value << std::endl; -// x = x->next; -// } - -// return 0; -//} diff --git a/module-1/homework/List/list.h b/module-1/homework/List/list.h index 41867062..bbc3cd17 100644 --- a/module-1/homework/List/list.h +++ b/module-1/homework/List/list.h @@ -21,7 +21,7 @@ class Node { } ~Node() { } -//private: +private: int value = 0; Node* prev = nullptr; Node* next = nullptr; @@ -68,7 +68,7 @@ class list { void unique(); void sort(); -//private: +private: Node* head = nullptr; Node* tail = nullptr; std::size_t list_size = 0; From 03d4ed2caddfcf616e047170c506c618bec7881d Mon Sep 17 00:00:00 2001 From: CardinalPrefect Date: Mon, 21 Dec 2020 19:30:42 +0300 Subject: [PATCH 3/3] Fixed empties --- module-1/homework/List/list.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/module-1/homework/List/list.cpp b/module-1/homework/List/list.cpp index fdd1236a..bbfbf43e 100644 --- a/module-1/homework/List/list.cpp +++ b/module-1/homework/List/list.cpp @@ -97,7 +97,7 @@ void list::clear() { } void list::push_back(const int& value) { - if (list_size == 0) + if (empty()) head = tail = new Node(value, nullptr, nullptr); else { tail->push_back(new Node(value, tail, nullptr)); @@ -121,7 +121,7 @@ void list::pop_back() { } void list::push_front(const int& value) { - if (list_size == 0) + if (empty()) head = tail = new Node(value, nullptr, nullptr); else { head->push_front(new Node(value, nullptr, head));