From 3c81e17ed3695a437cebcaa3b54ab6fb5ad8bd34 Mon Sep 17 00:00:00 2001 From: Grif-cpp <71193593+Grif-cpp@users.noreply.github.com> Date: Fri, 20 Nov 2020 22:55:53 +0300 Subject: [PATCH 1/5] Update list.cpp --- module-1/homework/List/list.cpp | 222 +++++++++++++++++++++++++++++++- 1 file changed, 220 insertions(+), 2 deletions(-) diff --git a/module-1/homework/List/list.cpp b/module-1/homework/List/list.cpp index 15252d3e..1b0c7f26 100644 --- a/module-1/homework/List/list.cpp +++ b/module-1/homework/List/list.cpp @@ -1,3 +1,221 @@ #include "list.h" - -// Your code goes here... \ No newline at end of file +#include +class List { +private: + struct Element { + int data; + Element* prev; + Element* next; + + }; + Element* first; +public: + void swap(Element* a, Element* b) { + if (a == nullptr) { + a = b; + b = nullptr; + } + else if (b == nullptr) { + b = a; + a = nullptr; + } + else if (a == nullptr && b == nullptr) + { + return; + } + else { + Element* temp; + temp = a->prev; + a->prev = b->prev; + b->prev = temp; + temp = a->next; + a->next = b->next; + b->next = temp; + int t = a->data; + a->data = b->data; + b->data = t; + } + } + void swap(int& a, int& b) { + int c = a; + a = b; + b = c; + } + List() { + first = nullptr; + } + int& front() { + //if (first == nullptr)return; + return first->data; + } + int& back() { + Element* e = first; + //if (e == nullptr)return ; + while (e->next != nullptr) + e = e->next; + return e->data; + } + bool empty() { + return(first == nullptr); + } + int size() { + int i = 1; + Element* e = first; + if (e == nullptr)return 0; + while (e->next != nullptr) + { + e = e->next; + i++; + } + return i; + } + void push_back(int data_) { + Element* e = first; + while (e->next != nullptr) + e = e->next; + Element* newone; + e->next = newone; + newone->prev = e; + newone->data = data_; + } + void pop_back() { + Element* e = first; + if (e == nullptr)return; + while (e->next != nullptr) + e = e->next; + e->prev->next = nullptr; + delete e; + } + void push_front(int data) { + if (first == nullptr) { + first = new Element(); + first->prev = nullptr; + first->next = nullptr; + first->data = data; + } + else { + Element* e = new Element(); + e->data = data; + e->prev = nullptr; + e->next = first; + first->prev = e; + first = e; + } + } + void pop_front() { + if (first == nullptr)return; + first->next->prev = nullptr; + delete first; + } + void resize(int new_size) { + int i = 0; + Element* e = first; + Element*e1=nullptr; + Element* temp; + if (new_size == 0)first = nullptr; + if (e == nullptr)return; + while (e!= nullptr) + { + if (i == new_size) + { + if(e->prev!=nullptr)e->prev->next = nullptr; + temp = e; + e = e->next; + delete temp; + i++; + continue; + } + if (e->next == nullptr)e1 = e; + e = e->next; + i++; + } + for (int j = i; j < new_size; j++) { + temp = new Element(); + temp->prev = e1; + temp->next = nullptr; + if(e1!=nullptr)e1->next = temp; + e1 = temp; + } + } + void Print() { + if (first != nullptr) { + Element* e = first; + while (e) { + std::cout << e->data << ' '; + e = e->next; + } + } + } + void clear() { + while (first) { + Element* temp = first->next; + delete first; + first = temp; + } + } + void swap(List &b) { + List temp; + temp.first = first; + first = b.first; + b.first = temp.first; + } + void remove(int data_) { + Element* e = first; + Element* temp; + while (e != nullptr) { + if (e->data == data_) { + if (e->prev != nullptr) { + e->prev->next = e->next; + } + if (e->next != nullptr) { + e->next->prev = e->prev; + } + temp = e; + e = e->next; + if (temp == first) + { + first = first->next; + } + delete temp; + } + else e = e->next; + } + } + void sort() { + Element* e = first; + Element* temp; + Element* temp1; + int n=this->size(); + for (int i = 0; i < n; i++) { + for (int j = 0; j < n-1; j++) { + if (e->data > e->next->data) + { + swap(e->data, e->next->data); + } + e = e->next; + } + e = first; + } + } + void unique() { + Element* e = first->next; + Element* temp = first->next; + while (e != nullptr) { + if (e->prev->data == e->data) { + e->prev->next = e->next; + if (e->next != nullptr) + { + e->next->prev = e->prev; + } + temp = e; + e = e->next; + delete temp; + } + else e = e->next; + } + } +}; +int main() +{ + return 0; +} From 39bf009755567434cda1966056cd278ccfa53fec Mon Sep 17 00:00:00 2001 From: Grif-cpp <71193593+Grif-cpp@users.noreply.github.com> Date: Sun, 22 Nov 2020 14:12:57 +0300 Subject: [PATCH 2/5] Update list.h --- module-1/homework/List/list.h | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/module-1/homework/List/list.h b/module-1/homework/List/list.h index a5cbaa35..1aff0cf6 100644 --- a/module-1/homework/List/list.h +++ b/module-1/homework/List/list.h @@ -8,7 +8,6 @@ namespace task { class list { public: - list(); list(size_t count, const int& value = int()); @@ -39,11 +38,19 @@ class list { void remove(const int& value); void unique(); void sort(); - + // Your code goes here?.. + void swap(Element* a, Element* b); + void swap(int& a, int& b); private: - +struct Element { + int data; + Element* prev; + Element* next; + + }; + Element* first; // Your code goes here... }; From cd3dc549aece010fd7cec0f3ae7220255982fc6a Mon Sep 17 00:00:00 2001 From: Grif-cpp <71193593+Grif-cpp@users.noreply.github.com> Date: Sun, 22 Nov 2020 14:14:19 +0300 Subject: [PATCH 3/5] Update list.cpp --- module-1/homework/List/list.cpp | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/module-1/homework/List/list.cpp b/module-1/homework/List/list.cpp index 1b0c7f26..b11a52c4 100644 --- a/module-1/homework/List/list.cpp +++ b/module-1/homework/List/list.cpp @@ -1,16 +1,6 @@ #include "list.h" #include -class List { -private: - struct Element { - int data; - Element* prev; - Element* next; - - }; - Element* first; -public: - void swap(Element* a, Element* b) { +void swap(Element* a, Element* b) { if (a == nullptr) { a = b; b = nullptr; @@ -214,8 +204,3 @@ class List { else e = e->next; } } -}; -int main() -{ - return 0; -} From 19eb79a799386f6b67bfe333c942336f99d27464 Mon Sep 17 00:00:00 2001 From: Grif-cpp <71193593+Grif-cpp@users.noreply.github.com> Date: Thu, 10 Dec 2020 20:40:42 +0300 Subject: [PATCH 4/5] Update list.cpp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit исправил некоторые ошибки --- module-1/homework/List/list.cpp | 63 ++++++--------------------------- 1 file changed, 11 insertions(+), 52 deletions(-) diff --git a/module-1/homework/List/list.cpp b/module-1/homework/List/list.cpp index b11a52c4..c05998ec 100644 --- a/module-1/homework/List/list.cpp +++ b/module-1/homework/List/list.cpp @@ -1,46 +1,13 @@ #include "list.h" #include -void swap(Element* a, Element* b) { - if (a == nullptr) { - a = b; - b = nullptr; - } - else if (b == nullptr) { - b = a; - a = nullptr; - } - else if (a == nullptr && b == nullptr) - { - return; - } - else { - Element* temp; - temp = a->prev; - a->prev = b->prev; - b->prev = temp; - temp = a->next; - a->next = b->next; - b->next = temp; - int t = a->data; - a->data = b->data; - b->data = t; - } - } - void swap(int& a, int& b) { - int c = a; - a = b; - b = c; - } - List() { +List() { first = nullptr; } int& front() { - //if (first == nullptr)return; return first->data; } int& back() { Element* e = first; - //if (e == nullptr)return ; while (e->next != nullptr) e = e->next; return e->data; @@ -98,7 +65,7 @@ void swap(Element* a, Element* b) { delete first; } void resize(int new_size) { - int i = 0; + int temp_size = 0; Element* e = first; Element*e1=nullptr; Element* temp; @@ -106,36 +73,28 @@ void swap(Element* a, Element* b) { if (e == nullptr)return; while (e!= nullptr) { - if (i == new_size) + if (temp_size == new_size) { if(e->prev!=nullptr)e->prev->next = nullptr; temp = e; e = e->next; delete temp; - i++; + temp_size++; continue; } if (e->next == nullptr)e1 = e; e = e->next; - i++; + temp_size++; } - for (int j = i; j < new_size; j++) { + for (size_t j = temp_size; j < new_size; j++) { temp = new Element(); temp->prev = e1; temp->next = nullptr; - if(e1!=nullptr)e1->next = temp; + if(e1!=nullptr) + e1->next = temp; e1 = temp; } } - void Print() { - if (first != nullptr) { - Element* e = first; - while (e) { - std::cout << e->data << ' '; - e = e->next; - } - } - } void clear() { while (first) { Element* temp = first->next; @@ -176,11 +135,11 @@ void swap(Element* a, Element* b) { Element* temp; Element* temp1; int n=this->size(); - for (int i = 0; i < n; i++) { - for (int j = 0; j < n-1; j++) { + for (size_t i = 0; i < n; i++) { + for (size_t j = 0; j < n-1; j++) { if (e->data > e->next->data) { - swap(e->data, e->next->data); + std::swap(e->data, e->next->data); } e = e->next; } From 211d602fd65f04e74684c48195d387398339ab39 Mon Sep 17 00:00:00 2001 From: Grif-cpp <71193593+Grif-cpp@users.noreply.github.com> Date: Sat, 19 Dec 2020 12:09:19 +0300 Subject: [PATCH 5/5] Update list.cpp Ch19.12.20 --- module-1/homework/List/list.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/module-1/homework/List/list.cpp b/module-1/homework/List/list.cpp index c05998ec..d256f387 100644 --- a/module-1/homework/List/list.cpp +++ b/module-1/homework/List/list.cpp @@ -134,7 +134,7 @@ List() { Element* e = first; Element* temp; Element* temp1; - int n=this->size(); + int n=size(); for (size_t i = 0; i < n; i++) { for (size_t j = 0; j < n-1; j++) { if (e->data > e->next->data)