add solution of lab 5
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
BIN
labs/list_implementation/checkpoint2
Executable file
BIN
labs/list_implementation/checkpoint2
Executable file
Binary file not shown.
@@ -21,6 +21,36 @@ public:
|
||||
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() {
|
||||
// Part 1: test NodeA class.
|
||||
// Initialize an empty linked list, consisting of NodeA nodes.
|
||||
|
||||
BIN
labs/list_implementation/checkpoint3
Executable file
BIN
labs/list_implementation/checkpoint3
Executable file
Binary file not shown.
@@ -15,6 +15,51 @@ public:
|
||||
// this function returns a pointer pointing to the head node of the merged list.
|
||||
template <class T>
|
||||
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() {
|
||||
|
||||
@@ -131,19 +131,32 @@ dslist<T>& dslist<T>::operator= (const dslist<T>& old) {
|
||||
|
||||
template <class T>
|
||||
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>
|
||||
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>
|
||||
@@ -163,11 +176,17 @@ void dslist<T>::push_back(const T& v) {
|
||||
|
||||
template <class T>
|
||||
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)?
|
||||
@@ -186,7 +205,7 @@ bool operator== (dslist<T>& left, dslist<T>& right) {
|
||||
|
||||
template <class T>
|
||||
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) {
|
||||
assert (size_ > 0);
|
||||
--size_;
|
||||
@@ -256,13 +275,15 @@ void dslist<T>::copy_list(const dslist<T>& old) {
|
||||
|
||||
template <class T>
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user