用c++代码实现常见的链表操作,插入头部,尾部,某数据之后或之前,修改,删除,排序,存储到文件,从文件构建链表,判空,是否存在,清空,排序
时间: 2024-05-01 14:17:14 浏览: 118
单向链表功能实现的源代码
4星 · 用户满意度95%
以下是用C语言实现常见链表操作的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义链表节点结构体
typedef struct Node {
int data;
struct Node *next;
} Node;
// 插入头部
void insertAtHead(Node **head, int data) {
Node *newNode = (Node*) malloc(sizeof(Node));
newNode->data = data;
newNode->next = *head;
*head = newNode;
}
// 插入尾部
void insertAtTail(Node **head, int data) {
Node *newNode = (Node*) malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
} else {
Node *temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
// 插入某数据之后
void insertAfter(Node *node, int data) {
if (node == NULL) {
return;
}
Node *newNode = (Node*) malloc(sizeof(Node));
newNode->data = data;
newNode->next = node->next;
node->next = newNode;
}
// 插入某数据之前
void insertBefore(Node **head, Node *node, int data) {
if (node == NULL) {
return;
}
if (*head == node) {
insertAtHead(head, data);
return;
}
Node *newNode = (Node*) malloc(sizeof(Node));
newNode->data = data;
Node *temp = *head;
while (temp->next != node) {
temp = temp->next;
}
newNode->next = node;
temp->next = newNode;
}
// 修改节点
void modifyNode(Node *node, int data) {
if (node == NULL) {
return;
}
node->data = data;
}
// 删除节点
void deleteNode(Node **head, Node *node) {
if (*head == NULL || node == NULL) {
return;
}
if (*head == node) {
*head = node->next;
free(node);
return;
}
Node *temp = *head;
while (temp->next != node) {
temp = temp->next;
}
temp->next = node->next;
free(node);
}
// 排序链表
void sortList(Node **head) {
if (*head == NULL || (*head)->next == NULL) {
return;
}
Node *curr, *prev, *next;
int swapped;
do {
swapped = 0;
curr = *head;
prev = NULL;
while (curr->next != NULL) {
next = curr->next;
if (curr->data > next->data) {
swapped = 1;
curr->next = next->next;
next->next = curr;
if (prev == NULL) {
*head = next;
} else {
prev->next = next;
}
prev = next;
} else {
prev = curr;
curr = curr->next;
}
}
} while (swapped);
}
// 存储链表到文件
void saveListToFile(Node *head, char *filename) {
FILE *fp = fopen(filename, "w");
if (fp == NULL) {
printf("Error opening file.\n");
return;
}
Node *temp = head;
while (temp != NULL) {
fprintf(fp, "%d ", temp->data);
temp = temp->next;
}
fclose(fp);
}
// 从文件构建链表
void buildListFromFile(Node **head, char *filename) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
printf("Error opening file.\n");
return;
}
int data;
while (fscanf(fp, "%d", &data) != EOF) {
insertAtTail(head, data);
}
fclose(fp);
}
// 判断链表是否为空
int isEmpty(Node *head) {
return (head == NULL);
}
// 判断某数据是否存在于链表中
int contains(Node *head, int data) {
Node *temp = head;
while (temp != NULL) {
if (temp->data == data) {
return 1;
}
temp = temp->next;
}
return 0;
}
// 清空链表
void clearList(Node **head) {
Node *temp;
while (*head != NULL) {
temp = *head;
*head = (*head)->next;
free(temp);
}
}
// 输出链表
void printList(Node *head) {
Node *temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
Node *head = NULL;
insertAtHead(&head, 4);
insertAtHead(&head, 3);
insertAtTail(&head, 5);
insertAfter(head->next, 6);
insertBefore(&head, head->next, 2);
printList(head); // 输出 3 2 4 6 5
modifyNode(head->next, 1);
deleteNode(&head, head->next);
sortList(&head);
printList(head); // 输出 2 3 4 5
saveListToFile(head, "list.txt");
clearList(&head);
buildListFromFile(&head, "list.txt");
printf("Is list empty? %d\n", isEmpty(head));
printf("Does list contain 4? %d\n", contains(head, 4));
printList(head); // 输出 2 3 4 5
clearList(&head);
printf("Is list empty? %d\n", isEmpty(head));
return 0;
}
```
阅读全文