我坚持如何在 C++ 中使用一个人的名字和姓氏在双向链表中制作排序算法
I'm stuck on how to make a sorting algorithm in a doubly linked list using a person's first name and last name in C++
所以我制作了一个双向链表,其中存储了一个人的名字、姓氏、地址和年龄,目前我正在为该列表制作一个排序算法。到目前为止,我已经设法创建了 3 个函数,一个将节点添加到列表中,一个从列表中删除一个节点,另一个打印列表。
这是我到目前为止的结构:
struct Node {
string First_Name;
string Last_Name;
string Address;
int age;
Node* next;
Node* prev;
} *first = 0, * last = 0;
addToList 函数:
void addToList()
{
string temp = "Yes";
string First_Name;
string Last_Name;
string Address;
int age;
Node* current = first;
while (temp == "Yes") {
cout << "Enter the persons first name: ";
cin >> First_Name;
cout << "Enter the persons last name: ";
cin >> Last_Name;
cout << "Enter the persons age: ";
cin >> age;
cout << "Enter the persons address: ";
cin >> Address;
cout << "Would you like to add another person? Yes or No";
cin >> temp;
current = new Node;
current->First_Name = First_Name;
current->Last_Name = Last_Name;
current->age = age;
current->Address = Address;
if (last) last->next = current;
else first = current;
current->prev = last;
current->next = 0;
last = current;
}
return;
}
打印列表:
void printList()
{
if (!first)
{
cout << "Nothing is present in the list." << endl;
return;
}
Node* current = first;
while (current)
{
cout << current->First_Name << " " << current->Last_Name << " " << current->age << " " << current->Address << endl;
current = current->next;
}
}
我的问题是,我怎样才能按字母顺序对列表进行排序,我以前从未进行过排序...
谢谢!!
要对双向链表使用自定义排序,重载 operator<
:
struct Person
{
std::string first;
std::string last;
std::string address;
unsigned int age;
bool operator<(const Person& p) const
{
bool is_less_than = false;
if (last == p.last)
{
is_less_than = first < p.first;
}
else
{
is_less_than = last < p.last;
}
return is_less_than;
}
};
现在您可以使用 std::list
,它会自动按姓氏排序,然后是第一位。而std::list
是一个双向链表。
比较Person
s:
Person a;
Person b;
//...
if (a < b)
{
std::cout << "Person A < Person B\n";
}
所以我制作了一个双向链表,其中存储了一个人的名字、姓氏、地址和年龄,目前我正在为该列表制作一个排序算法。到目前为止,我已经设法创建了 3 个函数,一个将节点添加到列表中,一个从列表中删除一个节点,另一个打印列表。 这是我到目前为止的结构:
struct Node {
string First_Name;
string Last_Name;
string Address;
int age;
Node* next;
Node* prev;
} *first = 0, * last = 0;
addToList 函数:
void addToList()
{
string temp = "Yes";
string First_Name;
string Last_Name;
string Address;
int age;
Node* current = first;
while (temp == "Yes") {
cout << "Enter the persons first name: ";
cin >> First_Name;
cout << "Enter the persons last name: ";
cin >> Last_Name;
cout << "Enter the persons age: ";
cin >> age;
cout << "Enter the persons address: ";
cin >> Address;
cout << "Would you like to add another person? Yes or No";
cin >> temp;
current = new Node;
current->First_Name = First_Name;
current->Last_Name = Last_Name;
current->age = age;
current->Address = Address;
if (last) last->next = current;
else first = current;
current->prev = last;
current->next = 0;
last = current;
}
return;
}
打印列表:
void printList()
{
if (!first)
{
cout << "Nothing is present in the list." << endl;
return;
}
Node* current = first;
while (current)
{
cout << current->First_Name << " " << current->Last_Name << " " << current->age << " " << current->Address << endl;
current = current->next;
}
}
我的问题是,我怎样才能按字母顺序对列表进行排序,我以前从未进行过排序... 谢谢!!
要对双向链表使用自定义排序,重载 operator<
:
struct Person
{
std::string first;
std::string last;
std::string address;
unsigned int age;
bool operator<(const Person& p) const
{
bool is_less_than = false;
if (last == p.last)
{
is_less_than = first < p.first;
}
else
{
is_less_than = last < p.last;
}
return is_less_than;
}
};
现在您可以使用 std::list
,它会自动按姓氏排序,然后是第一位。而std::list
是一个双向链表。
比较Person
s:
Person a;
Person b;
//...
if (a < b)
{
std::cout << "Person A < Person B\n";
}