diff options
| author | Yana Blashchishina <[email protected]> | 2024-03-11 16:43:04 -0700 |
|---|---|---|
| committer | Yana Blashchishina <[email protected]> | 2024-03-11 16:43:04 -0700 |
| commit | 88b1c26f4066734637853a90fcfd8f9eeb48ebed (patch) | |
| tree | 7ebc96e79f323b2c17b555569211d6d7242e8ff7 /Homework 8/MyStructures/ContactList.hpp | |
| parent | CharArrays (diff) | |
| download | homework-8-yanablash-88b1c26f4066734637853a90fcfd8f9eeb48ebed.tar.xz homework-8-yanablash-88b1c26f4066734637853a90fcfd8f9eeb48ebed.zip | |
errors
Diffstat (limited to 'Homework 8/MyStructures/ContactList.hpp')
| -rw-r--r-- | Homework 8/MyStructures/ContactList.hpp | 70 |
1 files changed, 56 insertions, 14 deletions
diff --git a/Homework 8/MyStructures/ContactList.hpp b/Homework 8/MyStructures/ContactList.hpp index 887ebfc..3824a0e 100644 --- a/Homework 8/MyStructures/ContactList.hpp +++ b/Homework 8/MyStructures/ContactList.hpp @@ -3,7 +3,7 @@ #include <algorithm> -namespace MySStructures +namespace MyStructures { template<class C> class ContactList @@ -13,8 +13,8 @@ namespace MySStructures * Contains an array of Contacts or othe C class objects */ ContactList() = default; - ContactList(size_t length); //Creates contact list of this size - ContactList(const C* contacts, const size_t& length); //Makes a copy of another contact list + ContactList(size_t length); //Creates contact list of this size + ContactList(const C* contacts, const size_t& length, const size_t size); //Makes a copy of another contact list ContactList(const C* storage, const size_t& length); ~ContactList(); @@ -35,12 +35,13 @@ namespace MySStructures void InsertAfter(const C& data, const C& after); //insert contact data after 'after' contact void InsertBefore(const C& data, const C& before); //insert contact data before 'before' contact void Clear(); //Deletes all contacts, empties the list, nothing left! - + void ContactDelete(const size_t& elementNumber); + C& Last(); //returns last contact reference in list C Last() const; //returns copy of last contact in list C& First(); //returns first contact reference in lsit C First() const; - C First() cosnt; //returns first contact copy + //returns first contact copy C& operator[](const int& index); //returns contact reference via [] overload C operator[](const int& index) const; //returns contact copy via [] overload @@ -61,19 +62,30 @@ namespace MySStructures C* AllocateContactList(const size_t& length); }; + + template<class C> + void ContactList<C>::ContactDelete(const size_t& elementNumber) + { + contacts_[elementNumber].Delete(); + } + template <class C> - ContactList<C>::ContactList(size_t length) + ContactList<C>::ContactList(const C* contacts, const size_t& length) { contacts_ = AllocateContactList(length); + for (auto i = 0u; i < length_; ++i) + { + contacts_[i] = contacts[i]; + } } template <class C> - ContactList<C>::ContactList(const C* contacts, const size_t& length) + ContactList<C>::ContactList(const C* contacts, const size_t& length, const size_t size) { contacts_ = AllocateContactList(length); - for (auto i = 0u; i < size_; ++i) + for (auto i = 0u; i < length_; ++i) { contacts_[i] = contacts[i]; } @@ -162,15 +174,27 @@ namespace MySStructures void ContactList<C>::Append(const C& data) { - size_t index = 0; + index = size_; + + contacts_[index] = data; + size_++; + return; + // whatifs; //whatif: end of the array is the beginning //whatif: in the body of the array - if (size_ < length_) + if (size_ >= length_) { index = size_; } + + if (size_ == 0) + { + contacts_[index] = data; + size_++; + return; + } //whatif: end of the array //length_ == 10; // size_ ==10 @@ -188,6 +212,7 @@ namespace MySStructures // Add to end of the array contacts_[index] = data; size_++; + return; } template <class C> @@ -195,6 +220,10 @@ namespace MySStructures { if (Empty()) { + if(contacts_ == nullptr) + { + + } Append(data); return; } @@ -202,7 +231,7 @@ namespace MySStructures if (!Full()) { - for (auto i = size_-1; i >= 0; i--) + for (int i = static_cast<int> (size_)-1; i >= 0; i--) { contacts_[i + 1] = contacts_[i]; } @@ -210,7 +239,7 @@ namespace MySStructures return; } - auto temp = ContactList<C>(length_ * 2); + auto temp = ContactList<C>(length_); temp.Append(data); for (auto i = 0; i < size_ ++i) { @@ -227,7 +256,10 @@ namespace MySStructures template <class C> void ContactList<C>::RemoveLast() { + if (Empty()) return; + ContactDelete(size_ - 1); + size_--; } template <class C> @@ -290,13 +322,23 @@ namespace MySStructures template<class C> inline C& ContactList<C>::operator[](const int& index) { - // TODO: insert return statement here + if (index < 0 || index >= static_cast<int>(length_)) + { + cerr << "The index exceeds the array length" << endl; + return emptyContact; + } + Contact contact = contacts_[index]; + return contact; } template <class C> C ContactList<C>::operator[](const int& index) const { - //TODO CHECK RANGE + if (index < 0 || index >= static_cast<int>(size_)) + { + cerr << "The index exceeds the array length" << endl; + return NULL;; + } return contacts_[index]; } |