写在前面
1、基于2021级计算机类实验指导书
2、代码仅提供参考
3、如果代码不满足你的要求,请寻求其他的途径
运行环境
window11家庭版
CLion 2023.2.2
实验要求、源代码和运行结果
1.建立一个单链表,随机产生10个100以内的整数,并按要求完成:
(1)在屏幕上显示单链表中的10个整数;
(2)删除值为a的结点,若不存在a,则把a插入到表尾,显示更新后的单链表;
#include <iostream>
#include <ctime>
using namespace std;
// 单链表结点类
class Node {
public:
int data; // 存储整数值
Node* next; // 指向下一个结点的指针
// 构造函数
Node(int d) {
data = d;
next = nullptr;
}
};
// 单链表类
class LinkedList {
private:
Node* head; // 指向链表头结点的指针
public:
// 构造函数
LinkedList() {
head = nullptr;
}
// 插入结点到链表尾部
void insert(int value) {
// 创建新结点
Node* newNode = new Node(value);
// 如果链表为空,则将新结点设为头结点
if (head == nullptr) {
head = newNode;
} else {
// 遍历链表,找到最后一个结点
Node* current = head;
while (current->next != nullptr) {
current = current->next;
}
// 插入新结点到最后
current->next = newNode;
}
}
// 删除值为value的结点
void remove(int value) {
// 链表为空
if (head == nullptr) {
return;
}
// 头结点的值为value
if (head->data == value) {
Node* temp = head;
head = head->next;
delete temp;
return;
}
// 遍历链表,找到要删除结点的前一个结点
Node* current = head;
while (current->next != nullptr && current->next->data != value) {
current = current->next;
}
// 找到要删除的结点或遍历完整个链表
if (current->next != nullptr) {
Node* temp = current->next;
current->next = current->next->next;
delete temp;
} else {
// 如果没有找到值为value的结点,则将value插入到表尾
insert(value);
}
}
// 显示链表中的元素
void display() {
if (head == nullptr) {
cout << "链表为空" << endl;
} else {
Node* current = head;
while (current != nullptr) {
cout << current->data << " ";
current = current->next;
}
cout << endl;
}
}
};
int main() {
// 设置随机种子
srand(time(nullptr));
LinkedList list;
// 生成10个100以内的随机整数并插入链表
for (int i = 0; i < 10; i++) {
int randNum = rand() % 100 + 1; // 生成1至100的随机整数
list.insert(randNum);
}
cout << "单链表中的整数:"<<endl;
list.display();
int a;
cout << "请输入要删除的整数值:"<<endl;
cin >> a;
list.remove(a);
cout << "更新后的单链表:"<<endl;
list.display();
return 0;
}
2.设计一个程序,生成两个按值非递减有序排列的线性表LA和LB,再将LA和LB归并为一个新的线性表LC,且LC中的数据仍按值非递减有序排列,输出线性表LA、LB、LC。
#include <iostream>
using namespace std;
// 单链表结点类
class Node {
public:
int data; // 存储整数值
Node* next; // 指向下一个结点的指针
// 构造函数
Node(int d) {
data = d;
next = nullptr;
}
};
// 单链表类
class LinkedList {
private:
Node* head; // 指向链表头结点的指针
public:
// 构造函数
LinkedList() {
head = nullptr;
}
// 插入结点到链表尾部
void insert(int value) {
// 创建新结点
Node* newNode = new Node(value);
// 如果链表为空,则将新结点设为头结点
if (head == nullptr) {
head = newNode;
} else {
// 遍历链表,找到最后一个结点
Node* current = head;
while (current->next != nullptr) {
current = current->next;
}
// 插入新结点到最后
current->next = newNode;
}
}
// 显示链表中的元素
void display() {
if (head == nullptr) {
cout << "链表为空" << endl;
} else {
Node* current = head;
while (current != nullptr) {
cout << current->data << " ";
current = current->next;
}
cout << endl;
}
}
// 合并两个有序链表
LinkedList merge(LinkedList& listA, LinkedList& listB) {
LinkedList listC;
Node* pA = listA.head;
Node* pB = listB.head;
// 比较两个链表的结点值,将较小的值插入到新链表中,并移动对应链表的指针
while (pA != nullptr && pB != nullptr) {
if (pA->data <= pB->data) {
listC.insert(pA->data);
pA = pA->next;
} else {
listC.insert(pB->data);
pB = pB->next;
}
}
// 将剩余的结点插入到新链表中
while (pA != nullptr) {
listC.insert(pA->data);
pA = pA->next;
}
while (pB != nullptr) {
listC.insert(pB->data);
pB = pB->next;
}
return listC;
}
};
int main() {
LinkedList listA;
LinkedList listB;
// 生成有序链表A
for (int i = 1; i <= 5; i++) {
listA.insert(i * 2);
}
// 生成有序链表B
for (int i = 0; i < 5; i++) {
listB.insert(i * 3 + 1);
}
cout << "链表LA:"<<endl;
listA.display();
cout << "链表LB:"<<endl;
listB.display();
LinkedList listC = listC.merge(listA, listB);
cout << "链表LC:"<<endl;
listC.display();
return 0;
}