diff --git a/.vscode/launch.json b/.vscode/launch.json index f5d9fc7..93fecc4 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -51,7 +51,7 @@ "environment": [], "MIMode": "gdb", "miDebuggerPath": "/usr/bin/gdb", - "preLaunchTask": "C/C++: g++ build active file" + "preLaunchTask": "C/C++: g++ build single active file" }, { "name": "nybusninesses", diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 9a9af73..8aa32e9 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -25,6 +25,32 @@ "isDefault": true }, "detail": "Task generated by Debugger." + }, + { + "type": "cppbuild", + "label": "C/C++: g++ build single active file", + "command": "/usr/bin/g++", + "args": [ + "-fdiagnostics-color=always", + "-std=c++17", + "-Wall", + "-Wextra", + "-g", + "${file}", + "-o", + "${fileDirname}/${fileBasenameNoExtension}" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." } ], "version": "2.0.0" diff --git a/labs/list_implementation/checkpoint1.cpp b/labs/list_implementation/checkpoint1.cpp index 3e3a35f..4c6c412 100644 --- a/labs/list_implementation/checkpoint1.cpp +++ b/labs/list_implementation/checkpoint1.cpp @@ -28,12 +28,8 @@ int main() { // clear out the list a.clear(); - /* - assert (a.size() == 0); - */ - + assert (a.size() == 0); - /* // simple tests of push_front, pop_front, and pop_back a.push_front(5); a.push_back(7); @@ -66,7 +62,6 @@ int main() { a.pop_front(); assert (a.size() == 0); assert (a.begin() == a.end()); - */ return 0; } diff --git a/labs/list_implementation/checkpoint2 b/labs/list_implementation/checkpoint2 new file mode 100755 index 0000000..f5eac4d Binary files /dev/null and b/labs/list_implementation/checkpoint2 differ diff --git a/labs/list_implementation/checkpoint2.cpp b/labs/list_implementation/checkpoint2.cpp index e3631d8..d6d1701 100644 --- a/labs/list_implementation/checkpoint2.cpp +++ b/labs/list_implementation/checkpoint2.cpp @@ -21,6 +21,36 @@ public: NodeB* prev; }; +template +void PushBack(NodeType*& head, NodeType* newNode) { + // If the list is empty, newNode becomes the head + if (!head) { + head = newNode; + } else { + // Otherwise, find the tail and attach there + NodeType* current = head; + while (current->next != nullptr) { + current = current->next; + } + current->next = newNode; + newNode->prev = current; + } +} + +template +void PrintList(NodeType* head) { + NodeType* current = head; + while (current != nullptr) { + // For doubles, you might do: std::cout << std::fixed << std::setprecision(5); + std::cout << current->data; + if (current->next != nullptr) { + std::cout << " -> "; + } + current = current->next; + } + std::cout << " -> nullptr" << std::endl; +} + int main() { // Part 1: test NodeA class. // Initialize an empty linked list, consisting of NodeA nodes. diff --git a/labs/list_implementation/checkpoint3 b/labs/list_implementation/checkpoint3 new file mode 100755 index 0000000..53628bd Binary files /dev/null and b/labs/list_implementation/checkpoint3 differ diff --git a/labs/list_implementation/checkpoint3.cpp b/labs/list_implementation/checkpoint3.cpp index 6474b38..a14e799 100644 --- a/labs/list_implementation/checkpoint3.cpp +++ b/labs/list_implementation/checkpoint3.cpp @@ -15,6 +15,51 @@ public: // this function returns a pointer pointing to the head node of the merged list. template Node* mergeLists(Node* head_A, Node* head_B) { + // If one list is empty, return the other + if (!head_A) return head_B; + if (!head_B) return head_A; + + // Decide the new head by comparing the first elements + Node* head = nullptr; + Node* tail = nullptr; + + // Pick whichever head is smaller as the new head + if (head_A->value < head_B->value) { + head = head_A; + head_A = head_A->next; + } else { + head = head_B; + head_B = head_B->next; + } + // At this point, 'head' is the first node of the merged list + head->prev = nullptr; // new head has no previous + tail = head; + + // Continue merging while both lists have nodes + while (head_A && head_B) { + if (head_A->value < head_B->value) { + tail->next = head_A; + head_A->prev = tail; + tail = tail->next; + head_A = head_A->next; + } else { + tail->next = head_B; + head_B->prev = tail; + tail = tail->next; + head_B = head_B->next; + } + } + + // Append any remaining nodes from A or B + if (head_A) { + tail->next = head_A; + head_A->prev = tail; + } else if (head_B) { + tail->next = head_B; + head_B->prev = tail; + } + + return head; } int main() { diff --git a/labs/list_implementation/dslist.h b/labs/list_implementation/dslist.h index c59ef55..eabe73b 100644 --- a/labs/list_implementation/dslist.h +++ b/labs/list_implementation/dslist.h @@ -131,19 +131,32 @@ dslist& dslist::operator= (const dslist& old) { template void dslist::push_front(const T& v) { - - - - - + Node* newp = new Node(v); + // if list is empty + if (!head_) { + head_ = newp; + tail_ = newp; + } else { + newp->next_ = head_; + head_->prev_ = newp; + head_ = newp; + } + ++size_; } template void dslist::pop_front() { - - - - + assert(head_ != NULL); // list must not be empty + Node* temp = head_; + head_ = head_->next_; + if (head_) { + head_->prev_ = NULL; + } else { + // list becomes empty + tail_ = NULL; + } + delete temp; + --size_; } template @@ -163,11 +176,17 @@ void dslist::push_back(const T& v) { template void dslist::pop_back() { - - - - - + assert(tail_ != NULL); // list must not be empty + Node* temp = tail_; + tail_ = tail_->prev_; + if (tail_) { + tail_->next_ = NULL; + } else { + // list becomes empty + head_ = NULL; + } + delete temp; + --size_; } // do these lists look the same (length & contents)? @@ -186,7 +205,7 @@ bool operator== (dslist& left, dslist& right) { template bool operator!= (dslist& left, dslist& right){ return !(left==right); } - template +template typename dslist::iterator dslist::erase(iterator itr) { assert (size_ > 0); --size_; @@ -256,13 +275,15 @@ void dslist::copy_list(const dslist& old) { template void dslist::destroy_list() { - - - - - - - + Node* current = head_; + while (current) { + Node* next = current->next_; + delete current; + current = next; + } + head_ = NULL; + tail_ = NULL; + size_ = 0; } #endif