[C++] 纯文本查看 复制代码
#include <iostream>
using namespace std;
// 1. 单向链表实现
// 节点结构体
typedef struct SinglyNode {
int data;
SinglyNode* next;
} SinglyNode;
// 创建新节点
SinglyNode* createSinglyNode(int val) {
SinglyNode* node = new SinglyNode; // 使用new替代malloc
node->data = val;
node->next = nullptr;
return node;
}
// 初始化链表(返回头节点)
SinglyNode* initSinglyLinkedList() {
return nullptr; // 空链表头节点为nullptr
}
// 获取链表长度
int getSinglyListLength(SinglyNode* head) {
int length = 0;
SinglyNode* current = head;
while (current != nullptr) {
length++;
current = current->next;
}
return length;
}
// 判断链表是否为空
bool isSinglyListEmpty(SinglyNode* head) {
return head == nullptr;
}
// 在头部插入
SinglyNode* insertSinglyAtHead(SinglyNode* head, int val) {
SinglyNode* newNode = createSinglyNode(val);
newNode->next = head;
return newNode; // 新节点成为新的头节点
}
// 在尾部插入
SinglyNode* insertSinglyAtTail(SinglyNode* head, int val) {
SinglyNode* newNode = createSinglyNode(val);
if (isSinglyListEmpty(head)) {
return newNode; // 空链表时新节点成为头节点
}
SinglyNode* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = newNode;
return head;
}
// 在指定位置插入(pos从0开始)
SinglyNode* insertSinglyAtPosition(SinglyNode* head, int pos, int val) {
int length = getSinglyListLength(head);
if (pos < 0 || pos > length) {
cout << "位置无效" << endl;
return head;
}
if (pos == 0) {
return insertSinglyAtHead(head, val);
}
SinglyNode* newNode = createSinglyNode(val);
SinglyNode* current = head;
for (int i = 0; i < pos - 1; i++) {
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
return head;
}
// 删除头部节点
SinglyNode* deleteSinglyAtHead(SinglyNode* head) {
if (isSinglyListEmpty(head)) {
cout << "链表为空,无法删除" << endl;
return head;
}
SinglyNode* temp = head;
head = head->next;
delete temp; // 使用delete替代free
return head;
}
// 删除尾部节点
SinglyNode* deleteSinglyAtTail(SinglyNode* head) {
if (isSinglyListEmpty(head)) {
cout << "链表为空,无法删除" << endl;
return head;
}
// 只有一个节点的情况
if (head->next == nullptr) {
delete head; // 使用delete替代free
return nullptr;
}
SinglyNode* current = head;
while (current->next->next != nullptr) {
current = current->next;
}
delete current->next; // 使用delete替代free
current->next = nullptr;
return head;
}
// 删除指定值的节点
SinglyNode* deleteSinglyByValue(SinglyNode* head, int val) {
if (isSinglyListEmpty(head)) {
cout << "链表为空,无法删除" << endl;
return head;
}
// 头节点就是要删除的节点
if (head->data == val) {
return deleteSinglyAtHead(head);
}
SinglyNode* current = head;
while (current->next != nullptr && current->next->data != val) {
current = current->next;
}
if (current->next == nullptr) {
cout << "未找到值为 " << val << " 的节点" << endl;
return head;
}
SinglyNode* temp = current->next;
current->next = current->next->next;
delete temp; // 使用delete替代free
return head;
}
// 查找节点是否存在
bool searchSinglyNode(SinglyNode* head, int val) {
SinglyNode* current = head;
while (current != nullptr) {
if (current->data == val) {
return true;
}
current = current->next;
}
return false;
}
// 修改指定值为新值
bool modifySinglyNode(SinglyNode* head, int oldVal, int newVal) {
SinglyNode* current = head;
while (current != nullptr) {
if (current->data == oldVal) {
current->data = newVal;
return true;
}
current = current->next;
}
return false;
}
// 遍历链表
void traverseSinglyList(SinglyNode* head) {
if (isSinglyListEmpty(head)) {
cout << "链表为空" << endl;
return;
}
SinglyNode* current = head;
while (current != nullptr) {
cout << current->data << " -> ";
current = current->next;
}
cout << "nullptr" << endl;
}
// 清空链表
SinglyNode* clearSinglyList(SinglyNode* head) {
SinglyNode* current = head;
while (current != nullptr) {
SinglyNode* next = current->next;
delete current; // 使用delete替代free
current = next;
}
return nullptr; // 清空后头节点为nullptr
}
// 2. 双向链表实现
// 节点结构体
typedef struct DoublyNode {
int data;
DoublyNode* prev;
DoublyNode* next;
} DoublyNode;
// 创建新节点
DoublyNode* createDoublyNode(int val) {
DoublyNode* node = new DoublyNode; // 使用new替代malloc
node->data = val;
node->prev = nullptr;
node->next = nullptr;
return node;
}
// 初始化链表(返回头节点)
DoublyNode* initDoublyLinkedList() {
return nullptr;
}
// 获取链表长度
int getDoublyListLength(DoublyNode* head) {
int length = 0;
DoublyNode* current = head;
while (current != nullptr) {
length++;
current = current->next;
}
return length;
}
// 判断链表是否为空
bool isDoublyListEmpty(DoublyNode* head) {
return head == nullptr;
}
// 获取尾节点
DoublyNode* getDoublyListTail(DoublyNode* head) {
if (isDoublyListEmpty(head)) {
return nullptr;
}
DoublyNode* current = head;
while (current->next != nullptr) {
current = current->next;
}
return current;
}
// 在头部插入
DoublyNode* insertDoublyAtHead(DoublyNode* head, int val) {
DoublyNode* newNode = createDoublyNode(val);
if (isDoublyListEmpty(head)) {
return newNode;
}
newNode->next = head;
head->prev = newNode;
return newNode;
}
// 在尾部插入
DoublyNode* insertDoublyAtTail(DoublyNode* head, int val) {
DoublyNode* newNode = createDoublyNode(val);
if (isDoublyListEmpty(head)) {
return newNode;
}
DoublyNode* tail = getDoublyListTail(head);
tail->next = newNode;
newNode->prev = tail;
return head;
}
// 在指定位置插入(pos从0开始)
DoublyNode* insertDoublyAtPosition(DoublyNode* head, int pos, int val) {
int length = getDoublyListLength(head);
if (pos < 0 || pos > length) {
cout << "位置无效" << endl;
return head;
}
if (pos == 0) {
return insertDoublyAtHead(head, val);
}
if (pos == length) {
return insertDoublyAtTail(head, val);
}
DoublyNode* newNode = createDoublyNode(val);
DoublyNode* current = head;
// 找到要插入位置的节点
for (int i = 0; i < pos; i++) {
current = current->next;
}
newNode->prev = current->prev;
newNode->next = current;
current->prev->next = newNode;
current->prev = newNode;
return head;
}
// 删除头部节点
DoublyNode* deleteDoublyAtHead(DoublyNode* head) {
if (isDoublyListEmpty(head)) {
cout << "链表为空,无法删除" << endl;
return head;
}
DoublyNode* temp = head;
head = head->next;
if (head != nullptr) {
head->prev = nullptr;
}
delete temp; // 使用delete替代free
return head;
}
// 删除尾部节点
DoublyNode* deleteDoublyAtTail(DoublyNode* head) {
if (isDoublyListEmpty(head)) {
cout << "链表为空,无法删除" << endl;
return head;
}
// 只有一个节点的情况
if (head->next == nullptr) {
delete head; // 使用delete替代free
return nullptr;
}
DoublyNode* tail = getDoublyListTail(head);
tail->prev->next = nullptr;
delete tail; // 使用delete替代free
return head;
}
// 删除指定值的节点
DoublyNode* deleteDoublyByValue(DoublyNode* head, int val) {
if (isDoublyListEmpty(head)) {
cout << "链表为空,无法删除" << endl;
return head;
}
// 头节点就是要删除的节点
if (head->data == val) {
return deleteDoublyAtHead(head);
}
DoublyNode* current = head->next;
while (current != nullptr && current->data != val) {
current = current->next;
}
if (current == nullptr) {
cout << "未找到值为 " << val << " 的节点" << endl;
return head;
}
// 尾节点的情况
if (current->next == nullptr) {
return deleteDoublyAtTail(head);
}
// 中间节点的情况
current->prev->next = current->next;
current->next->prev = current->prev;
delete current; // 使用delete替代free
return head;
}
// 查找节点是否存在
bool searchDoublyNode(DoublyNode* head, int val) {
DoublyNode* current = head;
while (current != nullptr) {
if (current->data == val) {
return true;
}
current = current->next;
}
return false;
}
// 修改指定值为新值
bool modifyDoublyNode(DoublyNode* head, int oldVal, int newVal) {
DoublyNode* current = head;
while (current != nullptr) {
if (current->data == oldVal) {
current->data = newVal;
return true;
}
current = current->next;
}
return false;
}
// 正向遍历链表
void traverseDoublyForward(DoublyNode* head) {
if (isDoublyListEmpty(head)) {
cout << "链表为空" << endl;
return;
}
DoublyNode* current = head;
while (current != nullptr) {
cout << current->data << " <-> ";
current = current->next;
}
cout << "nullptr" << endl;
}
// 反向遍历链表
void traverseDoublyBackward(DoublyNode* head) {
if (isDoublyListEmpty(head)) {
cout << "链表为空" << endl;
return;
}
DoublyNode* current = getDoublyListTail(head);
while (current != nullptr) {
cout << current->data << " <-> ";
current = current->prev;
}
cout << "nullptr" << endl;
}
// 清空链表
DoublyNode* clearDoublyList(DoublyNode* head) {
DoublyNode* current = head;
while (current != nullptr) {
DoublyNode* next = current->next;
delete current; // 使用delete替代free
current = next;
}
return nullptr;
}
// 3. 循环链表实现(基于单向循环链表)
// 节点结构体
typedef struct CircularNode {
int data;
CircularNode* next;
} CircularNode;
// 创建新节点
CircularNode* createCircularNode(int val) {
CircularNode* node = new CircularNode; // 使用new替代malloc
node->data = val;
node->next = nullptr;
return node;
}
// 初始化链表(返回头节点)
CircularNode* initCircularLinkedList() {
return nullptr;
}
// 获取链表长度
int getCircularListLength(CircularNode* head) {
if (head == nullptr) {
return 0;
}
int length = 1; // 至少有头节点
CircularNode* current = head->next;
while (current != head) {
length++;
current = current->next;
}
return length;
}
// 判断链表是否为空
bool isCircularListEmpty(CircularNode* head) {
return head == nullptr;
}
// 在头部插入
CircularNode* insertCircularAtHead(CircularNode* head, int val) {
CircularNode* newNode = createCircularNode(val);
if (isCircularListEmpty(head)) {
head = newNode;
head->next = head; // 指向自身,形成循环
return head;
}
// 新节点插入到头节点之后,然后交换数据
newNode->next = head->next;
head->next = newNode;
// 交换数据,使新节点成为逻辑上的头节点
int temp = head->data;
head->data = newNode->data;
newNode->data = temp;
return head;
}
// 在尾部插入
CircularNode* insertCircularAtTail(CircularNode* head, int val) {
CircularNode* newNode = createCircularNode(val);
if (isCircularListEmpty(head)) {
head = newNode;
head->next = head; // 指向自身,形成循环
return head;
}
// 插入到头节点之后
newNode->next = head->next;
head->next = newNode;
// 新节点成为新的尾节点(头指针指向尾节点)
head = newNode;
return head;
}
// 在指定位置插入(pos从0开始)
CircularNode* insertCircularAtPosition(CircularNode* head, int pos, int val) {
int length = getCircularListLength(head);
// 空链表且插入位置为0的情况
if (isCircularListEmpty(head)) {
if (pos == 0) {
return insertCircularAtHead(head, val);
} else {
cout << "位置无效" << endl;
return head;
}
}
if (pos < 0 || pos > length) {
cout << "位置无效" << endl;
return head;
}
if (pos == 0) {
return insertCircularAtHead(head, val);
}
if (pos == length) {
return insertCircularAtTail(head, val);
}
CircularNode* newNode = createCircularNode(val);
CircularNode* current = head;
// 移动到要插入位置的前一个节点
for (int i = 0; i < pos; i++) {
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
return head;
}
// 删除头部节点
CircularNode* deleteCircularAtHead(CircularNode* head) {
if (isCircularListEmpty(head)) {
cout << "链表为空,无法删除" << endl;
return head;
}
// 只有一个节点的情况
if (head->next == head) {
delete head; // 使用delete替代free
return nullptr;
}
// 将下一个节点的数据复制到头节点,然后删除下一个节点
CircularNode* temp = head->next;
head->data = temp->data;
head->next = temp->next;
delete temp; // 使用delete替代free
return head;
}
// 删除尾部节点
CircularNode* deleteCircularAtTail(CircularNode* head) {
if (isCircularListEmpty(head)) {
cout << "链表为空,无法删除" << endl;
return head;
}
// 只有一个节点的情况
if (head->next == head) {
delete head; // 使用delete替代free
return nullptr;
}
// 找到尾节点的前一个节点
CircularNode* current = head;
while (current->next != head) {
current = current->next;
}
current->next = head->next;
delete head; // 使用delete替代free
head = current; // 前一个节点成为新的尾节点
return head;
}
// 删除指定值的节点
CircularNode* deleteCircularByValue(CircularNode* head, int val) {
if (isCircularListEmpty(head)) {
cout << "链表为空,无法删除" << endl;
return head;
}
// 只有一个节点的情况
if (head->next == head) {
if (head->data == val) {
delete head; // 使用delete替代free
return nullptr;
} else {
cout << "未找到值为 " << val << " 的节点" << endl;
return head;
}
}
CircularNode* current = head;
CircularNode* prev = nullptr;
// 查找要删除的节点
do {
prev = current;
current = current->next;
if (current->data == val) {
prev->next = current->next;
// 如果删除的是头节点(当前head的下一个节点)
if (current == head->next && head->data == val) {
head->data = current->data;
head->next = current->next;
}
// 如果删除的是尾节点(head本身)
if (current == head) {
head = prev;
}
delete current; // 使用delete替代free
return head;
}
} while (current != head);
cout << "未找到值为 " << val << " 的节点" << endl;
return head;
}
// 查找节点是否存在
bool searchCircularNode(CircularNode* head, int val) {
if (isCircularListEmpty(head)) {
return false;
}
CircularNode* current = head;
do {
if (current->data == val) {
return true;
}
current = current->next;
} while (current != head);
return false;
}
// 修改指定值为新值
bool modifyCircularNode(CircularNode* head, int oldVal, int newVal) {
if (isCircularListEmpty(head)) {
return false;
}
CircularNode* current = head;
do {
if (current->data == oldVal) {
current->data = newVal;
return true;
}
current = current->next;
} while (current != head);
return false;
}
// 遍历链表
void traverseCircularList(CircularNode* head) {
if (isCircularListEmpty(head)) {
cout << "链表为空" << endl;
return;
}
CircularNode* current = head;
do {
cout << current->data << " -> ";
current = current->next;
} while (current != head);
cout << "(回到头部)" << endl;
}
// 清空链表
CircularNode* clearCircularList(CircularNode* head) {
if (isCircularListEmpty(head)) {
return nullptr;
}
CircularNode* current = head->next;
while (current != head) {
CircularNode* next = current->next;
delete current; // 使用delete替代free
current = next;
}
delete head; // 使用delete替代free
return nullptr;
}
// 测试函数
int main() {
// 测试单向链表
cout << "=== 测试单向链表 ===" << endl;
SinglyNode* sll = initSinglyLinkedList();
sll = insertSinglyAtHead(sll, 1);
sll = insertSinglyAtTail(sll, 3);
sll = insertSinglyAtPosition(sll, 1, 2);
traverseSinglyList(sll); // 1 -> 2 -> 3 -> nullptr
cout << "查找 2: " << (searchSinglyNode(sll, 2) ? "找到" : "未找到") << endl;
modifySinglyNode(sll, 2, 5);
traverseSinglyList(sll); // 1 -> 5 -> 3 -> nullptr
sll = deleteSinglyByValue(sll, 5);
traverseSinglyList(sll); // 1 -> 3 -> nullptr
cout << "单向链表长度: " << getSinglyListLength(sll) << endl;
sll = clearSinglyList(sll);
// 测试双向链表
cout << "\n=== 测试双向链表 ===" << endl;
DoublyNode* dll = initDoublyLinkedList();
dll = insertDoublyAtHead(dll, 1);
dll = insertDoublyAtTail(dll, 3);
dll = insertDoublyAtPosition(dll, 1, 2);
traverseDoublyForward(dll); // 1 <-> 2 <-> 3 <-> nullptr
traverseDoublyBackward(dll); // 3 <-> 2 <-> 1 <-> nullptr
cout << "查找 2: " << (searchDoublyNode(dll, 2) ? "找到" : "未找到") << endl;
modifyDoublyNode(dll, 2, 5);
traverseDoublyForward(dll); // 1 <-> 5 <-> 3 <-> nullptr
dll = deleteDoublyByValue(dll, 5);
traverseDoublyForward(dll); // 1 <-> 3 <-> nullptr
cout << "双向链表长度: " << getDoublyListLength(dll) << endl;
dll = clearDoublyList(dll);
// 测试循环链表
cout << "\n=== 测试循环链表 ===" << endl;
CircularNode* cll = initCircularLinkedList();
cll = insertCircularAtHead(cll, 1);
cll = insertCircularAtTail(cll, 3);
cll = insertCircularAtPosition(cll, 1, 2);
traverseCircularList(cll); // 1 -> 2 -> 3 -> (回到头部)
cout << "查找 2: " << (searchCircularNode(cll, 2) ? "找到" : "未找到") << endl;
modifyCircularNode(cll, 2, 5);
traverseCircularList(cll); // 1 -> 5 -> 3 -> (回到头部)
cll = deleteCircularByValue(cll, 5);
traverseCircularList(cll); // 1 -> 3 -> (回到头部)
cout << "循环链表长度: " << getCircularListLength(cll) << endl;
cll = clearCircularList(cll);
return 0;
}