diff --git a/lab_3/sequences-int/dynamic-array.cpp b/lab_3/sequences-int/dynamic-array.cpp index 068a539..8e6a601 100644 --- a/lab_3/sequences-int/dynamic-array.cpp +++ b/lab_3/sequences-int/dynamic-array.cpp @@ -15,9 +15,9 @@ void DynamicArray::clear() } // insert an item at index i -void DynamicArray::insert_at(int i, const int& inserted_item) +void DynamicArray::enqueue(int value) { - assert((i >= 0) && (this->logical_size >= i)); + int i = this->logical_size; // catch the case where capacity is exhausted and we need to allocate more memory if(this->logical_size == this->capacity) @@ -45,13 +45,13 @@ void DynamicArray::insert_at(int i, const int& inserted_item) // now we can write the inserted item into values[i] this->logical_size++; - this->values[i] = inserted_item; + this->values[i] = value; } // remove the item at index i -void DynamicArray::erase_at(int i) +int DynamicArray::dequeue() { - assert((i >= 0) && this->logical_size > i); + int i = 0; // shift all elements from index i+1 onward one to the left // we use a temporary storage and copy() from to do this efficiently diff --git a/lab_3/sequences-int/dynamic-array.h b/lab_3/sequences-int/dynamic-array.h index e632b30..feb9630 100644 --- a/lab_3/sequences-int/dynamic-array.h +++ b/lab_3/sequences-int/dynamic-array.h @@ -1,48 +1,18 @@ +#include "queue.h" #include "sequence.h" namespace seq { - class DynamicArray: public Sequence + class DynamicArray: public Queue { public: bool empty() const { return (this->logical_size == 0); } // test whether the array is empty size_t size() const { return this->logical_size; } // return the logical size (number of items in the array) - // it is the caller's responsibility to ensure that the array is not empty! - int& front() { return this->values[0]; } // return a reference to the first item - int& back() { return this->values[this->logical_size - 1]; } // return a reference to the final item + void enqueue(int element); + int dequeue(); - // return a reference to the item at position i of the sequence, counting from 0 - // we use modulo arithmetics to avoid over-/underflow; will still fail for an empty array - int& at(int i) { return this->values[i % this->logical_size]; } - - /* - * accepts an additional item into the dynamic array; - * by default, this is done at the back end of the array - * call push_front(...) to push an element at the front - * - * the array takes ownership of the copy (but not of the original!) - */ - void push(const int& pushed_item) { this->push_back(pushed_item); } - void push_back(const int& pushed_item) { this->insert_at(this->logical_size, pushed_item); } - void push_front(const int& pushed_item) { this->insert_at(0, pushed_item); } - - /* - * removes an item from the list (back end by default) - * to do the same at the front, call pop_front() - */ - void pop() { this->pop_back(); } - void pop_front() { this->erase_at(0); } - void pop_back() { this->erase_at(this->logical_size - 1); } void clear(); // remove all the items from the array - - void insert_at(int i, const int& inserted_item); // insert an item at index i - void erase_at(int i); // remove the item at index i - - // overwrite the element at index i - // we use modulo arithmetics to avoid over-/underflow; will still fail for an empty array - void set_value_at(int i, const int& in_item) { this->values[i % this->logical_size] = in_item; } - ~DynamicArray() { this->clear(); } private: diff --git a/lab_3/sequences-int/sequence-test.cpp b/lab_3/sequences-int/sequence-test.cpp index c90c69b..95fa959 100644 --- a/lab_3/sequences-int/sequence-test.cpp +++ b/lab_3/sequences-int/sequence-test.cpp @@ -48,11 +48,11 @@ namespace int main() { int iterations = 200; - /* + std::cout << "*** test with dynamic array ***\n"; seq::DynamicArray dyna; float dyna_time = test_with_time_measurement(&dyna, iterations); - */ + std::cout << "\n\n*** test with singly linked list ***\n"; seq::SinglyLinkedList sll; float sll_time = test_with_time_measurement(&sll, iterations); @@ -61,7 +61,7 @@ int main() seq::DoublyLinkedList dll; float dll_time = test_with_time_measurement(&dll, iterations); - //std::cout << "\n\nRuntime for dynamic array:\t" << dyna_time << " s\n"; + std::cout << "\n\nRuntime for dynamic array:\t" << dyna_time << " s\n"; std::cout << "Runtime for singly linked list:\t" << sll_time << " s\n"; std::cout << "Runtime for doubly linked list:\t" << dll_time << " s\n"; }