This commit is contained in:
Trygve 2024-03-10 14:24:50 +01:00
parent e3b70d57ed
commit 56cdc9248b
3 changed files with 12 additions and 42 deletions

View File

@ -15,9 +15,9 @@ void DynamicArray::clear()
} }
// insert an item at index i // 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 // catch the case where capacity is exhausted and we need to allocate more memory
if(this->logical_size == this->capacity) 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] // now we can write the inserted item into values[i]
this->logical_size++; this->logical_size++;
this->values[i] = inserted_item; this->values[i] = value;
} }
// remove the item at index i // 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 // shift all elements from index i+1 onward one to the left
// we use a temporary storage and copy() from <algorithm> to do this efficiently // we use a temporary storage and copy() from <algorithm> to do this efficiently

View File

@ -1,48 +1,18 @@
#include "queue.h"
#include "sequence.h" #include "sequence.h"
namespace seq namespace seq
{ {
class DynamicArray: public Sequence class DynamicArray: public Queue
{ {
public: public:
bool empty() const { return (this->logical_size == 0); } // test whether the array is empty 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) 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! void enqueue(int element);
int& front() { return this->values[0]; } // return a reference to the first item int dequeue();
int& back() { return this->values[this->logical_size - 1]; } // return a reference to the final item
// 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 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(); } ~DynamicArray() { this->clear(); }
private: private:

View File

@ -48,11 +48,11 @@ namespace
int main() int main()
{ {
int iterations = 200; int iterations = 200;
/*
std::cout << "*** test with dynamic array ***\n"; std::cout << "*** test with dynamic array ***\n";
seq::DynamicArray dyna; seq::DynamicArray dyna;
float dyna_time = test_with_time_measurement(&dyna, iterations); float dyna_time = test_with_time_measurement(&dyna, iterations);
*/
std::cout << "\n\n*** test with singly linked list ***\n"; std::cout << "\n\n*** test with singly linked list ***\n";
seq::SinglyLinkedList sll; seq::SinglyLinkedList sll;
float sll_time = test_with_time_measurement(&sll, iterations); float sll_time = test_with_time_measurement(&sll, iterations);
@ -61,7 +61,7 @@ int main()
seq::DoublyLinkedList dll; seq::DoublyLinkedList dll;
float dll_time = test_with_time_measurement(&dll, iterations); 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 singly linked list:\t" << sll_time << " s\n";
std::cout << "Runtime for doubly linked list:\t" << dll_time << " s\n"; std::cout << "Runtime for doubly linked list:\t" << dll_time << " s\n";
} }