updating checkpoint 3
This commit is contained in:
@@ -36,7 +36,25 @@ Linked List of NodeB nodes: 1 -> 1.41421 -> 1.73205 -> 2 -> 2.23607 -> nullptr
|
|||||||
|
|
||||||
**To complete this checkpoint**, show a TA the implementation and the output of your program.
|
**To complete this checkpoint**, show a TA the implementation and the output of your program.
|
||||||
|
|
||||||
## Checkpoint 3: Debugging a Merge Sort program.
|
## Checkpoint 3: Merge Two Lists.
|
||||||
|
*estimate: 30-40 minutes*
|
||||||
|
|
||||||
|
Given two doubly-linked lists: linked list A and linked list B, and both linked lists are sorted. Data in linked list A is sorted in an ascending order. Data in linked list B is also sorted in an ascending order. Merge these two lists such that the data in the merged list is still sorted in an ascending order.
|
||||||
|
|
||||||
|
More specifically, complete the mergeLists() function in [checkpoint3.cpp](checkpoint3.cpp), such that the program prints the following output.
|
||||||
|
|
||||||
|
```console
|
||||||
|
$ g++ checkpoint3.cpp
|
||||||
|
$ ./a.out
|
||||||
|
1 3 5 7 9
|
||||||
|
2 4 6 8 10
|
||||||
|
1 2 3 4 5 6 7 8 9 10
|
||||||
|
10 9 8 7 6 5 4 3 2 1
|
||||||
|
```
|
||||||
|
|
||||||
|
**To complete this checkpoint**, explain to a TA your implementation and show the output of your program.
|
||||||
|
|
||||||
|
<!--## Checkpoint 3: Debugging a Merge Sort program.
|
||||||
*estimate: 30-40 minutes*
|
*estimate: 30-40 minutes*
|
||||||
|
|
||||||
We expect our program [checkpoint3.cpp](checkpoint3.cpp) to produce the following results when it is compiled and run.
|
We expect our program [checkpoint3.cpp](checkpoint3.cpp) to produce the following results when it is compiled and run.
|
||||||
@@ -54,4 +72,4 @@ Sorted Vector: 2 3 4 7 8
|
|||||||
|
|
||||||
But this program currently does not behave as expected. Troubleshoot this program, find the problems and fix them. You can use a debugger.
|
But this program currently does not behave as expected. Troubleshoot this program, find the problems and fix them. You can use a debugger.
|
||||||
|
|
||||||
**To complete this checkpoint**, explain to a TA the bugs you found, show a TA your fixes and run the program to show that your fixes are correct and the program now produces the expected results.
|
**To complete this checkpoint**, explain to a TA the bugs you found, show a TA your fixes and run the program to show that your fixes are correct and the program now produces the expected results.-->
|
||||||
|
|||||||
@@ -1,107 +1,93 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
// prototype of the sorting function
|
template <class T>
|
||||||
std::vector<int> sortVector(std::vector<int>& nums);
|
class Node {
|
||||||
|
public:
|
||||||
|
T value;
|
||||||
|
Node<T>* next;
|
||||||
|
Node<T>* prev;
|
||||||
|
|
||||||
// function to print the elements of a vector
|
// constructor
|
||||||
void printVector(const std::vector<int>& nums) {
|
Node(T val) : value(val), next(nullptr), prev(nullptr) {}
|
||||||
for (int num : nums) {
|
};
|
||||||
std::cout << num << " ";
|
|
||||||
}
|
// function to merge two sorted doubly linked lists
|
||||||
std::cout << std::endl;
|
template <class T>
|
||||||
|
Node<T>* mergeLists(Node<T>* head_A, Node<T>* head_B) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
// test case 1
|
// create 5 nodes and link them to form a linked list, this is linked list A.
|
||||||
std::vector<int> test1 = {5, 2, 9, 1, 5, 6};
|
Node<int>* head_A = new Node<int>(1);
|
||||||
std::vector<int> result1 = sortVector(test1);
|
Node<int>* second_A = new Node<int>(3);
|
||||||
std::cout << "Test Case 1: Original Vector: ";
|
Node<int>* third_A = new Node<int>(5);
|
||||||
printVector(test1);
|
Node<int>* fourth_A = new Node<int>(7);
|
||||||
std::cout << "Sorted Vector: ";
|
Node<int>* fifth_A = new Node<int>(9);
|
||||||
printVector(result1);
|
|
||||||
|
// link the nodes
|
||||||
|
head_A->next = second_A;
|
||||||
|
second_A->prev = head_A;
|
||||||
|
second_A->next = third_A;
|
||||||
|
third_A->prev = second_A;
|
||||||
|
third_A->next = fourth_A;
|
||||||
|
fourth_A->prev = third_A;
|
||||||
|
fourth_A->next = fifth_A;
|
||||||
|
fifth_A->prev = fourth_A;
|
||||||
|
|
||||||
|
// traverse linked list A and print the values
|
||||||
|
Node<int>* current = head_A;
|
||||||
|
while (current != nullptr) {
|
||||||
|
std::cout << current->value << " ";
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
// test case 2
|
// create 5 nodes and link them to form a linked list, this is linked list B.
|
||||||
std::vector<int> test2 = {3, 8, 2, 7, 4};
|
Node<int>* head_B = new Node<int>(2);
|
||||||
std::vector<int> result2 = sortVector(test2);
|
Node<int>* second_B = new Node<int>(4);
|
||||||
std::cout << "Test Case 2: Original Vector: ";
|
Node<int>* third_B = new Node<int>(6);
|
||||||
printVector(test2);
|
Node<int>* fourth_B = new Node<int>(8);
|
||||||
std::cout << "Sorted Vector: ";
|
Node<int>* fifth_B = new Node<int>(10);
|
||||||
printVector(result2);
|
|
||||||
|
// link the nodes
|
||||||
|
head_B->next = second_B;
|
||||||
|
second_B->prev = head_B;
|
||||||
|
second_B->next = third_B;
|
||||||
|
third_B->prev = second_B;
|
||||||
|
third_B->next = fourth_B;
|
||||||
|
fourth_B->prev = third_B;
|
||||||
|
fourth_B->next = fifth_B;
|
||||||
|
fifth_B->prev = fourth_B;
|
||||||
|
|
||||||
|
// traverse linked list B and print the values
|
||||||
|
current = head_B;
|
||||||
|
while (current != nullptr) {
|
||||||
|
std::cout << current->value << " ";
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
Node<int>* head_C;
|
||||||
|
Node<int>* tail_C;
|
||||||
|
head_C = mergeLists(head_A, head_B);
|
||||||
|
|
||||||
|
// traverse linked list C and print the values
|
||||||
|
current = head_C;
|
||||||
|
while (current != nullptr) {
|
||||||
|
std::cout << current->value << " ";
|
||||||
|
// keep tracking current and when current reaches nullptr, tail_C will be the tail node.
|
||||||
|
tail_C = current;
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
// traverse linked list C backwards and print the values
|
||||||
|
current = tail_C;
|
||||||
|
while (current != nullptr) {
|
||||||
|
std::cout << current->value << " ";
|
||||||
|
current = current->prev;
|
||||||
|
}
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// merge two vectors which are already sorted
|
|
||||||
std::vector<int>& mergeVectors(std::vector<int>& v1, std::vector<int>& v2){
|
|
||||||
int size1 = v1.size();
|
|
||||||
int size2 = v2.size();
|
|
||||||
std::vector<int> v(size1+size2, 0);
|
|
||||||
int index1 = 0;
|
|
||||||
int index2 = 0;
|
|
||||||
int index = 0;
|
|
||||||
// traverse v1 and v2 at the same time
|
|
||||||
while(index1<size1 && index2<size2){
|
|
||||||
// whoever is smaller goes to v
|
|
||||||
if(v1[index1]<v2[index2]){
|
|
||||||
v[index] = v1[index1];
|
|
||||||
index1 = index1 + 1;
|
|
||||||
}else{
|
|
||||||
v[index] = v2[index2];
|
|
||||||
index2 = index2 + 1;
|
|
||||||
}
|
|
||||||
index = index + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// if v1 is done, let's now deal with v2 left overs
|
|
||||||
if(index1>=size1){
|
|
||||||
while(index2<size2){
|
|
||||||
v[index] = v2[index2];
|
|
||||||
index++;
|
|
||||||
index2++;
|
|
||||||
}
|
|
||||||
}else{
|
|
||||||
// else means v2 is done. let's now deal with v1 left overs
|
|
||||||
while(index1<size1){
|
|
||||||
v[index] = v1[index1];
|
|
||||||
index++;
|
|
||||||
index1++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
// sort a vecotr of integers
|
|
||||||
std::vector<int> sortVector(std::vector<int>& nums) {
|
|
||||||
int size = nums.size();
|
|
||||||
// base case
|
|
||||||
if(size==1){
|
|
||||||
return nums;
|
|
||||||
}
|
|
||||||
// general case
|
|
||||||
// split the vector into two halves.
|
|
||||||
int mid = size/2;
|
|
||||||
|
|
||||||
// nums1 to store the first half, and nums2 to store the second half.
|
|
||||||
std::vector<int> nums1;
|
|
||||||
std::vector<int> nums2;
|
|
||||||
|
|
||||||
// copy the first half
|
|
||||||
for(int i=0;i<mid;i++){
|
|
||||||
nums1[i]=nums[i];
|
|
||||||
}
|
|
||||||
// copy the second half
|
|
||||||
for(int i=mid;i<size;i++){
|
|
||||||
nums2[i]=nums[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
// make the recursive calls
|
|
||||||
nums1 = sortVector(nums1);
|
|
||||||
nums2 = sortVector(nums2);
|
|
||||||
|
|
||||||
// now that the two vectors are already sorted, let's merge them
|
|
||||||
return mergeVectors(nums1, nums2);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user