From 0b24fe98688a0df1b22c13dfac4919b5b541fe49 Mon Sep 17 00:00:00 2001 From: "github-classroom[bot]" <66690702+github-classroom[bot]@users.noreply.github.com> Date: Tue, 29 Mar 2022 07:05:31 +0000 Subject: [PATCH 01/13] Setting up GitHub Classroom Feedback From 0a60949f236e6c4ff92333658050bde1d5e8bc5a Mon Sep 17 00:00:00 2001 From: timyrkaa <99828985+timyrkaa@users.noreply.github.com> Date: Tue, 29 Mar 2022 10:19:30 +0300 Subject: [PATCH 02/13] Update array_stack.cpp --- src/array_stack.cpp | 83 --------------------------------------------- 1 file changed, 83 deletions(-) diff --git a/src/array_stack.cpp b/src/array_stack.cpp index 18141bc..8b13789 100644 --- a/src/array_stack.cpp +++ b/src/array_stack.cpp @@ -1,84 +1 @@ -#include "assignment/array_stack.hpp" -#include // copy, fill -#include // invalid_argument (НЕЛЬЗЯ ИСПОЛЬЗОВАТЬ) - -namespace assignment { - - ArrayStack::ArrayStack(int capacity) { - - // выбрасываем ошибку, если указана неположительная емкость стека - if (capacity <= 0) { - throw std::invalid_argument("capacity is not positive"); - } - - // Write your code here ... - } - - ArrayStack::~ArrayStack() { - // Write your code here ... - } - - void ArrayStack::Push(int value) { - // Write your code here ... - } - - bool ArrayStack::Pop() { - // Write your code here ... - return false; - } - - void ArrayStack::Clear() { - // Write your code here ... - } - - std::optional ArrayStack::Peek() const { - // Write your code here ... - return std::nullopt; - } - - bool ArrayStack::IsEmpty() const { - // Write your code here ... - return false; - } - - int ArrayStack::size() const { - // Write your code here ... - return 0; - } - - int ArrayStack::capacity() const { - // Write your code here ... - return 0; - } - - bool ArrayStack::Resize(int new_capacity) { - // Write your code here ... - return false; - } - - // ДЛЯ ТЕСТИРОВАНИЯ - ArrayStack::ArrayStack(const std::vector& values, int capacity) { - - size_ = static_cast(values.size()); - capacity_ = capacity; - - data_ = new int[capacity]{}; - - std::copy(values.data(), values.data() + size_, data_); - } - - std::vector ArrayStack::toVector(std::optional size) const { - - if (capacity_ == 0 || data_ == nullptr) { - return {}; - } - - if (size.has_value()) { - return {data_, data_ + size.value()}; - } - - return {data_, data_ + capacity_}; - } - -} // namespace assignment From fd29a23e400b865e77bd95632980a86b69dcb6eb Mon Sep 17 00:00:00 2001 From: timyrkaa <99828985+timyrkaa@users.noreply.github.com> Date: Tue, 29 Mar 2022 10:24:26 +0300 Subject: [PATCH 03/13] Update array_stack.cpp --- src/array_stack.cpp | 104 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/src/array_stack.cpp b/src/array_stack.cpp index 8b13789..2109960 100644 --- a/src/array_stack.cpp +++ b/src/array_stack.cpp @@ -1 +1,105 @@ +#include "assignment/array_stack.hpp" +#include // copy, fill +#include // invalid_argument (НЕЛЬЗЯ ИСПОЛЬЗОВАТЬ) + +namespace assignment { + + ArrayStack::ArrayStack(int capacity) { + + // выбрасываем ошибку, если указана неположительная емкость массива + if (capacity <= 0) { + throw std::invalid_argument("capacity is not positive"); + } + + capacity_ = capacity; + data_ = new int[capacity]{}; + } + + ArrayStack::~ArrayStack() { + size_ = 0; + capacity_ = 0; + delete data_; + data_ = nullptr; + } + + void ArrayStack::Push(int value) { + if (size_ >= capacity_) { + Resize(capacity_ + kCapacityGrowthCoefficient); + } + + data_[size_] = value; + size_ += 1; + } + + bool ArrayStack::Pop() { + if (size_ <= 0) { + return false; + } + + size_ -= 1; + return true; + } + + void ArrayStack::Clear() { + size_ = 0; + } + + std::optional ArrayStack::Peek() const { + if (size_ <= 0) { + return std::nullopt; + } + + return data_[size_-1]; + } + + bool ArrayStack::IsEmpty() const { + return size_ == 0; + } + + int ArrayStack::size() const { + return size_; + } + + int ArrayStack::capacity() const { + return capacity_; + } + + bool ArrayStack::Resize(int new_capacity) { + if (new_capacity <= capacity_) { + return false; + } + + int* new_data = new int[new_capacity]{}; + std::copy(data_, data_+size_, new_data); + delete data_; + data_ = new_data; + capacity_ = new_capacity; + return true; + } + + // ДЛЯ ТЕСТИРОВАНИЯ + ArrayStack::ArrayStack(const std::vector& values, int capacity) { + + size_ = static_cast(values.size()); + capacity_ = capacity; + + data_ = new int[capacity]{}; + + std::copy(values.data(), values.data() + size_, data_); + } + + std::vector ArrayStack::toVector(std::optional size) const { + + if (capacity_ == 0 || data_ == nullptr) { + return {}; + } + + if (size.has_value()) { + return {data_, data_ + size.value()}; + } + + return {data_, data_ + capacity_}; + } + +} // namespace assignment From 32c544b329455b789890cd0d9bfd5a90d4cf54ef Mon Sep 17 00:00:00 2001 From: timyrkaa <99828985+timyrkaa@users.noreply.github.com> Date: Tue, 29 Mar 2022 10:29:39 +0300 Subject: [PATCH 04/13] Update dynamic_array.cpp --- src/dynamic_array.cpp | 132 +++++++++++++++++++++++++++++++++++------- 1 file changed, 110 insertions(+), 22 deletions(-) diff --git a/src/dynamic_array.cpp b/src/dynamic_array.cpp index 55745bd..4d67176 100644 --- a/src/dynamic_array.cpp +++ b/src/dynamic_array.cpp @@ -4,73 +4,161 @@ #include // invalid_argument (НЕЛЬЗЯ ИСПОЛЬЗОВАТЬ) namespace assignment { - DynamicArray::DynamicArray(int capacity) { - - // выбрасываем ошибку, если указана неположительная емкость массива if (capacity <= 0) { throw std::invalid_argument("capacity is not positive"); } + size_ = 0; + capacity_ = capacity; + data_ = new int[capacity_]; + std::fill(data_,data_+capacity_,0); - // Write your code here ... } DynamicArray::~DynamicArray() { - // Write your code here ... + size_ = 0; + capacity_ = 0; + + delete[] data_; + data_ = nullptr; } void DynamicArray::Add(int value) { - // Write your code here ... + if (size_ == capacity_) { + int* new_data = new int[capacity_ + kCapacityGrowthCoefficient]; + for (int i = 0; i < size_; i++) { + new_data[i] = data_[i]; + } + new_data[size_] = value; + delete[] data_; + data_ = new_data; + size_++; + capacity_ = capacity_ + kCapacityGrowthCoefficient; + } else { + data_[size_] = value; + size_++; + } } bool DynamicArray::Insert(int index, int value) { - // Write your code here ... - return false; + if ((index < 0)||(index > size_)) { + return false; + } + if ((size_ == 0)||(index == size_)||((size_ == 1)&&(index == 1))) { + Add(value); + return true; + } + if ((size_ == 1)&&(index == 0)) { + Add(data_[0]); + data_[0] = value; + return true; + } + if (size_ == capacity_) { + int* new_array = new int[capacity_+kCapacityGrowthCoefficient]; + for (int i = 0; i <= size_; i++) { + if (i < index) { + new_array[i] = data_[i]; + } + if (i == index) { + new_array[i] = value; + } + if (i > index) { + new_array[i] = data_[i-1]; + } + } + size_++; + capacity_ = capacity_+kCapacityGrowthCoefficient; + data_ = new_array; + return true; + } + for (int i = size_; i > index; i--) { + data_[i] = data_[i-1]; + } + data_[index] = value; + size_++; + return true; } bool DynamicArray::Set(int index, int new_value) { - // Write your code here ... - return false; + if ((size_ == 0)||(index >= size_)||(index < 0)) { + return false; + } + data_[index] = new_value; + return true; + } std::optional DynamicArray::Remove(int index) { - // Write your code here ... - return std::nullopt; + if (IsEmpty()||(index < 0)||(index >= size_)) { + return std::nullopt; + } + int deleted_element = data_[index]; + if ((size_ == 1)||(index == size_-1)) { + data_[index] = 0; + size_--; + return deleted_element; + } + for (int i = index; i < size_-1; i++) { + data_[i] = data_[i+1]; + } + size_--; + return deleted_element; } void DynamicArray::Clear() { - // Write your code here ... + size_ = 0; } std::optional DynamicArray::Get(int index) const { - // Write your code here ... - return std::nullopt; + if (size_ == 0) { + return std::nullopt; + } else if ((index < size_)&&(index >= 0)) { + return data_[index]; + } else { + return std::nullopt; + } } std::optional DynamicArray::IndexOf(int value) const { - // Write your code here ... + for (int i = 0; i < size_; i++) { + if (data_[i] == value) { + return i; + } + } return std::nullopt; } bool DynamicArray::Contains(int value) const { + for (int i = 0; i < size_; i++) { + if (data_[i] == value) { + return true; + } + } return false; } bool DynamicArray::IsEmpty() const { - return false; + return size_ == 0; } int DynamicArray::size() const { - return 0; + return size_; } int DynamicArray::capacity() const { - return 0; + return capacity_; } bool DynamicArray::Resize(int new_capacity) { - // Write your code here ... - return false; + if (new_capacity <= capacity_) { + return false; + } + int* new_arr = new int[new_capacity]; + std::copy(data_,data_+size_,new_arr); + delete[] data_; + data_ = new_arr; + capacity_ = new_capacity; + return true; } // ДЛЯ ТЕСТИРОВАНИЯ @@ -97,4 +185,4 @@ namespace assignment { return {data_, data_ + capacity_}; } -} // namespace assignment \ No newline at end of file +} // namespace assignment From 8e898dd97c5b2daeed9da82a9fb8b9eb7d114c24 Mon Sep 17 00:00:00 2001 From: timyrkaa <99828985+timyrkaa@users.noreply.github.com> Date: Tue, 29 Mar 2022 10:31:02 +0300 Subject: [PATCH 05/13] Update linked_list.cpp --- src/linked_list.cpp | 146 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 125 insertions(+), 21 deletions(-) diff --git a/src/linked_list.cpp b/src/linked_list.cpp index 4497560..62a45dc 100644 --- a/src/linked_list.cpp +++ b/src/linked_list.cpp @@ -9,62 +9,166 @@ namespace assignment { } void LinkedList::Add(int value) { - // Write your code here ... + if (IsEmpty()) { + back_ = new Node(value, nullptr); + front_ = back_; + size_++; + } else { + Node* new_node = new Node(value, nullptr); + back_->next = new_node; + back_ = new_node; + size_++; + } } bool LinkedList::Insert(int index, int value) { - // Write your code here ... - return false; + if ((index < 0)||(index > size_)||(IsEmpty()&&(index > 0))) { + return false; + } + if ((size_ == 1)&&(index == 0)) { + Node* new_node = new Node(value,back_); + front_ = new_node; + size_++; + return true; + } + if (IsEmpty()||(index == size_)||((size_ == 1)&&(index == 1))) { + Add(value); + return true; + } + if (index == 0) { + Node* new_node = new Node(value, front_); + front_ = new_node; + size_++; + return true; + } + Node* new_node = new Node(value, FindNode(index)); + Node* first_before_inserted = FindNode(index-1); + first_before_inserted->next = new_node; + size_++; + return true; } bool LinkedList::Set(int index, int new_value) { - return false; + if (IsEmpty()||(index < 0)||(index >= size_)) { + return false; + } + Node* changed_node = front_; + for (int i = 0; i < index; i++) { + Node* next_node = changed_node->next; + changed_node = next_node; + } + changed_node->value = new_value; + return true; } std::optional LinkedList::Remove(int index) { - // Write your code here ... - return std::nullopt; + if (IsEmpty() || (index < 0) || (index >= size_)) { + return std::nullopt; + } + int deleted_value; + if (index == 0) { + deleted_value = front_->value; + front_ = front_->next; + size_--; + return deleted_value; + } + Node* search_node = front_; + for (int i = 0; i < index; i++) { + if ((index == size_-1)&&(i == index-1)) { + deleted_value = back_->value; + search_node->next = nullptr; + back_ = search_node; + size_--; + return deleted_value; + } + if (i == index-1) { + deleted_value = search_node->next->value; + search_node->next = search_node->next->next; + size_--; + return deleted_value; + } + search_node = search_node->next; + } } + void LinkedList::Clear() { - // Write your code here ... + size_ = 0; + front_ = nullptr; + back_ = nullptr; } std::optional LinkedList::Get(int index) const { - // Write your code here ... - return std::nullopt; + if (IsEmpty()||(index < 0)||(index >= size_)) { + return std::nullopt; + } + Node* search_node = front_; + for (int i = 0; i < index; i++) { + Node* next_node = search_node->next; + search_node = next_node; + } + return search_node->value; } std::optional LinkedList::IndexOf(int value) const { - // Write your code here ... - return std::nullopt; + Node* search_node = front_; + for (int i = 0; i < size_-1; i++) { + Node* next_node = search_node->next; + if (search_node->value == value) { + return i; + } + search_node = next_node; + } + if (search_node->value == value) { + return size_-1; + } + return false; } bool LinkedList::Contains(int value) const { - return false; + Node* search_node = front_; + for (int i = 0; i < size_-1; i++) { + Node* next_node = search_node->next; + if (search_node->value == value) { + return true; + } + search_node = next_node; + } + return search_node->value == value; } bool LinkedList::IsEmpty() const { - return false; + return size_ == 0; } int LinkedList::size() const { - return 0; + return size_; } std::optional LinkedList::front() const { - // Write your code here ... - return std::nullopt; + if (IsEmpty()) { + return std::nullopt; + } + return front_->value; } std::optional LinkedList::back() const { - // Write your code here ... - return std::nullopt; + if (IsEmpty()) { + return std::nullopt; + } + return back_->value; } Node* LinkedList::FindNode(int index) const { - // Write your code here ... - return nullptr; + if (IsEmpty()||(index < 0)||(index >= size_)) { + return nullptr; + } + Node* search_node = front_; + for (int i = 0; i < index; i++) { + Node* next_node = search_node->next; + search_node = next_node; + } + return search_node; } // ДЛЯ ТЕСТИРОВАНИЯ @@ -108,4 +212,4 @@ namespace assignment { return array; } -} // namespace assignment \ No newline at end of file +} // namespace assignment From 702f3105d47fad070bf3d820e78728b2c494c208 Mon Sep 17 00:00:00 2001 From: timyrkaa <99828985+timyrkaa@users.noreply.github.com> Date: Tue, 29 Mar 2022 10:33:44 +0300 Subject: [PATCH 06/13] Update linked_queue.cpp --- src/linked_queue.cpp | 50 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/src/linked_queue.cpp b/src/linked_queue.cpp index c99a57d..6524478 100644 --- a/src/linked_queue.cpp +++ b/src/linked_queue.cpp @@ -9,34 +9,62 @@ namespace assignment { } void LinkedQueue::Enqueue(int value) { - // Write your code here ... + size_ += 1; + if (front_ == nullptr) { + front_ = new Node(value); + back_ = front_; + } else { + back_->next = new Node(value); + back_ = back_->next; + } } bool LinkedQueue::Dequeue() { - // Write your code here ... - return false; + if (size_ <= 0) { + return false; + } + + Node deleted = *front_; + delete front_; + front_ = deleted.next; + size_ -= 1; + return true; } void LinkedQueue::Clear() { - // Write your code here ... + Node* node = front_; + while (node != nullptr) { + Node* next = node->next; + delete node; + node = next; + } + size_ = 0; + front_ = nullptr; + back_ = nullptr; } std::optional LinkedQueue::front() const { - // Write your code here ... - return std::nullopt; + if (front_ == nullptr) { + return std::nullopt; + } + + return front_->value; } std::optional LinkedQueue::back() const { - // Write your code here ... - return std::nullopt; + if (back_ == nullptr) { + return std::nullopt; + } + + return back_->value; } bool LinkedQueue::IsEmpty() const { - return false; + return size_ == 0; } int LinkedQueue::size() const { - return 0; + return size_; } // ДЛЯ ТЕСТИРОВАНИЯ @@ -79,4 +107,4 @@ namespace assignment { return array; } -} // namespace assignment \ No newline at end of file +} // namespace assignment From 88bf47baf026bd1f03c8b562736755e5b0ebb3d6 Mon Sep 17 00:00:00 2001 From: timyrkaa <99828985+timyrkaa@users.noreply.github.com> Date: Tue, 29 Mar 2022 10:37:08 +0300 Subject: [PATCH 07/13] Update dynamic_array.cpp --- src/dynamic_array.cpp | 109 ++++++++++++++---------------------------- 1 file changed, 35 insertions(+), 74 deletions(-) diff --git a/src/dynamic_array.cpp b/src/dynamic_array.cpp index 4d67176..afdcb28 100644 --- a/src/dynamic_array.cpp +++ b/src/dynamic_array.cpp @@ -4,105 +4,67 @@ #include // invalid_argument (НЕЛЬЗЯ ИСПОЛЬЗОВАТЬ) namespace assignment { + DynamicArray::DynamicArray(int capacity) { + + // выбрасываем ошибку, если указана неположительная емкость массива if (capacity <= 0) { throw std::invalid_argument("capacity is not positive"); } - size_ = 0; - capacity_ = capacity; - data_ = new int[capacity_]; - std::fill(data_,data_+capacity_,0); + capacity_ = capacity; + data_ = new int[capacity]{}; } DynamicArray::~DynamicArray() { size_ = 0; capacity_ = 0; - - delete[] data_; + delete data_; data_ = nullptr; } void DynamicArray::Add(int value) { - if (size_ == capacity_) { - int* new_data = new int[capacity_ + kCapacityGrowthCoefficient]; - for (int i = 0; i < size_; i++) { - new_data[i] = data_[i]; - } - new_data[size_] = value; - delete[] data_; - data_ = new_data; - size_++; - capacity_ = capacity_ + kCapacityGrowthCoefficient; - } else { - data_[size_] = value; - size_++; + if (size_ >= capacity_) { + Resize(capacity_ + kCapacityGrowthCoefficient); } + + data_[size_] = value; + size_ += 1; } bool DynamicArray::Insert(int index, int value) { - if ((index < 0)||(index > size_)) { + if (index < 0 || index > size_) { return false; } - if ((size_ == 0)||(index == size_)||((size_ == 1)&&(index == 1))) { - Add(value); - return true; - } - if ((size_ == 1)&&(index == 0)) { - Add(data_[0]); - data_[0] = value; - return true; - } - if (size_ == capacity_) { - int* new_array = new int[capacity_+kCapacityGrowthCoefficient]; - for (int i = 0; i <= size_; i++) { - if (i < index) { - new_array[i] = data_[i]; - } - if (i == index) { - new_array[i] = value; - } - if (i > index) { - new_array[i] = data_[i-1]; - } - } - size_++; - capacity_ = capacity_+kCapacityGrowthCoefficient; - data_ = new_array; - return true; - } - for (int i = size_; i > index; i--) { - data_[i] = data_[i-1]; + + if (size_ >= capacity_) { + Resize(capacity_ + kCapacityGrowthCoefficient); } + + std::copy(data_ + index, data_ + size_, data_ + index + 1); + size_ += 1; data_[index] = value; - size_++; return true; } bool DynamicArray::Set(int index, int new_value) { - if ((size_ == 0)||(index >= size_)||(index < 0)) { + if (index < 0 || index >= size_) { return false; } + data_[index] = new_value; return true; - } std::optional DynamicArray::Remove(int index) { - if (IsEmpty()||(index < 0)||(index >= size_)) { + if (index < 0 || index >= size_) { return std::nullopt; } - int deleted_element = data_[index]; - if ((size_ == 1)||(index == size_-1)) { - data_[index] = 0; - size_--; - return deleted_element; - } - for (int i = index; i < size_-1; i++) { - data_[i] = data_[i+1]; - } - size_--; - return deleted_element; + + int value = data_[index]; + std::copy(data_ + index + 1, data_ + size_, data_ + index); + size_ -= 1; + return value; } void DynamicArray::Clear() { @@ -110,13 +72,11 @@ namespace assignment { } std::optional DynamicArray::Get(int index) const { - if (size_ == 0) { - return std::nullopt; - } else if ((index < size_)&&(index >= 0)) { - return data_[index]; - } else { + if (index < 0 || index >= size_) { return std::nullopt; } + + return data_[index]; } std::optional DynamicArray::IndexOf(int value) const { @@ -138,7 +98,7 @@ namespace assignment { } bool DynamicArray::IsEmpty() const { - return size_ == 0; + return size_ <= 0; } int DynamicArray::size() const { @@ -153,10 +113,11 @@ namespace assignment { if (new_capacity <= capacity_) { return false; } - int* new_arr = new int[new_capacity]; - std::copy(data_,data_+size_,new_arr); - delete[] data_; - data_ = new_arr; + + int* new_data = new int[new_capacity]{}; + std::copy(data_, data_+size_, new_data); + delete data_; + data_ = new_data; capacity_ = new_capacity; return true; } From 97a953cd50cc75ed4ac9fe850558395c98c34c24 Mon Sep 17 00:00:00 2001 From: timyrkaa <99828985+timyrkaa@users.noreply.github.com> Date: Tue, 29 Mar 2022 10:42:46 +0300 Subject: [PATCH 08/13] Update array_stack.cpp --- src/array_stack.cpp | 104 -------------------------------------------- 1 file changed, 104 deletions(-) diff --git a/src/array_stack.cpp b/src/array_stack.cpp index 2109960..8b13789 100644 --- a/src/array_stack.cpp +++ b/src/array_stack.cpp @@ -1,105 +1 @@ -#include "assignment/array_stack.hpp" -#include // copy, fill -#include // invalid_argument (НЕЛЬЗЯ ИСПОЛЬЗОВАТЬ) - -namespace assignment { - - ArrayStack::ArrayStack(int capacity) { - - // выбрасываем ошибку, если указана неположительная емкость массива - if (capacity <= 0) { - throw std::invalid_argument("capacity is not positive"); - } - - capacity_ = capacity; - data_ = new int[capacity]{}; - } - - ArrayStack::~ArrayStack() { - size_ = 0; - capacity_ = 0; - delete data_; - data_ = nullptr; - } - - void ArrayStack::Push(int value) { - if (size_ >= capacity_) { - Resize(capacity_ + kCapacityGrowthCoefficient); - } - - data_[size_] = value; - size_ += 1; - } - - bool ArrayStack::Pop() { - if (size_ <= 0) { - return false; - } - - size_ -= 1; - return true; - } - - void ArrayStack::Clear() { - size_ = 0; - } - - std::optional ArrayStack::Peek() const { - if (size_ <= 0) { - return std::nullopt; - } - - return data_[size_-1]; - } - - bool ArrayStack::IsEmpty() const { - return size_ == 0; - } - - int ArrayStack::size() const { - return size_; - } - - int ArrayStack::capacity() const { - return capacity_; - } - - bool ArrayStack::Resize(int new_capacity) { - if (new_capacity <= capacity_) { - return false; - } - - int* new_data = new int[new_capacity]{}; - std::copy(data_, data_+size_, new_data); - delete data_; - data_ = new_data; - capacity_ = new_capacity; - return true; - } - - // ДЛЯ ТЕСТИРОВАНИЯ - ArrayStack::ArrayStack(const std::vector& values, int capacity) { - - size_ = static_cast(values.size()); - capacity_ = capacity; - - data_ = new int[capacity]{}; - - std::copy(values.data(), values.data() + size_, data_); - } - - std::vector ArrayStack::toVector(std::optional size) const { - - if (capacity_ == 0 || data_ == nullptr) { - return {}; - } - - if (size.has_value()) { - return {data_, data_ + size.value()}; - } - - return {data_, data_ + capacity_}; - } - -} // namespace assignment From d1c673a8410ab45e3d31e1017a0d939baf3d52ad Mon Sep 17 00:00:00 2001 From: timyrkaa <99828985+timyrkaa@users.noreply.github.com> Date: Tue, 29 Mar 2022 10:43:44 +0300 Subject: [PATCH 09/13] Update dynamic_array.cpp --- src/dynamic_array.cpp | 148 ------------------------------------------ 1 file changed, 148 deletions(-) diff --git a/src/dynamic_array.cpp b/src/dynamic_array.cpp index afdcb28..8b13789 100644 --- a/src/dynamic_array.cpp +++ b/src/dynamic_array.cpp @@ -1,149 +1 @@ -#include "assignment/dynamic_array.hpp" -#include // copy, fill -#include // invalid_argument (НЕЛЬЗЯ ИСПОЛЬЗОВАТЬ) - -namespace assignment { - - DynamicArray::DynamicArray(int capacity) { - - // выбрасываем ошибку, если указана неположительная емкость массива - if (capacity <= 0) { - throw std::invalid_argument("capacity is not positive"); - } - - capacity_ = capacity; - data_ = new int[capacity]{}; - } - - DynamicArray::~DynamicArray() { - size_ = 0; - capacity_ = 0; - delete data_; - data_ = nullptr; - } - - void DynamicArray::Add(int value) { - if (size_ >= capacity_) { - Resize(capacity_ + kCapacityGrowthCoefficient); - } - - data_[size_] = value; - size_ += 1; - } - - bool DynamicArray::Insert(int index, int value) { - if (index < 0 || index > size_) { - return false; - } - - if (size_ >= capacity_) { - Resize(capacity_ + kCapacityGrowthCoefficient); - } - - std::copy(data_ + index, data_ + size_, data_ + index + 1); - size_ += 1; - data_[index] = value; - return true; - } - - bool DynamicArray::Set(int index, int new_value) { - if (index < 0 || index >= size_) { - return false; - } - - data_[index] = new_value; - return true; - } - - std::optional DynamicArray::Remove(int index) { - if (index < 0 || index >= size_) { - return std::nullopt; - } - - int value = data_[index]; - std::copy(data_ + index + 1, data_ + size_, data_ + index); - size_ -= 1; - return value; - } - - void DynamicArray::Clear() { - size_ = 0; - } - - std::optional DynamicArray::Get(int index) const { - if (index < 0 || index >= size_) { - return std::nullopt; - } - - return data_[index]; - } - - std::optional DynamicArray::IndexOf(int value) const { - for (int i = 0; i < size_; i++) { - if (data_[i] == value) { - return i; - } - } - return std::nullopt; - } - - bool DynamicArray::Contains(int value) const { - for (int i = 0; i < size_; i++) { - if (data_[i] == value) { - return true; - } - } - return false; - } - - bool DynamicArray::IsEmpty() const { - return size_ <= 0; - } - - int DynamicArray::size() const { - return size_; - } - - int DynamicArray::capacity() const { - return capacity_; - } - - bool DynamicArray::Resize(int new_capacity) { - if (new_capacity <= capacity_) { - return false; - } - - int* new_data = new int[new_capacity]{}; - std::copy(data_, data_+size_, new_data); - delete data_; - data_ = new_data; - capacity_ = new_capacity; - return true; - } - - // ДЛЯ ТЕСТИРОВАНИЯ - DynamicArray::DynamicArray(const std::vector& values, int capacity) { - - size_ = static_cast(values.size()); - capacity_ = capacity; - - data_ = new int[capacity]{}; - - std::copy(values.data(), values.data() + size_, data_); - } - - std::vector DynamicArray::toVector(std::optional size) const { - - if (capacity_ == 0 || data_ == nullptr) { - return {}; - } - - if (size.has_value()) { - return {data_, data_ + size.value()}; - } - - return {data_, data_ + capacity_}; - } - -} // namespace assignment From 77cb9ff8009b54098fc280d53cd2b02b31d7fca5 Mon Sep 17 00:00:00 2001 From: timyrkaa <99828985+timyrkaa@users.noreply.github.com> Date: Tue, 29 Mar 2022 10:44:02 +0300 Subject: [PATCH 10/13] Update linked_list.cpp --- src/linked_list.cpp | 214 -------------------------------------------- 1 file changed, 214 deletions(-) diff --git a/src/linked_list.cpp b/src/linked_list.cpp index 62a45dc..8b13789 100644 --- a/src/linked_list.cpp +++ b/src/linked_list.cpp @@ -1,215 +1 @@ -#include "assignment/linked_list.hpp" -namespace assignment { - - LinkedList::~LinkedList() { - - // эквивалентно очистке списка - LinkedList::Clear(); - } - - void LinkedList::Add(int value) { - if (IsEmpty()) { - back_ = new Node(value, nullptr); - front_ = back_; - size_++; - } else { - Node* new_node = new Node(value, nullptr); - back_->next = new_node; - back_ = new_node; - size_++; - } - } - - bool LinkedList::Insert(int index, int value) { - if ((index < 0)||(index > size_)||(IsEmpty()&&(index > 0))) { - return false; - } - if ((size_ == 1)&&(index == 0)) { - Node* new_node = new Node(value,back_); - front_ = new_node; - size_++; - return true; - } - if (IsEmpty()||(index == size_)||((size_ == 1)&&(index == 1))) { - Add(value); - return true; - } - if (index == 0) { - Node* new_node = new Node(value, front_); - front_ = new_node; - size_++; - return true; - } - Node* new_node = new Node(value, FindNode(index)); - Node* first_before_inserted = FindNode(index-1); - first_before_inserted->next = new_node; - size_++; - return true; - } - - bool LinkedList::Set(int index, int new_value) { - if (IsEmpty()||(index < 0)||(index >= size_)) { - return false; - } - Node* changed_node = front_; - for (int i = 0; i < index; i++) { - Node* next_node = changed_node->next; - changed_node = next_node; - } - changed_node->value = new_value; - return true; - } - - std::optional LinkedList::Remove(int index) { - if (IsEmpty() || (index < 0) || (index >= size_)) { - return std::nullopt; - } - int deleted_value; - if (index == 0) { - deleted_value = front_->value; - front_ = front_->next; - size_--; - return deleted_value; - } - Node* search_node = front_; - for (int i = 0; i < index; i++) { - if ((index == size_-1)&&(i == index-1)) { - deleted_value = back_->value; - search_node->next = nullptr; - back_ = search_node; - size_--; - return deleted_value; - } - if (i == index-1) { - deleted_value = search_node->next->value; - search_node->next = search_node->next->next; - size_--; - return deleted_value; - } - search_node = search_node->next; - } - } - - - void LinkedList::Clear() { - size_ = 0; - front_ = nullptr; - back_ = nullptr; - } - - std::optional LinkedList::Get(int index) const { - if (IsEmpty()||(index < 0)||(index >= size_)) { - return std::nullopt; - } - Node* search_node = front_; - for (int i = 0; i < index; i++) { - Node* next_node = search_node->next; - search_node = next_node; - } - return search_node->value; - } - - std::optional LinkedList::IndexOf(int value) const { - Node* search_node = front_; - for (int i = 0; i < size_-1; i++) { - Node* next_node = search_node->next; - if (search_node->value == value) { - return i; - } - search_node = next_node; - } - if (search_node->value == value) { - return size_-1; - } - return false; - } - - bool LinkedList::Contains(int value) const { - Node* search_node = front_; - for (int i = 0; i < size_-1; i++) { - Node* next_node = search_node->next; - if (search_node->value == value) { - return true; - } - search_node = next_node; - } - return search_node->value == value; - } - - bool LinkedList::IsEmpty() const { - return size_ == 0; - } - - int LinkedList::size() const { - return size_; - } - - std::optional LinkedList::front() const { - if (IsEmpty()) { - return std::nullopt; - } - return front_->value; - } - - std::optional LinkedList::back() const { - if (IsEmpty()) { - return std::nullopt; - } - return back_->value; - } - - Node* LinkedList::FindNode(int index) const { - if (IsEmpty()||(index < 0)||(index >= size_)) { - return nullptr; - } - Node* search_node = front_; - for (int i = 0; i < index; i++) { - Node* next_node = search_node->next; - search_node = next_node; - } - return search_node; - } - - // ДЛЯ ТЕСТИРОВАНИЯ - LinkedList::LinkedList(const std::vector& values) { - - if (values.empty()) { - return; - } - - auto* curr_node = new Node(values.front()); - front_ = curr_node; - - for (int index = 1; index < values.size() - 1; ++index) { - curr_node->next = new Node(values[index]); - curr_node = curr_node->next; - } - - if (values.size() == 1) { - back_ = front_; - } else { - curr_node->next = new Node(values.back()); - back_ = curr_node->next; - } - - size_ = static_cast(values.size()); - } - - std::vector LinkedList::toVector() const { - - if (front_ == nullptr || size_ == 0) { - return {}; - } - - std::vector array; - array.reserve(size_); - - for (auto* node = front_; node != nullptr; node = node->next) { - array.push_back(node->value); - } - - return array; - } - -} // namespace assignment From cea256f2a897ec70045cd5d838e123e29fbbe452 Mon Sep 17 00:00:00 2001 From: timyrkaa <99828985+timyrkaa@users.noreply.github.com> Date: Tue, 29 Mar 2022 10:44:22 +0300 Subject: [PATCH 11/13] Update linked_queue.cpp --- src/linked_queue.cpp | 109 ------------------------------------------- 1 file changed, 109 deletions(-) diff --git a/src/linked_queue.cpp b/src/linked_queue.cpp index 6524478..8b13789 100644 --- a/src/linked_queue.cpp +++ b/src/linked_queue.cpp @@ -1,110 +1 @@ -#include "assignment/linked_queue.hpp" -namespace assignment { - - LinkedQueue::~LinkedQueue() { - - // эквивалентно очистке очереди - LinkedQueue::Clear(); - } - - void LinkedQueue::Enqueue(int value) { - size_ += 1; - if (front_ == nullptr) { - front_ = new Node(value); - back_ = front_; - } else { - back_->next = new Node(value); - back_ = back_->next; - } - } - - bool LinkedQueue::Dequeue() { - if (size_ <= 0) { - return false; - } - - Node deleted = *front_; - delete front_; - front_ = deleted.next; - size_ -= 1; - return true; - } - - void LinkedQueue::Clear() { - Node* node = front_; - while (node != nullptr) { - Node* next = node->next; - delete node; - node = next; - } - size_ = 0; - front_ = nullptr; - back_ = nullptr; - } - - std::optional LinkedQueue::front() const { - if (front_ == nullptr) { - return std::nullopt; - } - - return front_->value; - } - - std::optional LinkedQueue::back() const { - if (back_ == nullptr) { - return std::nullopt; - } - - return back_->value; - } - - bool LinkedQueue::IsEmpty() const { - return size_ == 0; - } - - int LinkedQueue::size() const { - return size_; - } - - // ДЛЯ ТЕСТИРОВАНИЯ - LinkedQueue::LinkedQueue(const std::vector& values) { - - if (values.empty()) { - return; - } - - auto* curr_node = new Node(values.front()); - front_ = curr_node; - - for (int index = 1; index < values.size() - 1; ++index) { - curr_node->next = new Node(values[index]); - curr_node = curr_node->next; - } - - if (values.size() == 1) { - back_ = front_; - } else { - curr_node->next = new Node(values.back()); - back_ = curr_node->next; - } - - size_ = static_cast(values.size()); - } - - std::vector LinkedQueue::toVector() const { - if (front_ == nullptr || size_ == 0) { - return {}; - } - - std::vector array; - array.reserve(size_); - - for (auto* node = front_; node != nullptr; node = node->next) { - array.push_back(node->value); - } - - return array; - } - -} // namespace assignment From f861564546b609da7a2724c1ecf0d5dce44725cf Mon Sep 17 00:00:00 2001 From: _timaaaaa Date: Tue, 29 Mar 2022 10:49:07 +0300 Subject: [PATCH 12/13] =?UTF-8?q?=D0=92=D1=8B=D0=BF=D0=BE=D0=BB=D0=BD?= =?UTF-8?q?=D0=B8=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +- src/array_stack.cpp | 57 +++++++++++------ src/dynamic_array.cpp | 130 ++++++++++++++++++++++++++++++++------ src/linked_list.cpp | 144 ++++++++++++++++++++++++++++++++++++------ src/linked_queue.cpp | 48 +++++++++++--- 5 files changed, 312 insertions(+), 71 deletions(-) diff --git a/README.md b/README.md index 2c2de0b..f8c0569 100644 --- a/README.md +++ b/README.md @@ -5,9 +5,9 @@ ## 1. Информация о студенте -**Номер группы**: 00-000 +**Номер группы**: 11-109 -**Фамилия и Имя**: Иванов Иван +**Фамилия и Имя**: Султанов Тимур ## 2. Описание задания diff --git a/src/array_stack.cpp b/src/array_stack.cpp index 18141bc..c55c36b 100644 --- a/src/array_stack.cpp +++ b/src/array_stack.cpp @@ -7,54 +7,75 @@ namespace assignment { ArrayStack::ArrayStack(int capacity) { - // выбрасываем ошибку, если указана неположительная емкость стека + // выбрасываем ошибку, если указана неположительная емкость массива if (capacity <= 0) { throw std::invalid_argument("capacity is not positive"); } - // Write your code here ... + capacity_ = capacity; + data_ = new int[capacity]{}; } ArrayStack::~ArrayStack() { - // Write your code here ... + size_ = 0; + capacity_ = 0; + delete data_; + data_ = nullptr; } void ArrayStack::Push(int value) { - // Write your code here ... + if (size_ >= capacity_) { + Resize(capacity_ + kCapacityGrowthCoefficient); + } + + data_[size_] = value; + size_ += 1; } bool ArrayStack::Pop() { - // Write your code here ... - return false; + if (size_ <= 0) { + return false; + } + + size_ -= 1; + return true; } void ArrayStack::Clear() { - // Write your code here ... + size_ = 0; } std::optional ArrayStack::Peek() const { - // Write your code here ... - return std::nullopt; + if (size_ <= 0) { + return std::nullopt; + } + + return data_[size_-1]; } bool ArrayStack::IsEmpty() const { - // Write your code here ... - return false; + return size_ == 0; } int ArrayStack::size() const { - // Write your code here ... - return 0; + return size_; } int ArrayStack::capacity() const { - // Write your code here ... - return 0; + return capacity_; } bool ArrayStack::Resize(int new_capacity) { - // Write your code here ... - return false; + if (new_capacity <= capacity_) { + return false; + } + + int* new_data = new int[new_capacity]{}; + std::copy(data_, data_+size_, new_data); + delete data_; + data_ = new_data; + capacity_ = new_capacity; + return true; } // ДЛЯ ТЕСТИРОВАНИЯ @@ -81,4 +102,4 @@ namespace assignment { return {data_, data_ + capacity_}; } -} // namespace assignment +} // namespace assignment \ No newline at end of file diff --git a/src/dynamic_array.cpp b/src/dynamic_array.cpp index 55745bd..429efdd 100644 --- a/src/dynamic_array.cpp +++ b/src/dynamic_array.cpp @@ -4,73 +4,161 @@ #include // invalid_argument (НЕЛЬЗЯ ИСПОЛЬЗОВАТЬ) namespace assignment { - DynamicArray::DynamicArray(int capacity) { - - // выбрасываем ошибку, если указана неположительная емкость массива if (capacity <= 0) { throw std::invalid_argument("capacity is not positive"); } + size_ = 0; + capacity_ = capacity; + data_ = new int[capacity_]; + std::fill(data_,data_+capacity_,0); - // Write your code here ... } DynamicArray::~DynamicArray() { - // Write your code here ... + size_ = 0; + capacity_ = 0; + + delete[] data_; + data_ = nullptr; } void DynamicArray::Add(int value) { - // Write your code here ... + if (size_ == capacity_) { + int* new_data = new int[capacity_ + kCapacityGrowthCoefficient]; + for (int i = 0; i < size_; i++) { + new_data[i] = data_[i]; + } + new_data[size_] = value; + delete[] data_; + data_ = new_data; + size_++; + capacity_ = capacity_ + kCapacityGrowthCoefficient; + } else { + data_[size_] = value; + size_++; + } } bool DynamicArray::Insert(int index, int value) { - // Write your code here ... - return false; + if ((index < 0)||(index > size_)) { + return false; + } + if ((size_ == 0)||(index == size_)||((size_ == 1)&&(index == 1))) { + Add(value); + return true; + } + if ((size_ == 1)&&(index == 0)) { + Add(data_[0]); + data_[0] = value; + return true; + } + if (size_ == capacity_) { + int* new_array = new int[capacity_+kCapacityGrowthCoefficient]; + for (int i = 0; i <= size_; i++) { + if (i < index) { + new_array[i] = data_[i]; + } + if (i == index) { + new_array[i] = value; + } + if (i > index) { + new_array[i] = data_[i-1]; + } + } + size_++; + capacity_ = capacity_+kCapacityGrowthCoefficient; + data_ = new_array; + return true; + } + for (int i = size_; i > index; i--) { + data_[i] = data_[i-1]; + } + data_[index] = value; + size_++; + return true; } bool DynamicArray::Set(int index, int new_value) { - // Write your code here ... - return false; + if ((size_ == 0)||(index >= size_)||(index < 0)) { + return false; + } + data_[index] = new_value; + return true; + } std::optional DynamicArray::Remove(int index) { - // Write your code here ... - return std::nullopt; + if (IsEmpty()||(index < 0)||(index >= size_)) { + return std::nullopt; + } + int deleted_element = data_[index]; + if ((size_ == 1)||(index == size_-1)) { + data_[index] = 0; + size_--; + return deleted_element; + } + for (int i = index; i < size_-1; i++) { + data_[i] = data_[i+1]; + } + size_--; + return deleted_element; } void DynamicArray::Clear() { - // Write your code here ... + size_ = 0; } std::optional DynamicArray::Get(int index) const { - // Write your code here ... - return std::nullopt; + if (size_ == 0) { + return std::nullopt; + } else if ((index < size_)&&(index >= 0)) { + return data_[index]; + } else { + return std::nullopt; + } } std::optional DynamicArray::IndexOf(int value) const { - // Write your code here ... + for (int i = 0; i < size_; i++) { + if (data_[i] == value) { + return i; + } + } return std::nullopt; } bool DynamicArray::Contains(int value) const { + for (int i = 0; i < size_; i++) { + if (data_[i] == value) { + return true; + } + } return false; } bool DynamicArray::IsEmpty() const { - return false; + return size_ == 0; } int DynamicArray::size() const { - return 0; + return size_; } int DynamicArray::capacity() const { - return 0; + return capacity_; } bool DynamicArray::Resize(int new_capacity) { - // Write your code here ... - return false; + if (new_capacity <= capacity_) { + return false; + } + int* new_arr = new int[new_capacity]; + std::copy(data_,data_+size_,new_arr); + delete[] data_; + data_ = new_arr; + capacity_ = new_capacity; + return true; } // ДЛЯ ТЕСТИРОВАНИЯ diff --git a/src/linked_list.cpp b/src/linked_list.cpp index 4497560..e2f281e 100644 --- a/src/linked_list.cpp +++ b/src/linked_list.cpp @@ -9,62 +9,166 @@ namespace assignment { } void LinkedList::Add(int value) { - // Write your code here ... + if (IsEmpty()) { + back_ = new Node(value, nullptr); + front_ = back_; + size_++; + } else { + Node* new_node = new Node(value, nullptr); + back_->next = new_node; + back_ = new_node; + size_++; + } } bool LinkedList::Insert(int index, int value) { - // Write your code here ... - return false; + if ((index < 0)||(index > size_)||(IsEmpty()&&(index > 0))) { + return false; + } + if ((size_ == 1)&&(index == 0)) { + Node* new_node = new Node(value,back_); + front_ = new_node; + size_++; + return true; + } + if (IsEmpty()||(index == size_)||((size_ == 1)&&(index == 1))) { + Add(value); + return true; + } + if (index == 0) { + Node* new_node = new Node(value, front_); + front_ = new_node; + size_++; + return true; + } + Node* new_node = new Node(value, FindNode(index)); + Node* first_before_inserted = FindNode(index-1); + first_before_inserted->next = new_node; + size_++; + return true; } bool LinkedList::Set(int index, int new_value) { - return false; + if (IsEmpty()||(index < 0)||(index >= size_)) { + return false; + } + Node* changed_node = front_; + for (int i = 0; i < index; i++) { + Node* next_node = changed_node->next; + changed_node = next_node; + } + changed_node->value = new_value; + return true; } std::optional LinkedList::Remove(int index) { - // Write your code here ... - return std::nullopt; + if (IsEmpty() || (index < 0) || (index >= size_)) { + return std::nullopt; + } + int deleted_value; + if (index == 0) { + deleted_value = front_->value; + front_ = front_->next; + size_--; + return deleted_value; + } + Node* search_node = front_; + for (int i = 0; i < index; i++) { + if ((index == size_-1)&&(i == index-1)) { + deleted_value = back_->value; + search_node->next = nullptr; + back_ = search_node; + size_--; + return deleted_value; + } + if (i == index-1) { + deleted_value = search_node->next->value; + search_node->next = search_node->next->next; + size_--; + return deleted_value; + } + search_node = search_node->next; + } } + void LinkedList::Clear() { - // Write your code here ... + size_ = 0; + front_ = nullptr; + back_ = nullptr; } std::optional LinkedList::Get(int index) const { - // Write your code here ... - return std::nullopt; + if (IsEmpty()||(index < 0)||(index >= size_)) { + return std::nullopt; + } + Node* search_node = front_; + for (int i = 0; i < index; i++) { + Node* next_node = search_node->next; + search_node = next_node; + } + return search_node->value; } std::optional LinkedList::IndexOf(int value) const { - // Write your code here ... - return std::nullopt; + Node* search_node = front_; + for (int i = 0; i < size_-1; i++) { + Node* next_node = search_node->next; + if (search_node->value == value) { + return i; + } + search_node = next_node; + } + if (search_node->value == value) { + return size_-1; + } + return false; } bool LinkedList::Contains(int value) const { - return false; + Node* search_node = front_; + for (int i = 0; i < size_-1; i++) { + Node* next_node = search_node->next; + if (search_node->value == value) { + return true; + } + search_node = next_node; + } + return search_node->value == value; } bool LinkedList::IsEmpty() const { - return false; + return size_ == 0; } int LinkedList::size() const { - return 0; + return size_; } std::optional LinkedList::front() const { - // Write your code here ... - return std::nullopt; + if (IsEmpty()) { + return std::nullopt; + } + return front_->value; } std::optional LinkedList::back() const { - // Write your code here ... - return std::nullopt; + if (IsEmpty()) { + return std::nullopt; + } + return back_->value; } Node* LinkedList::FindNode(int index) const { - // Write your code here ... - return nullptr; + if (IsEmpty()||(index < 0)||(index >= size_)) { + return nullptr; + } + Node* search_node = front_; + for (int i = 0; i < index; i++) { + Node* next_node = search_node->next; + search_node = next_node; + } + return search_node; } // ДЛЯ ТЕСТИРОВАНИЯ diff --git a/src/linked_queue.cpp b/src/linked_queue.cpp index c99a57d..54540a3 100644 --- a/src/linked_queue.cpp +++ b/src/linked_queue.cpp @@ -9,34 +9,62 @@ namespace assignment { } void LinkedQueue::Enqueue(int value) { - // Write your code here ... + size_ += 1; + if (front_ == nullptr) { + front_ = new Node(value); + back_ = front_; + } else { + back_->next = new Node(value); + back_ = back_->next; + } } bool LinkedQueue::Dequeue() { - // Write your code here ... - return false; + if (size_ <= 0) { + return false; + } + + Node deleted = *front_; + delete front_; + front_ = deleted.next; + size_ -= 1; + return true; } void LinkedQueue::Clear() { - // Write your code here ... + Node* node = front_; + while (node != nullptr) { + Node* next = node->next; + delete node; + node = next; + } + size_ = 0; + front_ = nullptr; + back_ = nullptr; } std::optional LinkedQueue::front() const { - // Write your code here ... - return std::nullopt; + if (front_ == nullptr) { + return std::nullopt; + } + + return front_->value; } std::optional LinkedQueue::back() const { - // Write your code here ... - return std::nullopt; + if (back_ == nullptr) { + return std::nullopt; + } + + return back_->value; } bool LinkedQueue::IsEmpty() const { - return false; + return size_ == 0; } int LinkedQueue::size() const { - return 0; + return size_; } // ДЛЯ ТЕСТИРОВАНИЯ From 0da8fac0146fdb438c3854f5d671657005af2336 Mon Sep 17 00:00:00 2001 From: timyrkaa Date: Sun, 19 Jun 2022 15:13:11 +0300 Subject: [PATCH 13/13] done --- src/array_stack.cpp | 60 +++++++++++++++------- src/dynamic_array.cpp | 92 +++++++++++++++++++++++++++------- src/linked_list.cpp | 113 +++++++++++++++++++++++++++++++++++------- src/linked_queue.cpp | 47 ++++++++++++++---- 4 files changed, 250 insertions(+), 62 deletions(-) diff --git a/src/array_stack.cpp b/src/array_stack.cpp index 18141bc..eb258b3 100644 --- a/src/array_stack.cpp +++ b/src/array_stack.cpp @@ -11,49 +11,75 @@ namespace assignment { if (capacity <= 0) { throw std::invalid_argument("capacity is not positive"); } - - // Write your code here ... + size_ = 0; + capacity_ = capacity; + data_ = new int[capacity_]; + for (int i = 0; i < capacity_; i++) { + data_[i] = 0; + } } ArrayStack::~ArrayStack() { - // Write your code here ... + size_ = 0; + capacity_ = 0; + delete[] data_; + data_ = nullptr; } void ArrayStack::Push(int value) { - // Write your code here ... + if (capacity_ == size_){ + Resize(capacity_ + kCapacityGrowthCoefficient); + } + data_[size_] = value; + size_++; } bool ArrayStack::Pop() { - // Write your code here ... - return false; + if(size_ == 0){ + return false; + } + else{ + data_[size_-1] = 0; + size_--; + return true; + } } void ArrayStack::Clear() { - // Write your code here ... + size_= 0; } std::optional ArrayStack::Peek() const { - // Write your code here ... - return std::nullopt; + if(size_ == 0){ + return std::nullopt; + } + + else { + return data_[size_-1]; + } } bool ArrayStack::IsEmpty() const { - // Write your code here ... - return false; + return size_ == 0; } int ArrayStack::size() const { - // Write your code here ... - return 0; + return size_; } int ArrayStack::capacity() const { - // Write your code here ... - return 0; + return capacity_; } bool ArrayStack::Resize(int new_capacity) { - // Write your code here ... + if (new_capacity > capacity_){ + int* data__ = new int[new_capacity]; + std::copy(&data_[0], &data_[size_], data__); + delete[] data_; + data_ = data__; + capacity_ = new_capacity; + return true; + } return false; } @@ -81,4 +107,4 @@ namespace assignment { return {data_, data_ + capacity_}; } -} // namespace assignment +} // namespace assignment \ No newline at end of file diff --git a/src/dynamic_array.cpp b/src/dynamic_array.cpp index 55745bd..1dc0957 100644 --- a/src/dynamic_array.cpp +++ b/src/dynamic_array.cpp @@ -11,65 +11,121 @@ namespace assignment { if (capacity <= 0) { throw std::invalid_argument("capacity is not positive"); } - - // Write your code here ... + size_ = 0; + capacity_ = capacity; + data_ = new int[capacity_]; + for (int i = 0; i < capacity_; ++i){ + data_[i] = 0; + } } DynamicArray::~DynamicArray() { - // Write your code here ... + size_ = 0; + capacity_ = 0; + + delete[] data_; + data_ = nullptr; } void DynamicArray::Add(int value) { - // Write your code here ... + if (size_ == capacity_){ + Resize(capacity_ + kCapacityGrowthCoefficient); + } + data_[size_] = value; + size_++; } bool DynamicArray::Insert(int index, int value) { - // Write your code here ... - return false; + if (index > size_ || index < 0) { + return false; + } + if (size_ == capacity_){ + Resize(capacity_ + kCapacityGrowthCoefficient); + } + int* data__ = new int[capacity_]; + std::copy(&data_[0], &data_[index], data__); + data__[index] = value; + for (int i = index+1; i < capacity_; i++) { + data__[i] = data_[i-1]; + } + size_++; + data_ = data__; + return true; } bool DynamicArray::Set(int index, int new_value) { - // Write your code here ... - return false; + if (index >= size_ || index < 0){ + return false; + } + data_[index] = new_value; + return true; } std::optional DynamicArray::Remove(int index) { - // Write your code here ... - return std::nullopt; + if (index >= size_ || index < 0) { + return std::nullopt; + } + int* data__ = new int[capacity_]; + std::copy(&data_[0], &data_[index], data__); + int t = data_[index]; + for (int i = index; i < size_ - 1; i++) { + data__[i] = data_[i + 1]; + } + size_--; + data_ = data__; + return t; } void DynamicArray::Clear() { - // Write your code here ... + size_ = 0; } std::optional DynamicArray::Get(int index) const { - // Write your code here ... - return std::nullopt; + if (index >= size_ || index < 0) { + return std::nullopt; + } + return data_[index]; } std::optional DynamicArray::IndexOf(int value) const { - // Write your code here ... + for(int i = 0; i < size_; i++){ + if(data_[i] == value){ + return i; + } + } return std::nullopt; } bool DynamicArray::Contains(int value) const { + for(int i = 0; i < size_; i++){ + if(data_[i] == value){ + return true; + } + } return false; } bool DynamicArray::IsEmpty() const { - return false; + return size_ == 0; } int DynamicArray::size() const { - return 0; + return size_; } int DynamicArray::capacity() const { - return 0; + return capacity_; } bool DynamicArray::Resize(int new_capacity) { - // Write your code here ... + if (new_capacity > capacity_){ + int* data__ = new int[new_capacity]; + std::copy(&data_[0], &data_[size_], data__); + delete[] data_; + data_ = data__; + capacity_ = new_capacity; + return true; + } return false; } diff --git a/src/linked_list.cpp b/src/linked_list.cpp index 4497560..aaeaace 100644 --- a/src/linked_list.cpp +++ b/src/linked_list.cpp @@ -9,62 +9,141 @@ namespace assignment { } void LinkedList::Add(int value) { - // Write your code here ... + Node* element = new Node(value); + if(front_ != nullptr){ + back_ ->next = element; + back_ = element; + } + else{ + front_ = element; + back_ = element; + } + size_++; } bool LinkedList::Insert(int index, int value) { - // Write your code here ... - return false; + if (index > size_ || index < 0) { + return false; + } + Node* insertV = new Node(value); + if (IsEmpty()) { + back_ = insertV; + front_ = insertV; + } + else if (index == size_){ + back_ -> next = insertV; + back_ = insertV; + } + else if (index == 0){ + insertV -> next = front_; + front_ = insertV; + } + else { + insertV->next = FindNode(index - 1)->next; + FindNode(index - 1) -> next = insertV; + } + size_++; + return true; } bool LinkedList::Set(int index, int new_value) { - return false; + if (index >= size_ || index < 0) { + return false; + } + else { + FindNode(index) -> value = new_value; + return true; + } } std::optional LinkedList::Remove(int index) { - // Write your code here ... - return std::nullopt; + if (index >= size_ || index < 0) { + return std::nullopt; + } + int remValue; + if (index != 0){ + remValue = FindNode(index)->value; + FindNode(index-1) -> next = FindNode(index) -> next; + } + else { + remValue = (front_ -> value); + front_= (front_ -> next); + } + size_--; + return remValue; } void LinkedList::Clear() { - // Write your code here ... + Node* temp = front_; + for (int i = 0; i < size_; i++) { + Node* curr = temp -> next; + delete temp; + temp = curr; + } + size_ = 0; + front_ = nullptr; + back_ = nullptr; } std::optional LinkedList::Get(int index) const { - // Write your code here ... - return std::nullopt; + if (index >= size_ || index < 0) { + return std::nullopt; + } + else { + Node* n = FindNode(index); + return n -> value; + } } std::optional LinkedList::IndexOf(int value) const { - // Write your code here ... + Node* temp = front_; + for (int i = 0; i < size_; i++) { + if (temp->value == value) { + return i; + } + temp = temp->next; + } return std::nullopt; } bool LinkedList::Contains(int value) const { + if (IndexOf(value) != std::nullopt){ + return true; + } return false; } bool LinkedList::IsEmpty() const { - return false; + return size_ == 0; } int LinkedList::size() const { - return 0; + return size_; } std::optional LinkedList::front() const { - // Write your code here ... - return std::nullopt; + if (front_ != nullptr) { + return front_->value; + } + return std::nullopt; } std::optional LinkedList::back() const { - // Write your code here ... + if (back_ != nullptr) { + return back_->value; + } return std::nullopt; } Node* LinkedList::FindNode(int index) const { - // Write your code here ... - return nullptr; + if (index >= size_ || index < 0){ + return nullptr; + } + Node* node = front_; + for (int i = 0; i < index; i++) { + node = node->next; + } + return node; } // ДЛЯ ТЕСТИРОВАНИЯ diff --git a/src/linked_queue.cpp b/src/linked_queue.cpp index c99a57d..7558389 100644 --- a/src/linked_queue.cpp +++ b/src/linked_queue.cpp @@ -9,34 +9,61 @@ namespace assignment { } void LinkedQueue::Enqueue(int value) { - // Write your code here ... + Node* temp = new Node(value); + if (IsEmpty()){ + front_ = temp; + back_ = temp; + } + else { + back_ -> next = temp; + back_ = temp; + } + size_++; } bool LinkedQueue::Dequeue() { - // Write your code here ... - return false; + if (IsEmpty()){ + return false; + } + else { + Node* temp = front_; + delete front_; + front_ = temp -> next; + } + size_ --; } void LinkedQueue::Clear() { - // Write your code here ... + size_ = 0; + front_ = nullptr; + back_ = nullptr; } std::optional LinkedQueue::front() const { - // Write your code here ... - return std::nullopt; + if (front_ == nullptr) { + return std::nullopt; + } + + else { + return front_ -> value; + } } std::optional LinkedQueue::back() const { - // Write your code here ... - return std::nullopt; + if (back_ == nullptr) { + return std::nullopt; + } + else { + return back_ -> value; + } } bool LinkedQueue::IsEmpty() const { - return false; + return size_ == 0; } int LinkedQueue::size() const { - return 0; + return size_; } // ДЛЯ ТЕСТИРОВАНИЯ