add solution of lab 5

This commit is contained in:
2025-02-26 12:16:28 -05:00
parent 05c92a01ae
commit c20ce7c2c3
8 changed files with 146 additions and 29 deletions

2
.vscode/launch.json vendored
View File

@@ -51,7 +51,7 @@
"environment": [], "environment": [],
"MIMode": "gdb", "MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb", "miDebuggerPath": "/usr/bin/gdb",
"preLaunchTask": "C/C++: g++ build active file" "preLaunchTask": "C/C++: g++ build single active file"
}, },
{ {
"name": "nybusninesses", "name": "nybusninesses",

26
.vscode/tasks.json vendored
View File

@@ -25,6 +25,32 @@
"isDefault": true "isDefault": true
}, },
"detail": "Task generated by Debugger." "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" "version": "2.0.0"

View File

@@ -28,12 +28,8 @@ int main() {
// clear out the list // clear out the list
a.clear(); a.clear();
/* assert (a.size() == 0);
assert (a.size() == 0);
*/
/*
// simple tests of push_front, pop_front, and pop_back // simple tests of push_front, pop_front, and pop_back
a.push_front(5); a.push_front(5);
a.push_back(7); a.push_back(7);
@@ -66,7 +62,6 @@ int main() {
a.pop_front(); a.pop_front();
assert (a.size() == 0); assert (a.size() == 0);
assert (a.begin() == a.end()); assert (a.begin() == a.end());
*/
return 0; return 0;
} }

Binary file not shown.

View File

@@ -21,6 +21,36 @@ public:
NodeB* prev; NodeB* prev;
}; };
template <typename NodeType>
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 <typename NodeType>
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() { int main() {
// Part 1: test NodeA class. // Part 1: test NodeA class.
// Initialize an empty linked list, consisting of NodeA nodes. // Initialize an empty linked list, consisting of NodeA nodes.

Binary file not shown.

View File

@@ -15,6 +15,51 @@ public:
// this function returns a pointer pointing to the head node of the merged list. // this function returns a pointer pointing to the head node of the merged list.
template <class T> template <class T>
Node<T>* mergeLists(Node<T>* head_A, Node<T>* head_B) { Node<T>* mergeLists(Node<T>* head_A, Node<T>* 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<T>* head = nullptr;
Node<T>* 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() { int main() {

View File

@@ -131,19 +131,32 @@ dslist<T>& dslist<T>::operator= (const dslist<T>& old) {
template <class T> template <class T>
void dslist<T>::push_front(const T& v) { void dslist<T>::push_front(const T& v) {
Node<T>* newp = new Node<T>(v);
// if list is empty
if (!head_) {
head_ = newp;
tail_ = newp;
} else {
newp->next_ = head_;
head_->prev_ = newp;
head_ = newp;
}
++size_;
} }
template <class T> template <class T>
void dslist<T>::pop_front() { void dslist<T>::pop_front() {
assert(head_ != NULL); // list must not be empty
Node<T>* temp = head_;
head_ = head_->next_;
if (head_) {
head_->prev_ = NULL;
} else {
// list becomes empty
tail_ = NULL;
}
delete temp;
--size_;
} }
template <class T> template <class T>
@@ -163,11 +176,17 @@ void dslist<T>::push_back(const T& v) {
template <class T> template <class T>
void dslist<T>::pop_back() { void dslist<T>::pop_back() {
assert(tail_ != NULL); // list must not be empty
Node<T>* 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)? // do these lists look the same (length & contents)?
@@ -186,7 +205,7 @@ bool operator== (dslist<T>& left, dslist<T>& right) {
template <class T> template <class T>
bool operator!= (dslist<T>& left, dslist<T>& right){ return !(left==right); } bool operator!= (dslist<T>& left, dslist<T>& right){ return !(left==right); }
template <class T> template <class T>
typename dslist<T>::iterator dslist<T>::erase(iterator itr) { typename dslist<T>::iterator dslist<T>::erase(iterator itr) {
assert (size_ > 0); assert (size_ > 0);
--size_; --size_;
@@ -256,13 +275,15 @@ void dslist<T>::copy_list(const dslist<T>& old) {
template <class T> template <class T>
void dslist<T>::destroy_list() { void dslist<T>::destroy_list() {
Node<T>* current = head_;
while (current) {
Node<T>* next = current->next_;
delete current;
current = next;
}
head_ = NULL;
tail_ = NULL;
size_ = 0;
} }
#endif #endif