建立一个具有多个节点(输入以0结束)的单向链表,每个节点包含一个整形数值和一个指向本节点类型后向指针,对该链表按照升序进行排序(定义一个排序函数,void sort(Student*),采用冒泡算法),输出排序的结果。(注意输入、输出格式!)
#include <iostream>
using namespace std;
struct Student {
int number; //学号
Student* next; //指向下一个节点的指针
};
Student* head; //链表头指针
//创建链表函数
Student* Create() {
Student* pS; //定义新节点指针
Student* pEnd; //定义链表尾部节点指针
pS = new Student; //为新节点分配内存空间
cin >> pS->number; //输入新节点数据
head = nullptr; //链表为空
pEnd = pS; //将pS赋值给链表尾部节点指针pEnd
while (pS->number != 0) { //循环读入新节点数据,直到输入0
if (head == nullptr) //如果链表为空,将pS赋值给头指针head
head = pS;
else //否则将pS接在链表尾部
pEnd->next = pS;
pEnd = pS; //将pS赋值给链表尾部节点指针pEnd
pS = new Student; //为新节点分配内存空间
cin >> pS->number; //输入新节点数据
}
pEnd->next = nullptr; //链表尾部指向空
delete pS; //释放掉未使用的节点空间
return head; //返回链表头指针
}
//对链表进行排序
void sort(Student* head) {
if (head == nullptr) //如果链表为空,直接退出函数
return;
Student* p; //定义两个指针,p和q
Student* q;
for (p = head; p != nullptr; p = p->next) { //遍历链表节点
for (q = p->next; q != nullptr; q = q->next) { //遍历p后面的所有节点
if (p->number > q->number) { //如果p节点的学号大于q节点的学号
p->number ^= q->number;
q->number ^= p->number;
p->number ^= q->number;
}
}
}
}
//展示链表中的数据
void ShowList(Student* head) {
while (head) { //遍历链表节点,输出每个节点的学号
cout << head->number << " ";
head = head->next;
}
}
//主函数
int main() {
head = Create(); //创建链表,并将头指针赋值给全局变量head
sort(head); //对链表进行排序
ShowList(head); //展示链表中的数据
return 0;
}
链表类通常至少要包含两个成员变量:头指针和长度。其中头指针指向链表的第一个节点,长度是指链表中包含的节点数。 一个基本的链表类包含以下几个方法: 1. 构造函数和析构函数:用于创建一个空的链表并进行释放。 2. 插入(Insert)和删除(Delete):用于在链表中插入一个节点或删除一个节点。 3. 查找(Search)和修改(Modify):用于在链表中查找一个节点或修改一个节点的值。 4. 遍历(Traverse):用于遍历链表中的每个节点并对其进行操作(例如输出每个节点的值)。 5. 排序(Sort):用于对链表中的节点按照某种规则进行排序。
class LinkedList {
private:
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
Node* head;
int length;
public:
LinkedList() : head(nullptr), length(0) {}
~LinkedList() {
Node* p = head;
while (p != nullptr) {
Node* q = p;
p = p->next;
delete q;
}
}
void Insert(int val, int pos = 0) {}
void Delete(int pos) {}
bool Search(int val) {}
void Modify(int val, int pos) {}
void Traverse() {}
void Sort() {}
int Length() { return length; }
};
#include <iostream>
using namespace std;
struct Node {
string name;
int age;
Node* next;
};
class Human {
private:
Node* head;
int length;
public:
Human() {
head = nullptr;
length = 0;
}
~Human() {
Node* curr = head;
while (curr != nullptr) {
Node* temp = curr;
curr = curr->next;
delete temp;
}
}
// 插入节点
void Insert(string name, int age, int pos = 0) {
if (pos < 0 || pos > length) {
cout << "Invalid position." << endl;
return;
}
Node* newNode = new Node;
newNode->name = name;
newNode->age = age;
if (pos == 0) {
newNode->next = head;
head = newNode;
}
else {
Node* curr = head;
for (int i = 0; i < pos - 1; i++) {
curr = curr->next;
}
newNode->next = curr->next;
curr->next = newNode;
}
length++;
}
// 删除节点
void Delete(int pos) {
if (pos < 0 || pos >= length) {
cout << "Invalid position." << endl;
return;
}
Node* curr = head;
if (pos == 0) {
head = head->next;
}
else {
for (int i = 0; i < pos - 1; i++) {
curr = curr->next;
}
Node* temp = curr->next;
curr->next = temp->next;
delete temp;
}
length--;
}
// 查找节点
bool Search(string name) {
Node* curr = head;
while (curr != nullptr) {
if (curr->name == name) {
cout << "Found " << name << ", age: " << curr->age << endl;
return true;
}
curr = curr->next;
}
cout << name << " not found." << endl;
return false;
}
// 修改节点
void Modify(string name, int age) {
Node* curr = head;
while (curr != nullptr) {
if (curr->name == name) {
curr->age = age;
cout << "Updated " << name << ", age: " << age << endl;
return;
}
curr = curr->next;
}
cout << name << " not found." << endl;
}
// 遍历节点
void Traverse() {
cout << "There are " << length << " persons." << endl;
Node* curr = head;
while (curr != nullptr) {
cout << curr->name << ", age: " << curr->age << endl;
curr = curr->next;
}
}
// 排序节点
void Sort() {
if (head == nullptr || head->next == nullptr) return;
Node* curr = head;
while (curr != nullptr) {
Node* innerCurr = curr->next;
while (innerCurr != nullptr) {
if (curr->name > innerCurr->name) {
swap(curr->name, innerCurr->name);
swap(curr->age, innerCurr->age);
}
innerCurr = innerCurr->next;
}
curr = curr->next;
}
}
};