实现常见的链表操作,插入头部,尾部,某数据之后或之前,修改,删除,排序,存储到文件,从文件构建链表,判空,是否存在,清空,排序, 重载运算符:下标,+(求和),-(求差),<<(输出插入符),>>(输入插入符) 实现常规链表类和模板链表类,并在main中测试
时间: 2023-05-21 17:04:59 浏览: 95
好的,以下是我的回答:
实现常见的链表操作:
首先,我们需要定义一个链表节点的数据结构,包含一个存储的值和一个指向下一个节点的指针。代码如下:
```c++
template <typename T>
struct Node {
T data;
Node<T>* next;
};
```
接下来,我们可以根据这个节点来实现链表的各种操作:
- 插入头部:从头部插入一个节点,让新节点成为头部即可。代码如下:
```c++
template <typename T>
void insert_head(Node<T>*& head, T val) {
Node<T>* new_node = new Node<T>{val, head};
head = new_node;
}
```
- 插入尾部:从尾部插入一个节点,找到链表的最后一个节点,让其指向新节点即可。代码如下:
```c++
template <typename T>
void insert_tail(Node<T>*& head, T val) {
Node<T>* new_node = new Node<T>{val, nullptr};
if (head == nullptr) {
head = new_node;
} else {
Node<T>* cur = head;
while (cur->next != nullptr) {
cur = cur->next;
}
cur->next = new_node;
}
}
```
- 插入某数据之后或之前:先找到目标数据所在的节点,然后插入新节点即可。代码如下:
```c++
template <typename T>
void insert_after(Node<T>*& head, T val, T new_val) {
Node<T>* cur = head;
while (cur != nullptr && cur->data != val) {
cur = cur->next;
}
if (cur != nullptr) {
Node<T>* new_node = new Node<T>{new_val, cur->next};
cur->next = new_node;
}
}
template <typename T>
void insert_before(Node<T>*& head, T val, T new_val) {
if (head != nullptr && head->data == val) {
// 如果要在头部之前插入新节点,直接调用 insert_head 即可
insert_head(head, new_val);
return;
}
Node<T>* cur = head;
while (cur != nullptr && cur->next != nullptr && cur->next->data != val) {
cur = cur->next;
}
if (cur != nullptr && cur->next != nullptr) {
Node<T>* new_node = new Node<T>{new_val, cur->next};
cur->next = new_node;
}
}
```
- 修改:找到目标节点,修改其数据即可。代码如下:
```c++
template <typename T>
void modify(Node<T>* head, T val, T new_val) {
Node<T>* cur = head;
while (cur != nullptr && cur->data != val) {
cur = cur->next;
}
if (cur != nullptr) {
cur->data = new_val;
}
}
```
- 删除:找到目标节点的前一个节点,修改其指针即可。代码如下:
```c++
template <typename T>
void remove(Node<T>*& head, T val) {
if (head != nullptr && head->data == val) {
// 如果要删除头节点,直接让 head 指向下一个节点即可
Node<T>* tmp = head;
head = head->next;
delete tmp;
return;
}
Node<T>* cur = head;
while (cur != nullptr && cur->next != nullptr && cur->next->data != val) {
cur = cur->next;
}
if (cur != nullptr && cur->next != nullptr) {
Node<T>* tmp = cur->next;
cur->next = tmp->next;
delete tmp;
}
}
```
- 排序:采用冒泡排序、快速排序、归并排序等算法均可,这里以冒泡排序为例。代码如下:
```c++
template <typename T>
void bubble_sort(Node<T>* head) {
int n = 0;
for (Node<T>* cur = head; cur != nullptr; cur = cur->next) {
n++;
}
for (int i = 0; i < n - 1; i++) {
bool swapped = false;
Node<T>* cur = head;
for (int j = 0; j < n - i - 1; j++) {
if (cur != nullptr && cur->next != nullptr && cur->data > cur->next->data) {
std::swap(cur->data, cur->next->data);
swapped = true;
}
cur = cur->next;
}
if (!swapped) {
break;
}
}
}
```
- 存储到文件和从文件构建链表:可以使用 std::ofstream 和 std::ifstream 来实现。代码如下:
```c++
template <typename T>
void save_to_file(Node<T>* head, const std::string& filename) {
std::ofstream fout(filename);
for (Node<T>* cur = head; cur != nullptr; cur = cur->next) {
fout << cur->data << " ";
}
fout.close();
}
template <typename T>
Node<T>* load_from_file(const std::string& filename) {
Node<T>* head = nullptr;
std::ifstream fin(filename);
T val;
while (fin >> val) {
insert_tail(head, val);
}
fin.close();
return head;
}
```
- 判空、是否存在、清空:代码如下:
```c++
template <typename T>
bool is_empty(Node<T>* head) {
return head == nullptr;
}
template <typename T>
bool exists(Node<T>* head, T val) {
Node<T>* cur = head;
while (cur != nullptr) {
if (cur->data == val) {
return true;
}
cur = cur->next;
}
return false;
}
template <typename T>
void clear(Node<T>*& head) {
while (head != nullptr) {
Node<T>* tmp = head;
head = head->next;
delete tmp;
}
}
```
- 重载运算符:下标、求和、求差、输出插入符、输入插入符。代码如下:
```c++
template <typename T>
class LinkedList {
public:
// ...
T& operator[](int index) {
Node<T>* cur = head;
for (int i = 0; i < index; i++) {
cur = cur->next;
}
return cur->data;
}
T sum() const {
T result = 0;
for (Node<T>* cur = head; cur != nullptr; cur = cur->next) {
result += cur->data;
}
return result;
}
T operator-(const LinkedList<T>& other) const {
T result = 0;
Node<T>* cur1 = head;
Node<T>* cur2 = other.head;
while (cur1 != nullptr && cur2 != nullptr) {
if (cur1->data > cur2->data) {
cur2 = cur2->next;
} else if (cur1->data < cur2->data) {
result += cur1->data;
cur1 = cur1->next;
} else {
cur1 = cur1->next;
cur2 = cur2->next;
}
}
while (cur1 != nullptr) {
result += cur1->data;
cur1 = cur1->next;
}
return result;
}
friend std::ostream& operator<<(std::ostream& os, const LinkedList<T>& list) {
for (Node<T>* cur = list.head; cur != nullptr; cur = cur->next) {
os << cur->data << " ";
}
return os;
}
friend std::istream& operator>>(std::istream& is, LinkedList<T>& list) {
T val;
is >> val;
list.insert_tail(val);
return is;
}
private:
Node<T>* head = nullptr;
};
```
完整代码可以参考下面的代码:
阅读全文