源码:
#include <iostream>
using namespace std;
// 定义模板类节点
template <typename T>
class Node
{
public:
T data; // 存储数据
Node* next; // 指向下一个节点的指针
Node* prev; // 指向前一个节点的指针
Node(T val) : data(val), next(nullptr), prev(nullptr)
{
} // 构造函数
};
// 定义模板类双向链表
template <typename T>
class DoublyLinkedList
{
private:
Node<T>* head; // 头节点指针
Node<T>* tail; // 尾节点指针
public:
DoublyLinkedList() : head(nullptr), tail(nullptr)
{
} // 构造函数
~DoublyLinkedList()
{
Node<T>* current = head;
while (current)
{
Node<T>* next = current->next;
delete current;
current = next;
}
} // 析构函数,释放所有节点内存
// 插入节点到链表尾部
void insert(T val)
{
Node<T>* newNode = new Node<T>(val); // 创建新节点
if (!head)
{ // 如果链表为空
head = tail = newNode; // 新节点作为头节点和尾节点
}
else
{
tail->next = newNode; // 将新节点链接到尾节点
newNode->prev = tail;
tail = newNode; // 更新尾节点
}
}
// 修改指定位置的节点数据
void modify(int index, T val)
{
Node<T>* temp = head;
for (int i = 0; i < index; i++)
{ // 遍历到指定位置
if (!temp)
{
cout << "索引超出范围" << endl;
return;
}
temp = temp->next;
}
if (temp)
{
temp->data = val; // 修改数据
}
else
{
cout << "索引超出范围" << endl;
}
}
// 删除指定位置的节点
void remove(int index)
{
if (!head)
{
cout << "链表为空" << endl;
return;
}
if (index == 0)
{ // 删除头节点
Node<T>* temp = head;
head = head->next;
if (head)
{
head->prev = nullptr;
}
else
{
tail = nullptr; // 如果链表只有一个节点,删除后更新尾节点
}
delete temp;
return;
}
Node<T>* temp = head;
for (int i = 0; i < index; i++)
{ // 遍历到指定位置
if (!temp)
{
cout << "索引超出范围" << endl;
return;
}
temp = temp->next;
}
if (temp)
{
if (temp->next)
{
temp->next->prev = temp->prev;
}
else
{
tail = temp->prev; // 更新尾节点
}
temp->prev->next = temp->next;
delete temp;
}
else
{
cout << "索引超出范围" << endl;
}
}
// 查找指定值的节点,并返回所有匹配的索引
void findAll(T val, int*& indices, int& count)
{
Node<T>* temp = head;
int index = 0;
count = 0;
// 先遍历一遍链表以计算匹配值的个数
while (temp)
{
if (temp->data == val)
{
count++;
}
temp = temp->next;
}
if (count == 0)
{
indices = nullptr;
return;
}
// 分配数组存储匹配值的索引
indices = new int[count];
temp = head;
index = 0;
count = 0;
// 再次遍历链表以记录匹配值的索引
while (temp)
{
if (temp->data == val)
{
indices[count++] = index;
}
temp = temp->next;
index++;
}
}
// 显示链表中的所有节点
void display()
{
Node<T>* temp = head;
while (temp)
{
cout << temp->data << " "; // 打印每个节点的数据
temp = temp->next;
}
cout << endl;
}
};
int main()
{
DoublyLinkedList<int> list;
int n, value, index;
cout << "请输入要添加的数字个数:";
cin >> n;
cout << "请输入这" << n << "个数字:" << endl;
for (int i = 0; i < n; i++)
{
cin >> value;
list.insert(value); // 插入数字
}
cout << "当前链表:" << endl;
list.display(); // 显示链表
cout << "请输入要修改的节点是第几个和新值:" << endl;
cin >> index >> value;
list.modify(index - 1, value); // 修改节点
list.display(); // 显示链表
cout << "请输入要删除的节点是第几个:" << endl;
cin >> index;
list.remove(index - 1); // 删除节点
list.display(); // 显示链表
cout << "请输入要查找的值:" << endl;
cin >> value;
int* indices;
int count;
list.findAll(value, indices, count); // 查找所有匹配的节点
if (count > 0)
{
cout << "找到值 " << value << " 在第: ";
for (int i = 0; i < count; i++)
{
cout << indices[i]+1 << " ";
}
cout << endl;
delete[] indices; // 释放内存
}
else
{
cout << "值 " << value << " 不在链表中" << endl;
}
system("pause");
return 0;
}