aboutsummaryrefslogtreecommitdiff
path: root/Homework8/MyStructures/ContactList.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'Homework8/MyStructures/ContactList.hpp')
-rw-r--r--Homework8/MyStructures/ContactList.hpp167
1 files changed, 154 insertions, 13 deletions
diff --git a/Homework8/MyStructures/ContactList.hpp b/Homework8/MyStructures/ContactList.hpp
index b324064..a2d1efa 100644
--- a/Homework8/MyStructures/ContactList.hpp
+++ b/Homework8/MyStructures/ContactList.hpp
@@ -28,17 +28,18 @@ namespace MyStructures
void Prepend(const C& data); //add contact as the first item
void RemoveLast(); //remove last
void RemoveFirst(); //remove first
- void Extract(const C& data); //delete this data, slash collecting its data
+ void Extract(const C& data); //delete this data, slash collecting its data ;;return 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 ContactDeleter(const size_t& size);
+ void DeleteContact(const size_t& choice);
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
C& First(); //returning first contact in list
C First() const; //returning first contact copy
-
+ void ContactDoubler();
C& operator[](const int& index); //like a getter/ returns contact reference via squarebracket overload
C operator[](const int& index) const; //returns a contact copy via squarebracket overload
@@ -158,7 +159,9 @@ namespace MyStructures
{
for (auto i = 0u; i < size_; ++i)
{
+
contacts_[i].Print();
+
}
}
@@ -194,13 +197,25 @@ namespace MyStructures
index_locate = size_;
}
- if (size_ >= length_)
+ if (Full())
{
- *this = ContactList(contacts_, length_ * 2);
+ auto temp = ContactList<C>{ length_ * 2 };
+ for (auto i = 0; i <= size_; ++i)
+ {
+ temp[i] = contacts_[i];
+ }
+ temp[size_] = data;
+
+ delete[] contacts_;
+ contacts_ = nullptr;
+
+ *this = std::move(temp);
+ size_+=4;
+ return;
}
//Add to end of the array
contacts_[index_locate] = data;
- size_++;
+ ++size_;
}
template<class C>
void ContactList<C>::Prepend(const C& data)
@@ -228,7 +243,7 @@ namespace MyStructures
}
if (Full())
{
- auto temp = ContactList<C>{length_};
+ auto temp = ContactList<C>{length_*2};
temp.Append(data);
for (auto i = 0; i <= size_; ++i)
{
@@ -276,19 +291,113 @@ namespace MyStructures
template<class C>
void ContactList<C>::Extract(const C& data)
{
+ if (Empty())return;
+ auto tempo = ContactList<C>{ length_ };
+ for (auto i = 0u; i < size_; i++)
+ {
+ if (contacts_[i] == data)
+ {
+ tempo.Append(data); //the extraction
+ ContactDeleter(i);
+ for (auto j = size_; j > i; j--)
+ {
+ contacts_[j - 1] = contacts_[j];
+ }
+
+ }
+
+ }
+ size_--;
+ //find where data is in the contacts
+ //extract data
+ //remove data
+ //decrement size after
+ //if there are elements above data, shift down after
+
+
}
template<class C>
void ContactList<C>::InsertAfter(const C& data, const C& after)
{
- if (Empty()) return;
-
- //if full, double size
-
+ if (Empty()) Append(data); return;
+ if (Full()) ContactDoubler();
+
+ if (after == contacts_[0]) //checks if after is the first contact
+ {
+ for (auto i = 1u; i < size_; i++)
+ {
+ contacts_[i + 1] = contacts_[i]; // shift the contacts up to make space for the incoming data
+ }
+ contacts_[1] = data;
+ size_++;
+ return;
+ }
+
+ if (after == contacts_[size_ - 1]) //checks if it is after the last contact
+ {
+ contacts_[size_] = data;
+ size_++;
+ return;
+ }
+
+ else
+ {
+ for (auto i = size_-1; i>0; i--) //shifts the content after "after"
+ {
+ if (contacts_[i] != after)
+ {
+ contacts_[i + 1] = contacts_[i];
+ }
+ }
+
+ for (auto j = 0u; j < size_; j++) //assigns the data after "after"
+ {
+ if (contacts_[j] == after)
+ {
+ contacts_[j + 1] = data;
+ }
+ }size_++;
+ return;
+ }
}
+
template<class C>
void ContactList<C>::InsertBefore(const C& data, const C& before)
{
+ if (Empty()) Append(data); return;
+ bool found = false;
+ for (auto i = 0u; i < size_ ; i++)
+ {
+ found = true;
+ if (contacts_[i] == before)
+ {
+ switch (i)
+ {
+ default:
+ auto temp = ContactList<C>{ length_ };
+ for (auto j = 0u; j < i; ++j)
+ {
+ temp.Append(contacts_[j]);
+ }
+ temp.Append(data);
+ for (auto k = i; k < size_; ++k)
+ {
+ temp.Append(contacts_[k]);
+ }
+ delete[] contacts_;
+ contacts_ = nullptr;
+ *this = std::move(temp);
+ }
+ }
+ }
+
+ if (!found)
+ {
+ cout << "Error: Before data not found" << endl;
+ }
+
}
+
template<class C>
void ContactList<C>::Clear()
{
@@ -297,6 +406,7 @@ namespace MyStructures
delete[] contacts_;
contacts_ = nullptr;
}
+
template<class C>
inline void ContactList<C>::ContactDeleter(const size_t& size)
{
@@ -310,21 +420,37 @@ namespace MyStructures
}
template<class C>
+ inline void ContactList<C>::DeleteContact(const size_t& choice)
+ {
+ ContactDeleter(choice);
+ for (auto i = size_; i >= choice; i--)
+ {
+ contacts_[i+1] = contacts_[i];
+ }
+ size_--;
+
+ }
+
+
+
+ template<class C>
inline C& ContactList<C>::Last()
{
return contacts_[size_-1];
- // TODO: insert return statement here
+
}
+
template<class C>
inline C ContactList<C>::Last() const
{
return contacts_[size_-1];
}
+
template<class C>
inline C& ContactList<C>::First()
{
return contacts_[0];
- // TODO: insert return statement here
+
}
template<class C>
inline C ContactList<C>::First() const
@@ -332,6 +458,20 @@ namespace MyStructures
return contacts_[0];
}
template<class C>
+ inline void ContactList<C>::ContactDoubler()
+ {
+ auto temp = ContactList<C>{ length_ * 2 }; //doubles the length
+ for (auto i = 0u; i <= size_; ++i)
+ {
+ temp.Append(contacts_[i]);
+ }
+ delete[] contacts_;
+ contacts_ = nullptr;
+
+ *this = std::move(temp);
+ size_--;
+ }
+ template<class C>
inline C& ContactList<C>::operator[](const int& index)
{
if (index < 0 || index >= static_cast<int>(length_))
@@ -341,7 +481,7 @@ namespace MyStructures
return EmptyContact;
}
return contacts_[index];
- // TODO: insert return statement here
+
}
template<class C>
inline C ContactList<C>::operator[](const int& index) const
@@ -375,6 +515,7 @@ namespace MyStructures
}
return true;
}
+
template<class C>
inline bool ContactList<C>::Empty() const
{