From 5684c0b29cb9d0360103fce41b266410c05fa267 Mon Sep 17 00:00:00 2001 From: Shaun Reed Date: Thu, 16 Apr 2020 11:16:11 -0400 Subject: [PATCH] Work on SingleList class big 3 --- plates/linkedlists/driver.cpp | 4 +++- plates/linkedlists/singlelist.cpp | 13 ++++++------- plates/linkedlists/singlelist.h | 2 +- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/plates/linkedlists/driver.cpp b/plates/linkedlists/driver.cpp index 6dd3771..7d791b9 100644 --- a/plates/linkedlists/driver.cpp +++ b/plates/linkedlists/driver.cpp @@ -23,8 +23,10 @@ int main() bool exit = false; int choice = -1; int val, key; + SingleList test2; while (!exit) { + test2 = testList; std::cout << "##### Singly Linked List Menu #####\n\t0. Exit" << "\n\t1. Insert\n\t2. Insert at\n\t3. Empty list\n\t4. Peek top of list" << "\n\t5. Print list\n\t6. Find\n\t7. Remove\n\t8. Replace\n"; @@ -67,7 +69,7 @@ int main() case PRINT: testList.print(); - // test2.print(); + test2.print(); // test3.print(); break; diff --git a/plates/linkedlists/singlelist.cpp b/plates/linkedlists/singlelist.cpp index ce47b59..4d94add 100644 --- a/plates/linkedlists/singlelist.cpp +++ b/plates/linkedlists/singlelist.cpp @@ -19,9 +19,8 @@ * * @param rhs SingleList object */ -SingleList::SingleList(const SingleList& rhs) +SingleList::SingleList(const SingleList& rhs) : head (rhs.head) { - head = rhs.head; } /** @@ -30,10 +29,11 @@ SingleList::SingleList(const SingleList& rhs) * @param rhs SingleList object * @return SingleList& The copied rhs SingleList to the lhs of the assignment */ -SingleList& SingleList::operator=(const SingleList& rhs) +SingleList SingleList::operator=(SingleList rhs) { - makeEmpty(); - head = rhs.head; + if (this == &rhs) return *this; + std::swap(head, rhs.head); + // head = rhs.head; return *this; } @@ -117,9 +117,8 @@ bool SingleList::replace(int val, int key) */ void SingleList::makeEmpty() { - Node *temp; + Node *temp(head); while(!isEmpty()) { - temp = head; head = head->next; delete temp; } diff --git a/plates/linkedlists/singlelist.h b/plates/linkedlists/singlelist.h index 7d12701..9028119 100644 --- a/plates/linkedlists/singlelist.h +++ b/plates/linkedlists/singlelist.h @@ -22,7 +22,7 @@ class SingleList{ public: SingleList() : head(NULL) {}; SingleList(const SingleList& rhs); - SingleList& operator=(const SingleList& rhs); + SingleList operator=(SingleList rhs); ~SingleList(); bool insert(int val); bool insert(int val, int key);