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..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_; } // ДЛЯ ТЕСТИРОВАНИЯ