Merge branch 'master' of https://github.com/jidongxiao/CSCI1200-DataStructures
This commit is contained in:
@@ -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.
|
||||
|
||||
|
||||
@@ -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;
|
||||
@@ -244,10 +244,10 @@ 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 dynamic memory 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)
|
||||
|
||||
@@ -5,8 +5,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.
|
||||
<!--- Practice overloading the output operator (<<).-->
|
||||
|
||||
## Background
|
||||
|
||||
@@ -58,15 +58,16 @@ These two images will give you a better understanding on some of the above field
|
||||
|
||||
## Specification
|
||||
|
||||
Your program will support four commands.
|
||||
Your program will support these commands:
|
||||
|
||||
1. show profiles match with a user's preference.
|
||||
2. show all matches to a user.
|
||||
3. show all users who swiped right on this user - this feature is only available to premium users.
|
||||
4. unmatch someone.
|
||||
<!--5. delete account.-->
|
||||
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.
|
||||
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.
|
||||
|
||||
@@ -140,6 +141,23 @@ 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.
|
||||
|
||||
### Block Someone
|
||||
|
||||
Tinder allows users to block other users. Blocking someone on Tinder means that you are preventing that person from seeing your profile and contacting you on the platform. It's a more severe action compared to unmatching.
|
||||
|
||||
When the user (here, *phoneNumber* is this user's phone number and *phoneNumberOther* represents the other user whom this user wants to block) runs this command:
|
||||
|
||||
```console
|
||||
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; 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.
|
||||
|
||||
<!--### Delete Account
|
||||
|
||||
Users can delete their accounts.
|
||||
@@ -149,6 +167,8 @@ When the user (here, *phoneNumber* is this user's phone number) runs this comman
|
||||
```console
|
||||
nydate.exe input.txt output.txt phoneNumber delete
|
||||
```
|
||||
|
||||
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
|
||||
@@ -192,37 +212,65 @@ 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. 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/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**: 10/12/2023, Thursday, 23:59pm.
|
||||
**Due Date**: 02/22/2024, Thursday, 22pm.
|
||||
|
||||
## 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)
|
||||
- Function bodies containing more than one statement are placed in the .h file. (okay for templated classes) (-2)
|
||||
- Missing include guards in the .h file. (Or does not declare them correctly) (-1)
|
||||
- Functions are not well documented or are poorly commented, in either the .h or the .cpp file. (-1)
|
||||
- Improper uses or omissions of const and reference. (-1)
|
||||
- Overly cramped, excessive whitespace, or poor indentation. (-1)
|
||||
- At least one function is excessively long (i.e., more than 200 lines). (-1)
|
||||
- Overly cramped. (-1)
|
||||
- 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)
|
||||
- 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 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)
|
||||
- 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)
|
||||
<!--- Member variables are public. (-2)-->
|
||||
- OUTPUT OPERATOR OVERLOADING (2 pts)
|
||||
<!-- - OUTPUT OPERATOR OVERLOADING (2 pts)
|
||||
- Does not overload the output (<<) operator. (-2)
|
||||
- Incorrect syntax (wrong return type, wrong arguments). (-1)
|
||||
- Incorrect syntax (wrong return type, wrong arguments). (-1)-->
|
||||
|
||||
@@ -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 >
|
||||
|
||||
|
||||
15
hws/discussions/yelp_businesses.txt
Normal file
15
hws/discussions/yelp_businesses.txt
Normal file
@@ -0,0 +1,15 @@
|
||||
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?
|
||||
|
||||
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?
|
||||
|
||||
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?
|
||||
|
||||
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?
|
||||
@@ -24,14 +24,18 @@ properties clear:
|
||||
```cpp
|
||||
std::list<int> 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<int>::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
|
||||
@@ -91,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.
|
||||
|
||||
@@ -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 T>
|
||||
class Node {
|
||||
public:
|
||||
@@ -39,7 +38,7 @@ void reverse(Node<T>* &input) {
|
||||
|
||||
int main() {
|
||||
|
||||
// manually create a linked list of notes with 4 elements
|
||||
// manually create a linked list of nodes with 4 elements
|
||||
Node<int>* my_list = new Node<int>;
|
||||
my_list->value = 1;
|
||||
my_list->ptr = new Node<int>;
|
||||
@@ -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!
|
||||
|
||||
}
|
||||
|
||||
|
||||
28
labs/06_lists_iterators/reference_to_a_pointer.cpp
Normal file
28
labs/06_lists_iterators/reference_to_a_pointer.cpp
Normal file
@@ -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 <iostream>
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
18
labs/06_lists_iterators/reverse_iterator.cpp
Normal file
18
labs/06_lists_iterators/reverse_iterator.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
|
||||
int main(){
|
||||
|
||||
std::list<int> a;
|
||||
unsigned int i;
|
||||
for ( i=1; i<10; ++i ){
|
||||
a.push_back( i*i );
|
||||
}
|
||||
|
||||
std::list<int>::reverse_iterator ri;
|
||||
for( ri = a.rbegin(); ri != a.rend(); ++ri ){
|
||||
std::cout << *ri << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -118,6 +118,153 @@ 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) demonstrates how to call the list sort function.
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
|
||||
int main() {
|
||||
// Create a list of integers
|
||||
std::list<int> 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;
|
||||
}
|
||||
```
|
||||
|
||||
### 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 <iostream>
|
||||
#include <list>
|
||||
#include <string>
|
||||
|
||||
// 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<Person> people = {
|
||||
{"Alice", 25},
|
||||
{"Bob", 30},
|
||||
{"Charlie", 20}
|
||||
};
|
||||
|
||||
// Print the original list
|
||||
std::cout << "Original list:" << std::endl;
|
||||
std::list<Person>::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
|
||||
people.sort(compareByAge);
|
||||
|
||||
// Print the sorted list
|
||||
std::cout << "\nSorted list:" << std::endl;
|
||||
itr = people.begin();
|
||||
while(itr != people.end()){
|
||||
std::cout << (*itr).name << " (" << (*itr).age << ")" << std::endl;
|
||||
++itr;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
### 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 <iostream>
|
||||
#include <list>
|
||||
#include <string>
|
||||
|
||||
// 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& p1, const Person& p2) {
|
||||
// Compare based on age
|
||||
return p1.age < p2.age;
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Create a list of Person objects
|
||||
std::list<Person> people = {
|
||||
{"Alice", 25},
|
||||
{"Bob", 30},
|
||||
{"Charlie", 20}
|
||||
};
|
||||
|
||||
// Print the original list
|
||||
std::cout << "Original list:" << std::endl;
|
||||
std::list<Person>::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
|
||||
people.sort();
|
||||
|
||||
// Print the sorted list
|
||||
std::cout << "\nSorted list:" << 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;
|
||||
}
|
||||
```
|
||||
|
||||
## 9.7 Erase & Iterators
|
||||
|
||||
STL lists and vectors each have a special member function called erase. In particular, given list of ints s,
|
||||
|
||||
27
lectures/09_iterators_linked_lists/list_sort.cpp
Normal file
27
lectures/09_iterators_linked_lists/list_sort.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
|
||||
int main() {
|
||||
// Create a list of integers
|
||||
std::list<int> 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;
|
||||
}
|
||||
|
||||
48
lectures/09_iterators_linked_lists/list_sort_objects.cpp
Normal file
48
lectures/09_iterators_linked_lists/list_sort_objects.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <string>
|
||||
|
||||
// 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<Person> people = {
|
||||
{"Alice", 25},
|
||||
{"Bob", 30},
|
||||
{"Charlie", 20}
|
||||
};
|
||||
|
||||
// Print the original list
|
||||
std::cout << "Original list:" << std::endl;
|
||||
std::list<Person>::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
|
||||
people.sort(compareByAge);
|
||||
|
||||
// Print the sorted list
|
||||
std::cout << "\nSorted list:" << std::endl;
|
||||
itr = people.begin();
|
||||
while(itr != people.end()){
|
||||
std::cout << (*itr).name << " (" << (*itr).age << ")" << std::endl;
|
||||
++itr;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
52
lectures/09_iterators_linked_lists/list_sort_objects2.cpp
Normal file
52
lectures/09_iterators_linked_lists/list_sort_objects2.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <string>
|
||||
|
||||
// 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& p1, const Person& p2) {
|
||||
// Compare based on age
|
||||
return p1.age < p2.age;
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Create a list of Person objects
|
||||
std::list<Person> people = {
|
||||
{"Alice", 25},
|
||||
{"Bob", 30},
|
||||
{"Charlie", 20}
|
||||
};
|
||||
|
||||
// Print the original list
|
||||
std::cout << "Original list:" << std::endl;
|
||||
std::list<Person>::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
|
||||
people.sort();
|
||||
|
||||
// Print the sorted list
|
||||
std::cout << "\nSorted list:" << 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;
|
||||
}
|
||||
@@ -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
|
||||
<!--- Keep an eye for an update on Student Excuse Absence Policy which would go in effect from HW4
|
||||
* Please remember incorporating the new rule may result in
|
||||
+ grading process gets delayed
|
||||
+ you will receive grades late
|
||||
+ will affect your grade inquiry time
|
||||
+ may affect when you receive the solutions or see the actual rubric
|
||||
+ if that HW was due before exam then be ready to receive feedback on that after exams
|
||||
* More detailed info soon
|
||||
* More detailed info soon-->
|
||||
- Review of iterators
|
||||
- Building our own basic linked lists:
|
||||
– Stepping through a list
|
||||
@@ -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?
|
||||
|
||||
```cpp
|
||||
#include <list>
|
||||
#include <iostream>
|
||||
|
||||
int main(){
|
||||
|
||||
std::list<int> lst;
|
||||
lst.push_back(150);
|
||||
lst.push_back(250);
|
||||
lst.push_back(350);
|
||||
lst.push_back(450);
|
||||
|
||||
std::list<int>::iterator itr;
|
||||
itr = lst.begin();
|
||||
++itr;
|
||||
*itr += 5;
|
||||
|
||||
std::list<int>::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.
|
||||
|
||||
23
lectures/10_linked_lists/iterator.cpp
Normal file
23
lectures/10_linked_lists/iterator.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#include <list>
|
||||
#include <iostream>
|
||||
|
||||
int main(){
|
||||
|
||||
std::list<int> lst;
|
||||
lst.push_back(150);
|
||||
lst.push_back(250);
|
||||
lst.push_back(350);
|
||||
lst.push_back(450);
|
||||
|
||||
std::list<int>::iterator itr;
|
||||
itr = lst.begin();
|
||||
++itr;
|
||||
*itr += 5;
|
||||
|
||||
std::list<int>::iterator itr2 = lst.begin();
|
||||
while(itr2 != lst.end()){
|
||||
std::cout << *itr2 << std::endl;
|
||||
itr2++;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -32,11 +32,11 @@ trying to use ->next and ->prev with STL list iterators.)
|
||||
```cpp
|
||||
template <class T> class Node {
|
||||
public:
|
||||
Node() : next_(NULL), prev_(NULL) {}
|
||||
Node(const T& v) : value_(v), next_(NULL), prev_(NULL) {}
|
||||
T value_;
|
||||
Node<T>* next_;
|
||||
Node<T>* prev_;
|
||||
Node() : next_(NULL), prev_(NULL) {}
|
||||
Node(const T& v) : value_(v), next_(NULL), prev_(NULL) {}
|
||||
T value_;
|
||||
Node<T>* next_;
|
||||
Node<T>* prev_;
|
||||
};
|
||||
```
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
template <class T>
|
||||
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
|
||||
@@ -49,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<T>;
|
||||
private:
|
||||
Node<T>* ptr;
|
||||
@@ -59,7 +60,6 @@ template <class T>
|
||||
class dslist {
|
||||
public:
|
||||
typedef list_iterator<T> iterator;
|
||||
typedef unsigned int size_type;
|
||||
// default constructor
|
||||
dslist(){
|
||||
head = nullptr;
|
||||
@@ -102,7 +102,7 @@ class dslist {
|
||||
}
|
||||
}
|
||||
|
||||
size_type size(){
|
||||
unsigned int size(){
|
||||
return size_;
|
||||
}
|
||||
|
||||
|
||||
@@ -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<std::string>::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<<"==============="<<std::endl;
|
||||
|
||||
itr = teams.begin();
|
||||
itr++;
|
||||
teams.erase(itr);
|
||||
|
||||
for(itr = teams.begin(); itr != teams.end(); itr++){
|
||||
std::cout << *itr << std::endl;
|
||||
}
|
||||
std::cout<<"==============="<<std::endl;
|
||||
|
||||
teams.push_front("harvard");
|
||||
teams.push_front("princeton");
|
||||
|
||||
for(itr = teams.begin(); itr != teams.end(); itr++){
|
||||
std::cout << *itr << std::endl;
|
||||
}
|
||||
std::cout<<"==============="<<std::endl;
|
||||
|
||||
teams.pop_front();
|
||||
for(itr = teams.begin(); itr != teams.end(); itr++){
|
||||
std::cout << *itr << std::endl;
|
||||
}
|
||||
std::cout<<"==============="<<std::endl;
|
||||
|
||||
dslist<double> ll;
|
||||
ll.push_back(2.5);
|
||||
ll.push_back(3.4);
|
||||
int i = 0;
|
||||
dslist<double>::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<double> 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<double> 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<double> w = ll;
|
||||
for (i=0, itr2 = w.begin(); itr2 != w.end(); itr2++, i++){
|
||||
std::cout << "w[" << i << "] is " << *itr2 << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user