Skip to content
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

## 1. Информация о студенте

**Номер группы**: 00-000
**Номер группы**: 11-109

**Фамилия и Имя**: Иванов Иван
**Фамилия и Имя**: Султанов Тимур

## 2. Описание задания

Expand Down
60 changes: 43 additions & 17 deletions src/array_stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> 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;
}

Expand Down Expand Up @@ -81,4 +107,4 @@ namespace assignment {
return {data_, data_ + capacity_};
}

} // namespace assignment
} // namespace assignment
92 changes: 74 additions & 18 deletions src/dynamic_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> 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<int> 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<int> 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;
}

Expand Down
Loading