diff options
Diffstat (limited to 'Homework8/MyStructures/ContactList.hpp')
| -rw-r--r-- | Homework8/MyStructures/ContactList.hpp | 129 |
1 files changed, 114 insertions, 15 deletions
diff --git a/Homework8/MyStructures/ContactList.hpp b/Homework8/MyStructures/ContactList.hpp index e7959a8..bc2240f 100644 --- a/Homework8/MyStructures/ContactList.hpp +++ b/Homework8/MyStructures/ContactList.hpp @@ -31,8 +31,8 @@ namespace MyStructures void Extract(const C& data); //delete this data void InsertAfter(const C& data, const C& after); //Insert contact data after 'after' void InsertBefore(const C& data, const C& before); //Insert contact data before 'before' - void Clear(); // //deletes all contacts, empties list, nothing left - + void Clear(); //deletes all contacts, empties list, nothing left + void ContactDeleter(const size_t& size); C& Last(); //returning a reference to contact, it gives last contact in list C Last() const; //const version of this, unchanging because its const and its a copy @@ -47,7 +47,7 @@ namespace MyStructures bool operator==(const ContactList<C>& rhs) const; //overloading == e.g if(newcontactlist == oldcontactlist) bool Empty() const; //is the list empty - + bool Full() const; //returns true if allocated private: C* contacts_{ nullptr }; size_t length_{ 0 }; //entire length of array @@ -66,7 +66,7 @@ namespace MyStructures ContactList<C>::ContactList(const C* contacts, const size_t& length) { contacts_ = AllocateContactList(length); - for (auto i = 0u; i < length; ++i) + for (auto i = 0u; i < size_; ++i) { contacts_[i] = contacts[i]; } @@ -76,19 +76,30 @@ namespace MyStructures { delete[] contacts_; contacts_ = nullptr; + + } template<class C> ContactList<C>::ContactList(const ContactList& copy) { - length_ = copy.length_; + //length_ = copy.length_; + //size_ = copy.size_; + //contacts_ = AllocateContactList(copy.length_); + + //contacts_ = AllocateContactList(length_); + //for (auto i = 0u; i < length_; ++i) + //{ + // contacts_[i] = copy[i]; + //} + + delete[] contacts_; + contacts_ = nullptr; + + auto temp = ContactList<C>{ copy.contacts_, copy.length_ }; + *this = std::move(temp); size_ = copy.size_; - contacts_ = AllocateContactList(copy.length_); - contacts_ = AllocateContactList(length_); - for (auto i = 0u; i < length_; ++i) - { - contacts_[i] = copy[i]; - } + } template<class C> @@ -145,7 +156,7 @@ namespace MyStructures template<class C> void ContactList<C>::PrintList() const { - for (auto i = 0u; i < length_; ++i) + for (auto i = 0u; i < size_; ++i) { contacts_[i].Print(); } @@ -162,6 +173,7 @@ namespace MyStructures { C* storage = nullptr; length_ = length; + storage = new C[length]{}; return storage; @@ -193,15 +205,78 @@ namespace MyStructures template<class C> void ContactList<C>::Prepend(const C& data) { + //data goes to element zero + + if (Empty()) + { + if (contacts_ == nullptr) + { + + } + + Append(data); + size_++; + return; + } + //move all elements +1 element + //check for sizing + //check for length + if (!Full()) + { + for (auto i = 0; i<size_; ++i) + { + contacts_[i+1] = contacts_[i]; + } + contacts_[0] = data; + size_++; + return; + } + if (Full()) + { + auto temp = ContactList<C>{length_}; + temp.Append(data); + for (auto i = 0; i <= size_; ++i) + { + temp.Append(contacts_[i]); + } + + delete[] contacts_; + contacts_ = nullptr; + + *this = std::move(temp); + } + size_++; + } template<class C> void ContactList<C>::RemoveLast() { + if (Empty()) return; + + //clear the last member + ContactDeleter(size_ - 1); + + size_--; + } template<class C> void ContactList<C>::RemoveFirst() { + if (Empty()) return; + + //clear the first member + ContactDeleter(0); + //shift so the program doesn't throw in the print function + for (auto i = 0u; i < size_; i++) + { + contacts_[i] = contacts_[i + 1]; + } + //just to eliminate the last cell from the copy assignment above^^^^ + ContactDeleter(size_ - 1); + size_--; + + } template<class C> void ContactList<C>::Extract(const C& data) @@ -218,10 +293,27 @@ namespace MyStructures template<class C> void ContactList<C>::Clear() { + size_ = 0; + length_ = 0; + delete[] contacts_; + contacts_ = nullptr; } template<class C> + inline void ContactList<C>::ContactDeleter(const size_t& size) + { + contacts_[size].SetFirstName(nullptr); + contacts_[size].SetLastName(nullptr); + contacts_[size].SetEmail(nullptr); + contacts_[size].SetStreet(nullptr); + contacts_[size].SetState(nullptr); + contacts_[size].SetCity(nullptr); + contacts_[size].SetZip(0); + } + + template<class C> inline C& ContactList<C>::Last() { + return contacts_[size_-1]; // TODO: insert return statement here } template<class C> @@ -258,11 +350,11 @@ namespace MyStructures template<class C> inline bool ContactList<C>::operator==(const ContactList<C>& rhs) const { - if (contacts_ == rhs.contacts_) return true; + if (&contacts_ == &rhs.contacts_) return true; - if (size_ != rhs.size_) return false; + if (&size_ != &rhs.size_) return false; - if (length_ != rhs.length_) return false; + if (&length_ != &rhs.length_) return false; for (auto i = 0u; i < size_; ++i) { @@ -275,6 +367,13 @@ namespace MyStructures { return size_ == 0; } + template<class C> + bool ContactList<C>::Full() const + { + if (size_ < length_) return false; + + return true; + } }; |