From acfb30d1a60b0e19aec2597a5ecd976fb057dda9 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Thu, 8 Feb 2024 23:30:23 -0500 Subject: [PATCH 01/38] adding hw4 discussion questions --- hws/discussions/yelp_businesses.txt | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 hws/discussions/yelp_businesses.txt diff --git a/hws/discussions/yelp_businesses.txt b/hws/discussions/yelp_businesses.txt new file mode 100644 index 0000000..3fb7b78 --- /dev/null +++ b/hws/discussions/yelp_businesses.txt @@ -0,0 +1,12 @@ +In-class discussion + +Discuss the following questions with students around you. + +1. The last command line argument (i.e., categories) may contain one or more words, how does our program know how many words are provided in the categories argument? + +2. In our program, how do we determine if a business falls into the chosen categories or not? +3. Is it necessary to store every field of each line into some data structure or some class object? +4. Is it neceesary to check the categories field of every line? + +5. Do we need call a sort function? The std::list container does have a sort member function. +6. Do we need to implement a destructor for the class we write? Is heap memory a good choice to store the linked list? From 0377b986cad016d51f83d53ec1c993aa2de70227 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Thu, 8 Feb 2024 23:31:00 -0500 Subject: [PATCH 02/38] updating lecture notes --- lectures/10_linked_lists/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lectures/10_linked_lists/README.md b/lectures/10_linked_lists/README.md index 42d7d76..614b48b 100644 --- a/lectures/10_linked_lists/README.md +++ b/lectures/10_linked_lists/README.md @@ -1,13 +1,13 @@ # Lecture 10 --- Vector Iterators & Linked Lists -- Keep an eye for an update on Student Excuse Absence Policy which would go in effect from HW4 + - Review of iterators - Building our own basic linked lists: – Stepping through a list From 32f0fbb02d17caa0650e18ee38b75469022bd90f Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Thu, 8 Feb 2024 23:34:47 -0500 Subject: [PATCH 03/38] adding one more question --- hws/discussions/yelp_businesses.txt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/hws/discussions/yelp_businesses.txt b/hws/discussions/yelp_businesses.txt index 3fb7b78..d10ba9f 100644 --- a/hws/discussions/yelp_businesses.txt +++ b/hws/discussions/yelp_businesses.txt @@ -5,8 +5,10 @@ Discuss the following questions with students around you. 1. The last command line argument (i.e., categories) may contain one or more words, how does our program know how many words are provided in the categories argument? 2. In our program, how do we determine if a business falls into the chosen categories or not? -3. Is it necessary to store every field of each line into some data structure or some class object? -4. Is it neceesary to check the categories field of every line? +3. Since the homework spec requires us to store the matching businesses in a linked list, is std::vector still a good choice for any part of the program? -5. Do we need call a sort function? The std::list container does have a sort member function. -6. Do we need to implement a destructor for the class we write? Is heap memory a good choice to store the linked list? +4. Is it necessary to store every field of each line into some data structure or some class object? +5. Is it neceesary to check the categories field of every line? + +6. Do we need call a sort function? The std::list container does have a sort member function. +7. Do we need to implement a destructor for the class we write? Is heap memory a good choice to store the linked list? From c22f815609647fc2c60d41523b2d074cec436e25 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Thu, 8 Feb 2024 23:38:08 -0500 Subject: [PATCH 04/38] adding one more discussion question --- hws/discussions/yelp_businesses.txt | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/hws/discussions/yelp_businesses.txt b/hws/discussions/yelp_businesses.txt index d10ba9f..ba6f42a 100644 --- a/hws/discussions/yelp_businesses.txt +++ b/hws/discussions/yelp_businesses.txt @@ -3,12 +3,13 @@ In-class discussion Discuss the following questions with students around you. 1. The last command line argument (i.e., categories) may contain one or more words, how does our program know how many words are provided in the categories argument? +2. Functions to get the business name, the categories, the rating, the price are all given, but how do we get the city? the zip code? and the review count? -2. In our program, how do we determine if a business falls into the chosen categories or not? -3. Since the homework spec requires us to store the matching businesses in a linked list, is std::vector still a good choice for any part of the program? +3. In our program, how do we determine if a business falls into the chosen categories or not? +4. Since the homework spec requires us to store the matching businesses in a linked list, is std::vector still a good choice for any part of the program? -4. Is it necessary to store every field of each line into some data structure or some class object? -5. Is it neceesary to check the categories field of every line? +5. Is it necessary to store every field of each line into some data structure or some class object? +6. Is it neceesary to check the categories field of every line? -6. Do we need call a sort function? The std::list container does have a sort member function. -7. Do we need to implement a destructor for the class we write? Is heap memory a good choice to store the linked list? +7. Do we need call a sort function? The std::list container does have a sort member function. +8. Do we need to implement a destructor for the class we write? Is heap memory a good choice to store the linked list? From e87b63884d1a8e1ef8d2092c3b3b423214c44f67 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Fri, 9 Feb 2024 13:36:07 -0500 Subject: [PATCH 05/38] adding iterator.cpp code --- lectures/10_linked_lists/iterator.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 lectures/10_linked_lists/iterator.cpp diff --git a/lectures/10_linked_lists/iterator.cpp b/lectures/10_linked_lists/iterator.cpp new file mode 100644 index 0000000..beeeb57 --- /dev/null +++ b/lectures/10_linked_lists/iterator.cpp @@ -0,0 +1,24 @@ +#include +#include +#include + +int main(){ + + std::list lst; + lst.push_back(150); + lst.push_back(250); + lst.push_back(350); + lst.push_back(450); + + std::list::iterator itr; + itr = lst.begin(); + ++itr; + *itr += 5; + + std::list::iterator itr2 = lst.begin(); + while(itr2 != lst.end()){ + std::cout << *itr2 << std::endl; + itr2++; + } + +} From 2c0f825d2051e143a60f9277868e0f9b3c9403a2 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Fri, 9 Feb 2024 13:38:10 -0500 Subject: [PATCH 06/38] no assert --- lectures/10_linked_lists/iterator.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/lectures/10_linked_lists/iterator.cpp b/lectures/10_linked_lists/iterator.cpp index beeeb57..afc6c58 100644 --- a/lectures/10_linked_lists/iterator.cpp +++ b/lectures/10_linked_lists/iterator.cpp @@ -1,6 +1,5 @@ #include #include -#include int main(){ From cb7ea6977f837fc7e85672280dbf57cd099ab652 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Fri, 9 Feb 2024 13:38:32 -0500 Subject: [PATCH 07/38] adding the iterator exercise code --- lectures/10_linked_lists/README.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lectures/10_linked_lists/README.md b/lectures/10_linked_lists/README.md index 614b48b..6bdb959 100644 --- a/lectures/10_linked_lists/README.md +++ b/lectures/10_linked_lists/README.md @@ -140,6 +140,36 @@ itr++; assert (*itr == 100); // might seem ok... but rewrite the code to avoid this! ``` +### Exercise + +What is the output of this program? + +#include +#include + +```cpp +int main(){ + + std::list lst; + lst.push_back(150); + lst.push_back(250); + lst.push_back(350); + lst.push_back(450); + + std::list::iterator itr; + itr = lst.begin(); + ++itr; + *itr += 5; + + std::list::iterator itr2 = lst.begin(); + while(itr2 != lst.end()){ + std::cout << *itr2 << std::endl; + itr2++; + } + +} +``` + ## 10.3 Working towards our own version of the STL list - Our discussion of how the STL list<T> is implemented has been intuitive: it is a “chain” of objects. From 8c639eb9370fe86297d2cbeaefd35429b992e269 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Fri, 9 Feb 2024 13:39:56 -0500 Subject: [PATCH 08/38] put the cpp code in the right place --- lectures/10_linked_lists/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lectures/10_linked_lists/README.md b/lectures/10_linked_lists/README.md index 6bdb959..2123870 100644 --- a/lectures/10_linked_lists/README.md +++ b/lectures/10_linked_lists/README.md @@ -144,10 +144,10 @@ assert (*itr == 100); // might seem ok... but rewrite the code to avoid this! What is the output of this program? +```cpp #include #include -```cpp int main(){ std::list lst; From ed6e8e2116d4fe7a3dca418e40c9af83cc8e3abf Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Fri, 9 Feb 2024 13:43:31 -0500 Subject: [PATCH 09/38] adding the list sort example --- lectures/09_iterators_linked_lists/README.md | 33 +++++++++++++++++++ .../09_iterators_linked_lists/list_sort.cpp | 27 +++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 lectures/09_iterators_linked_lists/list_sort.cpp diff --git a/lectures/09_iterators_linked_lists/README.md b/lectures/09_iterators_linked_lists/README.md index 7bd807b..03eaea9 100644 --- a/lectures/09_iterators_linked_lists/README.md +++ b/lectures/09_iterators_linked_lists/README.md @@ -118,6 +118,39 @@ compare function as STL vector. The value of any associated vector iterator must be re-assigned / re-initialized after these operations. +### List Sort Example + +The following [example](list_sort.cpp) demonstrate how to call the list sort function. + +```cpp +#include +#include + +int main() { + // Create a list of integers + std::list numbers = {5, 2, 9, 3, 7}; + + // Print the original list + std::cout << "Original list: "; + for (int num : numbers) { + std::cout << num << " "; + } + std::cout << std::endl; + + // Sort the list in ascending order + numbers.sort(); + + // Print the sorted list + std::cout << "Sorted list: "; + for (int num : numbers) { + std::cout << num << " "; + } + std::cout << std::endl; + + return 0; +} +``` + ## 9.7 Erase & Iterators STL lists and vectors each have a special member function called erase. In particular, given list of ints s, diff --git a/lectures/09_iterators_linked_lists/list_sort.cpp b/lectures/09_iterators_linked_lists/list_sort.cpp new file mode 100644 index 0000000..84507dc --- /dev/null +++ b/lectures/09_iterators_linked_lists/list_sort.cpp @@ -0,0 +1,27 @@ +#include +#include + +int main() { + // Create a list of integers + std::list numbers = {5, 2, 9, 3, 7}; + + // Print the original list + std::cout << "Original list: "; + for (int num : numbers) { + std::cout << num << " "; + } + std::cout << std::endl; + + // Sort the list in ascending order + numbers.sort(); + + // Print the sorted list + std::cout << "Sorted list: "; + for (int num : numbers) { + std::cout << num << " "; + } + std::cout << std::endl; + + return 0; +} + From 1e8f9d9a366418c92afb76fb86ef5e013203f04d Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Fri, 9 Feb 2024 13:44:08 -0500 Subject: [PATCH 10/38] syntax error --- lectures/09_iterators_linked_lists/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lectures/09_iterators_linked_lists/README.md b/lectures/09_iterators_linked_lists/README.md index 03eaea9..93cd44c 100644 --- a/lectures/09_iterators_linked_lists/README.md +++ b/lectures/09_iterators_linked_lists/README.md @@ -120,7 +120,7 @@ The value of any associated vector iterator must be re-assigned / re-initialized ### List Sort Example -The following [example](list_sort.cpp) demonstrate how to call the list sort function. +The following [example](list_sort.cpp) demonstrates how to call the list sort function. ```cpp #include From 1a91659934161d1ddadcdeeec62d9bcb43935334 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Fri, 9 Feb 2024 13:48:47 -0500 Subject: [PATCH 11/38] adding sorting examples --- lectures/09_iterators_linked_lists/README.md | 51 +++++++++++++++++++ .../list_sort_objects.cpp | 45 ++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 lectures/09_iterators_linked_lists/list_sort_objects.cpp diff --git a/lectures/09_iterators_linked_lists/README.md b/lectures/09_iterators_linked_lists/README.md index 93cd44c..97003fe 100644 --- a/lectures/09_iterators_linked_lists/README.md +++ b/lectures/09_iterators_linked_lists/README.md @@ -151,6 +151,57 @@ int main() { } ``` +### List Sort Example - Sorting Class Objects + +The following [example](list_sort_objects.cpp) demonstrates how to call the list sort function to sort a list which contains class objects. + +```cpp +#include +#include +#include + +// Define a simple class representing a person +class Person { +public: + std::string name; + int age; + + // Constructor + Person(std::string name, int age) : name(name), age(age) {} +}; + +// Custom comparison function to sort Person objects by age +bool compareByAge(const Person& p1, const Person& p2) { + return p1.age < p2.age; +} + +int main() { + // Create a list of Person objects + std::list people = { + {"Alice", 25}, + {"Bob", 30}, + {"Charlie", 20} + }; + + // Print the original list + std::cout << "Original list:" << std::endl; + for (const auto& person : people) { + std::cout << person.name << " (" << person.age << ")" << std::endl; + } + + // Sort the list of Person objects using the custom comparison function + people.sort(compareByAge); + + // Print the sorted list + std::cout << "\nSorted list:" << std::endl; + for (const auto& person : people) { + std::cout << person.name << " (" << person.age << ")" << std::endl; + } + + return 0; +} +``` + ## 9.7 Erase & Iterators STL lists and vectors each have a special member function called erase. In particular, given list of ints s, diff --git a/lectures/09_iterators_linked_lists/list_sort_objects.cpp b/lectures/09_iterators_linked_lists/list_sort_objects.cpp new file mode 100644 index 0000000..77ba4f3 --- /dev/null +++ b/lectures/09_iterators_linked_lists/list_sort_objects.cpp @@ -0,0 +1,45 @@ +#include +#include +#include + +// Define a simple class representing a person +class Person { +public: + std::string name; + int age; + + // Constructor + Person(std::string name, int age) : name(name), age(age) {} +}; + +// Custom comparison function to sort Person objects by age +bool compareByAge(const Person& p1, const Person& p2) { + return p1.age < p2.age; +} + +int main() { + // Create a list of Person objects + std::list people = { + {"Alice", 25}, + {"Bob", 30}, + {"Charlie", 20} + }; + + // Print the original list + std::cout << "Original list:" << std::endl; + for (const auto& person : people) { + std::cout << person.name << " (" << person.age << ")" << std::endl; + } + + // Sort the list of Person objects using the custom comparison function + people.sort(compareByAge); + + // Print the sorted list + std::cout << "\nSorted list:" << std::endl; + for (const auto& person : people) { + std::cout << person.name << " (" << person.age << ")" << std::endl; + } + + return 0; +} + From 560bf9fcb94ce6c2d4390a85b263f07cf074fdda Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Fri, 9 Feb 2024 13:51:11 -0500 Subject: [PATCH 12/38] adding overloading operator example --- lectures/09_iterators_linked_lists/README.md | 53 +++++++++++++++++++ .../list_sort_objects2.cpp | 45 ++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 lectures/09_iterators_linked_lists/list_sort_objects2.cpp diff --git a/lectures/09_iterators_linked_lists/README.md b/lectures/09_iterators_linked_lists/README.md index 97003fe..3d5f888 100644 --- a/lectures/09_iterators_linked_lists/README.md +++ b/lectures/09_iterators_linked_lists/README.md @@ -202,6 +202,59 @@ int main() { } ``` +### List Sort Example - Sorting Class Objects, via Overloading the Less Than Operator + +The following [example](list_sort_objects2.cpp) also demonstrates how to call the list sort function to sort a list which contains class objects. + + +```cpp +#include +#include +#include + +// Define a simple class representing a person +class Person { +public: + std::string name; + int age; + + // Constructor + Person(std::string name, int age) : name(name), age(age) {} + + // Overload the < operator for sorting + bool operator<(const Person& other) const { + // Compare based on age + return age < other.age; + } +}; + +int main() { + // Create a list of Person objects + std::list people = { + {"Alice", 25}, + {"Bob", 30}, + {"Charlie", 20} + }; + + // Print the original list + std::cout << "Original list:" << std::endl; + for (const auto& person : people) { + std::cout << person.name << " (" << person.age << ")" << std::endl; + } + + // Sort the list of Person objects + people.sort(); + + // Print the sorted list + std::cout << "\nSorted list:" << std::endl; + for (const auto& person : people) { + std::cout << person.name << " (" << person.age << ")" << std::endl; + } + + return 0; +} +``` + ## 9.7 Erase & Iterators STL lists and vectors each have a special member function called erase. In particular, given list of ints s, diff --git a/lectures/09_iterators_linked_lists/list_sort_objects2.cpp b/lectures/09_iterators_linked_lists/list_sort_objects2.cpp new file mode 100644 index 0000000..5a92644 --- /dev/null +++ b/lectures/09_iterators_linked_lists/list_sort_objects2.cpp @@ -0,0 +1,45 @@ +#include +#include +#include + +// Define a simple class representing a person +class Person { +public: + std::string name; + int age; + + // Constructor + Person(std::string name, int age) : name(name), age(age) {} + + // Overload the < operator for sorting + bool operator<(const Person& other) const { + // Compare based on age + return age < other.age; + } +}; + +int main() { + // Create a list of Person objects + std::list people = { + {"Alice", 25}, + {"Bob", 30}, + {"Charlie", 20} + }; + + // Print the original list + std::cout << "Original list:" << std::endl; + for (const auto& person : people) { + std::cout << person.name << " (" << person.age << ")" << std::endl; + } + + // Sort the list of Person objects + people.sort(); + + // Print the sorted list + std::cout << "\nSorted list:" << std::endl; + for (const auto& person : people) { + std::cout << person.name << " (" << person.age << ")" << std::endl; + } + + return 0; +} From 989133ee1dd945d0e530d16032b2d12be9bee1a7 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Fri, 9 Feb 2024 16:12:14 -0500 Subject: [PATCH 13/38] removing the auto keyword --- lectures/09_iterators_linked_lists/README.md | 26 ++++++++++++------- .../list_sort_objects2.cpp | 25 +++++++++++------- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/lectures/09_iterators_linked_lists/README.md b/lectures/09_iterators_linked_lists/README.md index 3d5f888..4b293e6 100644 --- a/lectures/09_iterators_linked_lists/README.md +++ b/lectures/09_iterators_linked_lists/README.md @@ -206,7 +206,6 @@ int main() { The following [example](list_sort_objects2.cpp) also demonstrates how to call the list sort function to sort a list which contains class objects. - ```cpp #include #include @@ -221,13 +220,14 @@ public: // Constructor Person(std::string name, int age) : name(name), age(age) {} - // Overload the < operator for sorting - bool operator<(const Person& other) const { - // Compare based on age - return age < other.age; - } }; +// Overload the < operator for sorting +bool operator<(const Person& p1, const Person& p2) { + // Compare based on age + return p1.age < p2.age; +} + int main() { // Create a list of Person objects std::list people = { @@ -238,8 +238,11 @@ int main() { // Print the original list std::cout << "Original list:" << std::endl; - for (const auto& person : people) { - std::cout << person.name << " (" << person.age << ")" << std::endl; + std::list::iterator itr = people.begin(); + while(itr != people.end()){ + // one way to use iterators + std::cout << (*itr).name << " (" << (*itr).age << ")" << std::endl; + ++itr; } // Sort the list of Person objects @@ -247,8 +250,11 @@ int main() { // Print the sorted list std::cout << "\nSorted list:" << std::endl; - for (const auto& person : people) { - std::cout << person.name << " (" << person.age << ")" << std::endl; + itr = people.begin(); + while(itr != people.end()){ + // another way to use iterators + std::cout << itr->name << " (" << itr->age << ")" << std::endl; + ++itr; } return 0; diff --git a/lectures/09_iterators_linked_lists/list_sort_objects2.cpp b/lectures/09_iterators_linked_lists/list_sort_objects2.cpp index 5a92644..038099a 100644 --- a/lectures/09_iterators_linked_lists/list_sort_objects2.cpp +++ b/lectures/09_iterators_linked_lists/list_sort_objects2.cpp @@ -11,13 +11,14 @@ public: // Constructor Person(std::string name, int age) : name(name), age(age) {} - // Overload the < operator for sorting - bool operator<(const Person& other) const { - // Compare based on age - return age < other.age; - } }; +// Overload the < operator for sorting +bool operator<(const Person& p1, const Person& p2) { + // Compare based on age + return p1.age < p2.age; +} + int main() { // Create a list of Person objects std::list people = { @@ -28,8 +29,11 @@ int main() { // Print the original list std::cout << "Original list:" << std::endl; - for (const auto& person : people) { - std::cout << person.name << " (" << person.age << ")" << std::endl; + std::list::iterator itr = people.begin(); + while(itr != people.end()){ + // one way to use iterators + std::cout << (*itr).name << " (" << (*itr).age << ")" << std::endl; + ++itr; } // Sort the list of Person objects @@ -37,8 +41,11 @@ int main() { // Print the sorted list std::cout << "\nSorted list:" << std::endl; - for (const auto& person : people) { - std::cout << person.name << " (" << person.age << ")" << std::endl; + itr = people.begin(); + while(itr != people.end()){ + // another way to use iterators + std::cout << itr->name << " (" << itr->age << ")" << std::endl; + ++itr; } return 0; From ba43fdc3dc83c6cec44313b86da3702139b68f61 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Fri, 9 Feb 2024 16:16:34 -0500 Subject: [PATCH 14/38] get rid of the keyword auto --- lectures/09_iterators_linked_lists/README.md | 12 ++++++++---- .../09_iterators_linked_lists/list_sort_objects.cpp | 13 ++++++++----- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/lectures/09_iterators_linked_lists/README.md b/lectures/09_iterators_linked_lists/README.md index 4b293e6..2755f3e 100644 --- a/lectures/09_iterators_linked_lists/README.md +++ b/lectures/09_iterators_linked_lists/README.md @@ -185,8 +185,10 @@ int main() { // Print the original list std::cout << "Original list:" << std::endl; - for (const auto& person : people) { - std::cout << person.name << " (" << person.age << ")" << std::endl; + std::list::iterator itr = people.begin(); + while(itr != people.end()){ + std::cout << (*itr).name << " (" << (*itr).age << ")" << std::endl; + ++itr; } // Sort the list of Person objects using the custom comparison function @@ -194,8 +196,10 @@ int main() { // Print the sorted list std::cout << "\nSorted list:" << std::endl; - for (const auto& person : people) { - std::cout << person.name << " (" << person.age << ")" << std::endl; + itr = people.begin(); + while(itr != people.end()){ + std::cout << (*itr).name << " (" << (*itr).age << ")" << std::endl; + ++itr; } return 0; diff --git a/lectures/09_iterators_linked_lists/list_sort_objects.cpp b/lectures/09_iterators_linked_lists/list_sort_objects.cpp index 77ba4f3..23739ee 100644 --- a/lectures/09_iterators_linked_lists/list_sort_objects.cpp +++ b/lectures/09_iterators_linked_lists/list_sort_objects.cpp @@ -27,8 +27,10 @@ int main() { // Print the original list std::cout << "Original list:" << std::endl; - for (const auto& person : people) { - std::cout << person.name << " (" << person.age << ")" << std::endl; + std::list::iterator itr = people.begin(); + while(itr != people.end()){ + std::cout << (*itr).name << " (" << (*itr).age << ")" << std::endl; + ++itr; } // Sort the list of Person objects using the custom comparison function @@ -36,10 +38,11 @@ int main() { // Print the sorted list std::cout << "\nSorted list:" << std::endl; - for (const auto& person : people) { - std::cout << person.name << " (" << person.age << ")" << std::endl; + itr = people.begin(); + while(itr != people.end()){ + std::cout << (*itr).name << " (" << (*itr).age << ")" << std::endl; + ++itr; } return 0; } - From 24556d0f58392835f7aefc0814391db1b1efc309 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Fri, 9 Feb 2024 16:52:02 -0500 Subject: [PATCH 15/38] about initializing the matrix --- hws/03_amazon_prime/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hws/03_amazon_prime/README.md b/hws/03_amazon_prime/README.md index a2bf907..695b37b 100644 --- a/hws/03_amazon_prime/README.md +++ b/hws/03_amazon_prime/README.md @@ -146,7 +146,7 @@ void process_one_line(int** matrix, int i, std::string& line){ } ``` -This function will help you to parse information from the input file, and update the matrix. This function takes three arguments, the first argument is a two-dimensional integer pointer, also known as the matrix pointer, which is expected to point to the beginning memory location of a two-dimensional array. This function assumes the caller allocates and reclaims memory for matrix. The second argument is the index i, indicating which row of the matrix will be updated. The third argument is the string line, which represents one line of the input file. +This function will help you to parse information from the input file, and update the matrix. This function takes three arguments, the first argument is a two-dimensional integer pointer, also known as the matrix pointer, which is expected to point to the beginning memory location of a two-dimensional array. This function assumes the caller allocates and reclaims memory for matrix - **it also assumes that every element of the matrix is initialized to 0**. The second argument is the index i, indicating which row of the matrix will be updated. The third argument is the string line, which represents one line of the input file. To understand what this function does, let's take the first line of the [movieRatings.txt](movieRatings.txt) as an example. From bce14b025d3fcf7d60aa158a472974c07529aafa Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Fri, 9 Feb 2024 16:59:02 -0500 Subject: [PATCH 16/38] clarifying requirements --- hws/05_online_dating/README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/hws/05_online_dating/README.md b/hws/05_online_dating/README.md index 6a0033b..e6b6801 100644 --- a/hws/05_online_dating/README.md +++ b/hws/05_online_dating/README.md @@ -192,14 +192,14 @@ This function takes four parameters, which are the latitude and longitude of two ``` ## Program Requirements & Submission Details -In this assignment, you are required to create and manipulate linked lists, **but you are not allowed to use the std::list library, and the concept of iterators should not appear anywhere in your code. In addition, you are NOT allowed to use std::vector, or any data structures we have not learned so far.** You are also required to overload the output operator. +This assignment has some unique requirements: In this assignment, you are required to create and manipulate linked lists using nodes, **you are not allowed to use the std::list library, and the concept of iterators should not appear anywhere in your code. The only class you can define is a class named Node. You are not allowed to define any other classes, or use the keyword struct anywhere in your code. In addition, you are NOT allowed to use std::vector, or any data structures we have not learned so far.** You are also required to overload the output operator. Use good coding style when you design and implement your program. Organize your program into functions: don’t put all the code in main! Be sure to read the [Homework Policies](https://www.cs.rpi.edu/academics/courses/fall23/csci1200/homework_policies.php) as you put the finishing touches on your solution. Be sure to make up new test cases to fully debug your program and don’t forget to comment your code! Use the provided template [README.txt](./README.txt) file for notes you want the grader to read. You must do this assignment on your own, as described in the [Collaboration Policy & Academic Integrity](https://www.cs.rpi.edu/academics/courses/fall23/csci1200/academic_integrity.php) page. If you did discuss the problem or error messages, etc. with anyone, please list their names in your README.txt file. -**Due Date**: 10/12/2023, Thursday, 23:59pm. +**Due Date**: 02/22/2024, Thursday, 22pm. ## Rubric @@ -222,6 +222,9 @@ You must do this assignment on your own, as described in the [Collaboration Poli - No credit (significantly incomplete implementation). (-5) - Uses std::vector, std::list, or data structures which have not been covered in this class. (-5) - Uses iterators in the code (okay for iterating through something other than the lists). (-5) + - Does not define the Node class. (-5) + - Defines another class other than the Node class. (-5) + - Uses the keyword struct. (-5) - OUTPUT OPERATOR OVERLOADING (2 pts) - Does not overload the output (<<) operator. (-2) From dbb5e7148da4afa3ff51331cc81b07fd41e0b05b Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Sun, 11 Feb 2024 20:17:35 -0500 Subject: [PATCH 17/38] not dynamic memory, just std::list --- hws/04_yelp_businesses/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hws/04_yelp_businesses/README.md b/hws/04_yelp_businesses/README.md index 8f86ff9..d9cdb75 100644 --- a/hws/04_yelp_businesses/README.md +++ b/hws/04_yelp_businesses/README.md @@ -247,7 +247,7 @@ You must do this assignment on your own, as described in the [Collaboration Poli - Overly cramped, excessive whitespace, or poor indentation. (-1) - Poor file organization: Puts more than one class in a file (okay for very small helper classes) (-1) - Poor choice of variable names: non-descriptive names (e.g. 'vec', 'str', 'var'), single-letter variable names (except single loop counter), etc. (-2) -- DATA REPRESENTATION (Must use dynamic memory for the implementation.) (7 pts) +- DATA REPRESENTATION (Must use std::list for the implementation.) (7 pts) - No credit (significantly incomplete implementation). (-7) - Does not use std::list to store the matchng businesses. (-7) - Uses data structures which have not been covered in this class. (-7) From 32117826a7f3a73b52e52a3650cdaac4c2909585 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Sun, 11 Feb 2024 23:39:02 -0500 Subject: [PATCH 18/38] indentation could be because of the given code, remove it --- hws/04_yelp_businesses/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hws/04_yelp_businesses/README.md b/hws/04_yelp_businesses/README.md index d9cdb75..11ce5af 100644 --- a/hws/04_yelp_businesses/README.md +++ b/hws/04_yelp_businesses/README.md @@ -244,7 +244,7 @@ You must do this assignment on your own, as described in the [Collaboration Poli - Function bodies containing more than one statement are placed in the .h file. (okay for templated classes) (-2) - Functions are not well documented or are poorly commented, in either the .h or the .cpp file. (-1) - At least one function is excessively long (i.e., more than 200 lines). (-1) - - Overly cramped, excessive whitespace, or poor indentation. (-1) + - Overly cramped. (-1) - Poor file organization: Puts more than one class in a file (okay for very small helper classes) (-1) - Poor choice of variable names: non-descriptive names (e.g. 'vec', 'str', 'var'), single-letter variable names (except single loop counter), etc. (-2) - DATA REPRESENTATION (Must use std::list for the implementation.) (7 pts) From f632017dc4fb1578e433247b76e575389b86b893 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Mon, 12 Feb 2024 17:38:58 -0500 Subject: [PATCH 19/38] remind to remember --- hws/04_yelp_businesses/README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/hws/04_yelp_businesses/README.md b/hws/04_yelp_businesses/README.md index 11ce5af..cf251da 100644 --- a/hws/04_yelp_businesses/README.md +++ b/hws/04_yelp_businesses/README.md @@ -98,7 +98,7 @@ std::string getName(std::string& line){ // to find the first name string. int key_start = line.find("name", start); int value_start = key_start + 7; - // remind them to use escape. + // remember to use escape. int value_end = line.find("\"", value_start); int len = value_end - value_start; // go from start to end, but exclude the character at end. @@ -120,7 +120,7 @@ std::string getCategories(std::string& line){ // to find the first categories string. int key_start = line.find("categories", start); int value_start = key_start + 13; - // remind them to use escape. + // remember to use escape. int value_end = line.find("\"", value_start); int len = value_end - value_start; // go from start to end, but exclude the character at end. @@ -142,7 +142,7 @@ double getRating(std::string& line){ // to find the first stars string. int key_start = line.find("stars", start); int value_start = key_start + 7; - // remind them to use escape. + // remember to use escape. int value_end = line.find("\"", value_start); // -1 here because this is not a string. int len = value_end - value_start - 1; @@ -170,7 +170,7 @@ int getPrice(std::string& line){ return -1; } int value_start = key_start + 25; - // remind them to use escape. + // remember to use escape. int value_end = line.find("\"", value_start); // -1 here because this is not a string. int len = value_end - value_start; From 1d19cc5021065886177328cb3865dd4b504017fb Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Tue, 13 Feb 2024 13:04:15 -0500 Subject: [PATCH 20/38] adding notes to hw5 --- hws/05_online_dating/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/hws/05_online_dating/README.md b/hws/05_online_dating/README.md index e6b6801..15abde3 100644 --- a/hws/05_online_dating/README.md +++ b/hws/05_online_dating/README.md @@ -1,3 +1,5 @@ +**This README is still incomplete! Don't work on this assignment now.** + # Homework 5 — Design and Implementation of a Simple Tinder In this assignment you will develop a simple online dating application called New York Date. Your program will mimic some of the features provided by Tinder. Please read the entire handout before starting to code the assignment. From 9cea2712bbb56a3fb2031c3ac9efdcc216c3d31c Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Tue, 13 Feb 2024 13:06:08 -0500 Subject: [PATCH 21/38] fixing the comments --- labs/06_lists_iterators/checkpoint3.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/labs/06_lists_iterators/checkpoint3.cpp b/labs/06_lists_iterators/checkpoint3.cpp index d0d12b7..95ed1bd 100644 --- a/labs/06_lists_iterators/checkpoint3.cpp +++ b/labs/06_lists_iterators/checkpoint3.cpp @@ -3,8 +3,7 @@ // Note: It's ok that all the member variables are public for this -// tiny class. We'll build up to a more robust and complete linked -// list implementation in lecture 11. +// tiny class. template class Node { public: @@ -39,7 +38,7 @@ void reverse(Node* &input) { int main() { - // manually create a linked list of notes with 4 elements + // manually create a linked list of nodes with 4 elements Node* my_list = new Node; my_list->value = 1; my_list->ptr = new Node; @@ -57,7 +56,7 @@ int main() { // Note: We are not deleting any of the Nodes we created... so this - // program has memory leaks! More on this in lecture 11. + // program has memory leaks! } From 62f30d5f3e676c4053e46d209b7261a173092c0e Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Tue, 13 Feb 2024 13:07:20 -0500 Subject: [PATCH 22/38] indentation --- lectures/11_list_implementation/README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lectures/11_list_implementation/README.md b/lectures/11_list_implementation/README.md index de1d9d7..4bc26c8 100644 --- a/lectures/11_list_implementation/README.md +++ b/lectures/11_list_implementation/README.md @@ -32,11 +32,11 @@ trying to use ->next and ->prev with STL list iterators.) ```cpp template class Node { public: -Node() : next_(NULL), prev_(NULL) {} -Node(const T& v) : value_(v), next_(NULL), prev_(NULL) {} -T value_; -Node* next_; -Node* prev_; + Node() : next_(NULL), prev_(NULL) {} + Node(const T& v) : value_(v), next_(NULL), prev_(NULL) {} + T value_; + Node* next_; + Node* prev_; }; ``` From ae6e937a3d6cc6a16505e06b14e777d1ebcd5939 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Tue, 13 Feb 2024 13:18:21 -0500 Subject: [PATCH 23/38] adding list implementation --- lectures/11_list_implementation/README.md | 7 ++- lectures/11_list_implementation/list.h | 3 +- lectures/11_list_implementation/list_main.cpp | 58 +------------------ 3 files changed, 9 insertions(+), 59 deletions(-) diff --git a/lectures/11_list_implementation/README.md b/lectures/11_list_implementation/README.md index 4bc26c8..c726edc 100644 --- a/lectures/11_list_implementation/README.md +++ b/lectures/11_list_implementation/README.md @@ -73,13 +73,18 @@ variable p. - Play this [animation](https://jidongxiao.github.io/CSCI1200-DataStructures/animations/doubly_lists/remove/index.html) to see how the remove works. -## 11.7 Special Cases of Remove +## 11.6 Special Cases of Remove - If p==head and p==tail, the single node in the list must be removed and both the head and tail pointer variables must be assigned the value NULL. - If p==head or p==tail, then the pointer adjustment code we just wrote needs to be specialized to removing the first or last node. +## 11.7 List Implementation + +- We have a list implementation [here](list.h). +- And we can test our list with this [program](list_main.cpp). + ## 11.8 Leetcode Exercises - [Leetcode problem 141: Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/). Solution: [p141_linkedlistcycle.cpp](../../leetcode/p141_linkedlistcycle.cpp) diff --git a/lectures/11_list_implementation/list.h b/lectures/11_list_implementation/list.h index 858b680..8aed48a 100644 --- a/lectures/11_list_implementation/list.h +++ b/lectures/11_list_implementation/list.h @@ -59,7 +59,6 @@ template class dslist { public: typedef list_iterator iterator; - typedef unsigned int size_type; // default constructor dslist(){ head = nullptr; @@ -102,7 +101,7 @@ class dslist { } } - size_type size(){ + unsigned int size(){ return size_; } diff --git a/lectures/11_list_implementation/list_main.cpp b/lectures/11_list_implementation/list_main.cpp index d61817e..ee9eff0 100644 --- a/lectures/11_list_implementation/list_main.cpp +++ b/lectures/11_list_implementation/list_main.cpp @@ -11,13 +11,8 @@ int main(){ teams.push_back("wpi"); teams.push_back("yale"); teams.push_back("brown"); - teams.push_back("cornell"); - teams.push_back("colgate"); - teams.push_back("miami"); - teams.push_back("colorado"); teams.push_back("harvard"); - // we can use a type alias defined inside a class even if there is no object of that class. The type alias becomes part of the class's scope and can be used anywhere in your code where the class's scope is visible. although no object of dslist is created in this code, the type alias size_type is still accessible because it is part of the class's scope. dslist::iterator itr; for(itr = teams.begin(); itr != teams.end(); itr++){ std::cout << *itr << std::endl; @@ -26,58 +21,9 @@ int main(){ teams.pop_back(); - for(itr = teams.begin(); itr != teams.end(); itr++){ + for(itr = teams.begin(); itr != teams.end(); itr++){ std::cout << *itr << std::endl; } - std::cout<<"==============="< ll; - ll.push_back(2.5); - ll.push_back(3.4); - int i = 0; - dslist::iterator itr2 = ll.begin(); - for (i=0, itr2 = ll.begin(); itr2 != ll.end(); itr2++, i++){ - std::cout << "ll[" << i << "] is " << *itr2 << std::endl; - } - - // copy a list - calls copy constructor. - dslist u(ll); - itr2 = u.begin(); - itr2++; - u.insert(itr2, 6.5); - u.insert(itr2, 4.8); - for (i=0, itr2 = u.begin(); itr2 != u.end(); itr2++, i++){ - std::cout << "u[" << i << "] is " << *itr2 << std::endl; - } - - // equivalent to list w(v), w is a copy of the elements in v. - // we use the const keyword in front of a variable definition to indicate that the value of the variable cannot be changed after it is initialized. - dslist w = ll; - for (i=0, itr2 = w.begin(); itr2 != w.end(); itr2++, i++){ - std::cout << "w[" << i << "] is " << *itr2 << std::endl; - } + return 0; } From 8202884d3469dc635b5f495be5fcaa695cfc77f8 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Tue, 13 Feb 2024 13:20:33 -0500 Subject: [PATCH 24/38] adding the comment --- lectures/11_list_implementation/list.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lectures/11_list_implementation/list.h b/lectures/11_list_implementation/list.h index 8aed48a..e1492c1 100644 --- a/lectures/11_list_implementation/list.h +++ b/lectures/11_list_implementation/list.h @@ -1,10 +1,11 @@ template class Node { + // this is an exception in this course + // where we make the member variables of this tiny class public. public: T value; Node* next; Node* prev; - private: }; // A "forward declaration" of this class is needed From 0b29ddbbe12ab08d84d163d7af4e6bbb809904e0 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Tue, 13 Feb 2024 13:22:11 -0500 Subject: [PATCH 25/38] remove the underscore --- lectures/11_list_implementation/list.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lectures/11_list_implementation/list.h b/lectures/11_list_implementation/list.h index e1492c1..cff9a75 100644 --- a/lectures/11_list_implementation/list.h +++ b/lectures/11_list_implementation/list.h @@ -50,7 +50,7 @@ class list_iterator { return (this->ptr != other.ptr); } - // the dslist class needs access to the private ptr_ member variable + // the dslist class needs access to the private ptr member variable friend class dslist; private: Node* ptr; From aa5bb20b1f7d26055d90476ef603b79f852cad59 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Tue, 13 Feb 2024 13:41:10 -0500 Subject: [PATCH 26/38] adding reverse iterator code --- labs/06_lists_iterators/README.md | 14 +++++++++----- labs/06_lists_iterators/reverse_iterator.cpp | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 labs/06_lists_iterators/reverse_iterator.cpp diff --git a/labs/06_lists_iterators/README.md b/labs/06_lists_iterators/README.md index 69597eb..d7d508d 100644 --- a/labs/06_lists_iterators/README.md +++ b/labs/06_lists_iterators/README.md @@ -24,14 +24,18 @@ properties clear: ```cpp std::list a; unsigned int i; -for ( i=1; i<10; ++i ) a.push_back( i*i ); +for ( i=1; i<10; ++i ){ + a.push_back( i*i ); +} std::list::reverse_iterator ri; -for( ri = a.rbegin(); ri != a.rend(); ++ri ) -cout << *ri << endl; +for( ri = a.rbegin(); ri != a.rend(); ++ri ){ + std::cout << *ri << std::endl; +} ``` -This code will print out the values 81, 64, 49, . . . , 1, in order, on separate lines. Observe the type for the -reverse iterator, the use of the functions rbegin and rend to provide iterators that delimit the bounds on +This code will print out the values 81, 64, 49, . . . , 1, in order, on separate lines. You can also compile and run this [example program](reverse_iterator.cpp). + +Observe the type for the reverse iterator, the use of the functions rbegin and rend to provide iterators that delimit the bounds on the reverse iterator, and the use of the ++ operator to take one step backwards through the list. It is very important to realize that rbegin and end are NOT the same thing! One of the challenges here will be determining when to stop (when you’ve reached the halfway point in the list). You may use an integer diff --git a/labs/06_lists_iterators/reverse_iterator.cpp b/labs/06_lists_iterators/reverse_iterator.cpp new file mode 100644 index 0000000..2a73cd6 --- /dev/null +++ b/labs/06_lists_iterators/reverse_iterator.cpp @@ -0,0 +1,18 @@ +#include +#include + +int main(){ + + std::list a; + unsigned int i; + for ( i=1; i<10; ++i ){ + a.push_back( i*i ); + } + + std::list::reverse_iterator ri; + for( ri = a.rbegin(); ri != a.rend(); ++ri ){ + std::cout << *ri << std::endl; + } + + return 0; +} From 21cf56d5249279d2593a10e3c40a8e2554ada517 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Tue, 13 Feb 2024 17:02:43 -0500 Subject: [PATCH 27/38] clarify the rubric --- hws/04_yelp_businesses/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hws/04_yelp_businesses/README.md b/hws/04_yelp_businesses/README.md index cf251da..179e34f 100644 --- a/hws/04_yelp_businesses/README.md +++ b/hws/04_yelp_businesses/README.md @@ -247,7 +247,7 @@ You must do this assignment on your own, as described in the [Collaboration Poli - Overly cramped. (-1) - Poor file organization: Puts more than one class in a file (okay for very small helper classes) (-1) - Poor choice of variable names: non-descriptive names (e.g. 'vec', 'str', 'var'), single-letter variable names (except single loop counter), etc. (-2) -- DATA REPRESENTATION (Must use std::list for the implementation.) (7 pts) +- DATA REPRESENTATION (7 pts) - No credit (significantly incomplete implementation). (-7) - Does not use std::list to store the matchng businesses. (-7) - Uses data structures which have not been covered in this class. (-7) From 8319c5cdf55ba0d0ad312a849dabb0302c6a1f40 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Tue, 13 Feb 2024 17:44:29 -0500 Subject: [PATCH 28/38] updating the rubric --- hws/05_online_dating/README.md | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/hws/05_online_dating/README.md b/hws/05_online_dating/README.md index 15abde3..50074df 100644 --- a/hws/05_online_dating/README.md +++ b/hws/05_online_dating/README.md @@ -7,8 +7,8 @@ In this assignment you will develop a simple online dating application called Ne ## Learning Objectives - Get familiar with a commonly used data structure - linked lists. -- Practice creating and manipulating linked lists without using the std::list library. -- Practice overloading the output operator (<<). +- Practice creating and manipulating linked lists without using the std::list library or creating a list class. + ## Background @@ -194,7 +194,7 @@ This function takes four parameters, which are the latitude and longitude of two ``` ## Program Requirements & Submission Details -This assignment has some unique requirements: In this assignment, you are required to create and manipulate linked lists using nodes, **you are not allowed to use the std::list library, and the concept of iterators should not appear anywhere in your code. The only class you can define is a class named Node. You are not allowed to define any other classes, or use the keyword struct anywhere in your code. In addition, you are NOT allowed to use std::vector, or any data structures we have not learned so far.** You are also required to overload the output operator. +This assignment has some unique requirements: In this assignment, you are required to create and manipulate linked lists using nodes, **you are not allowed to use the std::list library, and the concept of iterators should not appear anywhere in your code. Do not define a List class. You are now allowed to any data structures we have not learned so far.** Use good coding style when you design and implement your program. Organize your program into functions: don’t put all the code in main! Be sure to read the [Homework Policies](https://www.cs.rpi.edu/academics/courses/fall23/csci1200/homework_policies.php) as you put the finishing touches on your solution. Be sure to make up new test cases to fully debug your program and don’t forget @@ -206,9 +206,10 @@ You must do this assignment on your own, as described in the [Collaboration Poli ## Rubric 15 pts - - README.txt Completed (2 pts) + - README.txt Completed (3 pts) - One of name, collaborators, or hours not filled in. (-1) - Two or more of name, collaborators, or hours not filled in. (-2) + - No reflection. (-1) - OVERALL CLASS DECLARATION & IMPLEMENTATION AND CODING STYLE (Good class design, split into a .h and .cpp file. Functions > 1 line are in .cpp file. Organized class implementation and reasonable comments throughout. Correct use of const/const& and of class method const. ) (6 pts) - No credit (significantly incomplete implementation) (-6) - Putting almost everything in the main function. It's better to create separate functions for different tasks. (-2) @@ -220,14 +221,12 @@ You must do this assignment on your own, as described in the [Collaboration Poli - Poor file organization: Puts more than one class in a file (okay for very small helper classes) (-1) - Poor variable names. (-1) - Contains useless comments like commented-out code, terminal commands, or silly notes. (-1) - - DATA REPRESENTATION (Must create and use homemade linked lists for the implementation.) (5 pts) - - No credit (significantly incomplete implementation). (-5) - - Uses std::vector, std::list, or data structures which have not been covered in this class. (-5) - - Uses iterators in the code (okay for iterating through something other than the lists). (-5) - - Does not define the Node class. (-5) - - Defines another class other than the Node class. (-5) - - Uses the keyword struct. (-5) + - DATA REPRESENTATION (Must create and use homemade linked lists for the implementation.) (7 pts) + - No credit (significantly incomplete implementation). (-7) + - Uses std::list, or data structures which have not been covered in this class. (-7) + - Uses iterators in the code to visit list elements (okay for iterating through something other than the lists). (-7) + - Defines a list class. (-5) - - OUTPUT OPERATOR OVERLOADING (2 pts) + From f1ab2006b60c8abc4cec412e84cb084a3ee67aa5 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Tue, 13 Feb 2024 17:47:49 -0500 Subject: [PATCH 29/38] adding the reflection section --- hws/05_online_dating/README.txt | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/hws/05_online_dating/README.txt b/hws/05_online_dating/README.txt index 22e0b9f..3fe7cef 100644 --- a/hws/05_online_dating/README.txt +++ b/hws/05_online_dating/README.txt @@ -22,3 +22,16 @@ ESTIMATE OF # OF HOURS SPENT ON THIS ASSIGNMENT: < insert # hours > MISC. COMMENTS TO GRADER: (optional, please be concise!) + +## Reflection and Self Assessment + +Discuss the issues you encountered during development and testing. What +problems did you have? What did you have to research and learn on your +own? What kinds of errors did you get? How did you fix them? + +What parts of the assignment did you find challenging? Is there anything that +finally "clicked" for you in the process of working on this assignment? How well +did the development and testing process go for you? + +< insert reflection > + From 07a11d21234e53f527a7d5f2857f6d267efaeeb0 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Tue, 13 Feb 2024 19:02:07 -0500 Subject: [PATCH 30/38] explicitly say it's not ready --- hws/05_online_dating/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hws/05_online_dating/README.md b/hws/05_online_dating/README.md index 50074df..c3f5928 100644 --- a/hws/05_online_dating/README.md +++ b/hws/05_online_dating/README.md @@ -1,6 +1,6 @@ -**This README is still incomplete! Don't work on this assignment now.** +# **This README is still incomplete! Don't work on this assignment now.** -# Homework 5 — Design and Implementation of a Simple Tinder + In this assignment you will develop a simple online dating application called New York Date. Your program will mimic some of the features provided by Tinder. Please read the entire handout before starting to code the assignment. From a3754018f4750c81bed59e7b7b9f28e61359d233 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Tue, 13 Feb 2024 19:21:37 -0500 Subject: [PATCH 31/38] adding passing pointer by reference example and notes --- labs/06_lists_iterators/README.md | 2 ++ .../reference_to_a_pointer.cpp | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 labs/06_lists_iterators/reference_to_a_pointer.cpp diff --git a/labs/06_lists_iterators/README.md b/labs/06_lists_iterators/README.md index d7d508d..a8054e9 100644 --- a/labs/06_lists_iterators/README.md +++ b/labs/06_lists_iterators/README.md @@ -95,5 +95,7 @@ and lists with one or two values. Also add a test or two of a node chain with so If you have time, write 2 versions of this function, one version should be iterative (using a for or while loop) and one version should be recursive. +**Note**: this reverse function takes a pointer as its argument, but we are passing this pointer by reference, because we want to modify this pointer. To understand the concept of passing a pointer by reference, you are recommended to read and run this [example program](reference_to_a_pointer.cpp). + **To complete this checkpoint**, show a TA or mentor your diagram and your debugged function(s) to reverse a homemade singly-linked list. diff --git a/labs/06_lists_iterators/reference_to_a_pointer.cpp b/labs/06_lists_iterators/reference_to_a_pointer.cpp new file mode 100644 index 0000000..f7682e7 --- /dev/null +++ b/labs/06_lists_iterators/reference_to_a_pointer.cpp @@ -0,0 +1,28 @@ +/* This example demonstrates the usage of passing a pointer by reference. + * It is needed when you want to modify the pointer. + */ + +#include + +// function to modify the value of a pointer through reference +void modifyPointer(int* & ptr, int& newValue) { + ptr = &newValue; // assign the address of newValue to the pointer +} + +int main() { + int value = 42; + int* ptr = &value; + + // print the original value of the pointer + std::cout << "Original value of pointer: " << *ptr << std::endl; + + int newValue = 100; // new value to assign to the pointer + // pass the pointer by reference to the function, so that we can change the pointer + modifyPointer(ptr, newValue); + + // print the modified value of the pointer + std::cout << "Modified value of pointer: " << *ptr << std::endl; + + return 0; +} + From 0cfcf7c348d613d9857be017944f17d4ffe5d79d Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Tue, 13 Feb 2024 19:35:39 -0500 Subject: [PATCH 32/38] clarify the requirement --- hws/05_online_dating/README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/hws/05_online_dating/README.md b/hws/05_online_dating/README.md index c3f5928..86ccc40 100644 --- a/hws/05_online_dating/README.md +++ b/hws/05_online_dating/README.md @@ -194,12 +194,12 @@ This function takes four parameters, which are the latitude and longitude of two ``` ## Program Requirements & Submission Details -This assignment has some unique requirements: In this assignment, you are required to create and manipulate linked lists using nodes, **you are not allowed to use the std::list library, and the concept of iterators should not appear anywhere in your code. Do not define a List class. You are now allowed to any data structures we have not learned so far.** +This assignment has some unique requirements: In this assignment, you are required to create and manipulate linked lists using nodes, **you are not allowed to use the std::list library. You are not allowed to define a List class or an iterator class. You are not allowed to use any data structures we have not learned so far.** Use good coding style when you design and implement your program. Organize your program into functions: -don’t put all the code in main! Be sure to read the [Homework Policies](https://www.cs.rpi.edu/academics/courses/fall23/csci1200/homework_policies.php) as you put the finishing touches on your solution. Be sure to make up new test cases to fully debug your program and don’t forget +don’t put all the code in main! Be sure to read the [Homework Policies](https://www.cs.rpi.edu/academics/courses/spring24/csci1200/homework_policies.php) as you put the finishing touches on your solution. Be sure to make up new test cases to fully debug your program and don’t forget to comment your code! Use the provided template [README.txt](./README.txt) file for notes you want the grader to read. -You must do this assignment on your own, as described in the [Collaboration Policy & Academic Integrity](https://www.cs.rpi.edu/academics/courses/fall23/csci1200/academic_integrity.php) page. If you did discuss the problem or error messages, etc. with anyone, please list their names in your README.txt file. +You must do this assignment on your own, as described in the [Collaboration Policy & Academic Integrity](https://www.cs.rpi.edu/academics/courses/spring24/csci1200/academic_integrity.php) page. If you did discuss the problem or error messages, etc. with anyone, please list their names in your README.txt file. **Due Date**: 02/22/2024, Thursday, 22pm. @@ -224,8 +224,8 @@ You must do this assignment on your own, as described in the [Collaboration Poli - DATA REPRESENTATION (Must create and use homemade linked lists for the implementation.) (7 pts) - No credit (significantly incomplete implementation). (-7) - Uses std::list, or data structures which have not been covered in this class. (-7) - - Uses iterators in the code to visit list elements (okay for iterating through something other than the lists). (-7) - Defines a list class. (-5) + - Defines an iterator class. (-5) +5. block someone. +6. delete account. More details about each of these four commands are given below. For all four commands, *users.txt* represents the input file, and *output.txt* represents the output file, please do not hardcode the file names into your program, as the actual input/output file may have a different name. You should just use the *argv[]* array to retrieve the name of the input and the output file. Also note that all of our output files have an empty line at the very end, and the Submitty autograder sometimes will display that as two empty lines. But this empty line is not displayed if you open the file from Github in the browser, because GitHub automatically trims trailing whitespace and empty lines at the end of files when displaying them in the browser. @@ -142,7 +143,19 @@ See [user_405-855-9725_unmatch_595-383-2432.txt](user_405-855-9725_unmatch_595-3 - [user_741-273-7248_unmatch_621-883-6784.txt](user_741-273-7248_unmatch_621-883-6784.txt): when user has only one match. - [user_621-883-6784_unmatch_741-273-7248.txt](user_621-883-6784_unmatch_741-273-7248.txt): when the other user has only one match. - + +your program should print all users' information into the output file. This output file should be different from the original input file in two aspects: First, in your output file, this user should be excluded; Second, in your output file, this user's phone number should not appear on any user's like list. In summary, anything about this user should be erased from output file. ### Output File Order From f0627313c1e995a3acbf335eb8d4da3ff8efc065 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Thu, 15 Feb 2024 03:02:54 -0500 Subject: [PATCH 35/38] more specific about iterators --- hws/05_online_dating/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hws/05_online_dating/README.md b/hws/05_online_dating/README.md index e04c9e0..c6bd8a9 100644 --- a/hws/05_online_dating/README.md +++ b/hws/05_online_dating/README.md @@ -238,7 +238,7 @@ You must do this assignment on your own, as described in the [Collaboration Poli - No credit (significantly incomplete implementation). (-7) - Uses std::list, or data structures which have not been covered in this class. (-7) - Defines/Uses a list class. (-5) - - Defines/Uses an iterator class. (-5) + - Defines/Uses an iterator class (okay to use iterators to iterate through other containers such as vectors). (-5) More details about each of these four commands are given below. For all four commands, *users.txt* represents the input file, and *output.txt* represents the output file, please do not hardcode the file names into your program, as the actual input/output file may have a different name. You should just use the *argv[]* array to retrieve the name of the input and the output file. Also note that all of our output files have an empty line at the very end, and the Submitty autograder sometimes will display that as two empty lines. But this empty line is not displayed if you open the file from Github in the browser, because GitHub automatically trims trailing whitespace and empty lines at the end of files when displaying them in the browser. @@ -155,7 +155,7 @@ nydate.exe users.txt output.txt phoneNumber block phoneNumberOther your program should first print all profiles shown to this user - clearly this other user's profile should not be included; and then print all profiles shown to this other user - similarly, the current user's profile should not be included. -### Delete Account + ### Output File Order From f543dd563cc9a27035d0b0fe67d7dc7831a9b8b7 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Thu, 15 Feb 2024 12:32:56 -0500 Subject: [PATCH 37/38] defining the block command --- hws/05_online_dating/README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/hws/05_online_dating/README.md b/hws/05_online_dating/README.md index 713d620..4eef37b 100644 --- a/hws/05_online_dating/README.md +++ b/hws/05_online_dating/README.md @@ -153,7 +153,12 @@ When the user (here, *phoneNumber* is this user's phone number and *phoneNumberO nydate.exe users.txt output.txt phoneNumber block phoneNumberOther ``` -your program should first print all profiles shown to this user - clearly this other user's profile should not be included; and then print all profiles shown to this other user - similarly, the current user's profile should not be included. +your program should + +- First, print all profiles shown to this user - clearly this other user's profile should not be included; Here the term "all profiles" means all profiles which match with this user's preference on age, gender, and distance. +- Second, print all profiles shown to this other user - similarly, the current user's profile should not be included. Here the term "all profiles" means all profiles which match with this other user's preference on age, gender, and distance. + +All these profiles should be printed into the output file. +# Homework 5 — Design and Implementation of a Simple Tinder In this assignment you will develop a simple online dating application called New York Date. Your program will mimic some of the features provided by Tinder. Please read the entire handout before starting to code the assignment. @@ -69,7 +67,7 @@ Your program will support these commands: 5. block someone. -More details about each of these four commands are given below. For all four commands, *users.txt* represents the input file, and *output.txt* represents the output file, please do not hardcode the file names into your program, as the actual input/output file may have a different name. You should just use the *argv[]* array to retrieve the name of the input and the output file. Also note that all of our output files have an empty line at the very end, and the Submitty autograder sometimes will display that as two empty lines. But this empty line is not displayed if you open the file from Github in the browser, because GitHub automatically trims trailing whitespace and empty lines at the end of files when displaying them in the browser. +More details about each of these four commands are given below. For all four commands, *users.txt* represents the input file, and *output.txt* represents the output file, please do not hardcode the file names into your program, as the actual input/output file may have a different name. You should just use the *argv[]* array to retrieve the name of the input and the output file. Also note that all the expected output files contain an empty line at the end of file, to match with that, you just need to make sure to use *std::endl;* (or just *endl;* if you don't use *std::*) when printing the last line of a file. **Note**: for all the commands, you can assume the phone numbers (used in the commands) are valid and are corresponding to an existing account. @@ -214,8 +212,34 @@ This function takes four parameters, which are the latitude and longitude of two ``` ## Program Requirements & Submission Details + This assignment has some unique requirements: In this assignment, you are required to create and manipulate linked lists using nodes, **you are not allowed to use the std::list library. You are not allowed to define a List class or an iterator class. You are not allowed to use any data structures we have not learned so far.** +There is no requirement on whether your linked lists should be singly-linked lists, or doubly-linked lists. It is your design choice, but you must store all the users in a linked list, where each node of the linked list represents one user. For example, you can define your node like this if you decide to use doubly-linked lists: + +```cpp +class User { +public: + // add other fields as needed. + User* next; + User* prev; + +}; +``` + +or this, if you decide to use singly-linked lists: + +```cpp +class User { +public: + // add other fields as needed. + User* next; + +}; +``` + +**Note**: for this homework, it is okay to define member variables as public variables, and this is due to the unique nature of linked lists. + Use good coding style when you design and implement your program. Organize your program into functions: don’t put all the code in main! Be sure to read the [Homework Policies](https://www.cs.rpi.edu/academics/courses/spring24/csci1200/homework_policies.php) as you put the finishing touches on your solution. Be sure to make up new test cases to fully debug your program and don’t forget to comment your code! Use the provided template [README.txt](./README.txt) file for notes you want the grader to read. @@ -245,6 +269,7 @@ You must do this assignment on your own, as described in the [Collaboration Poli - Uses std::list, or data structures which have not been covered in this class. (-7) - Defines/Uses a list class. (-5) - Defines/Uses an iterator class (okay to use iterators to iterate through other containers such as vectors). (-5) + - Does not use homemade linked lists (which consists of chain of nodes) to store all the users. (-5)