作者:mobiledu2502909493 | 来源:互联网 | 2024-12-26 13:59
一、引言
在学习C语言的过程中,链表是一个非常重要的数据结构。本文将详细介绍如何使用C语言动态创建链表,并重点讲解头插法和尾插法的具体实现。通过这些方法,我们可以更灵活地管理链表中的节点。
二、头插法
头插法是指每次插入的新节点都作为链表的头部。具体实现如下:
// 动态创建链表(头插法)
struct Node *insertAtHead(struct Node *head) {
struct Node *newNode;
while (1) {
newNode = (struct Node *)malloc(sizeof(struct Node));
printf("请输入新节点的数据:\n");
scanf("%d", &newNode->data);
if (newNode->data == 0) {
printf("输入0退出!\n");
return head;
}
if (head == NULL) {
head = newNode;
newNode->next = NULL;
} else {
newNode->next = head;
head = newNode;
}
}
return head;
}
三、尾插法
尾插法是指每次插入的新节点都作为链表的尾部。具体实现如下:
// 动态创建链表(尾插法)
struct Node *insertAtTail(struct Node *head) {
struct Node *newNode, *temp = head;
while (1) {
newNode = (struct Node *)malloc(sizeof(struct Node));
printf("请输入新节点的数据:\n");
scanf("%d", &newNode->data);
if (newNode->data == 0) {
printf("输入0退出!\n");
return head;
}
if (head == NULL) {
head = newNode;
newNode->next = NULL;
} else {
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
newNode->next = NULL;
}
}
return head;
}
四、示例源码
以下是完整的示例源码,包含链表的遍历、计数、查询、插入和删除等基本操作:
#include
#include
struct Node {
int data;
struct Node *next;
};
void traverseList(struct Node *head) {
while (head != NULL) {
printf("%d ", head->data);
head = head->next;
}
putchar('\n');
}
int countNodes(struct Node *head) {
int count = 0;
while (head != NULL) {
count++;
head = head->next;
}
return count;
}
int searchNode(struct Node *head, int target) {
while (head != NULL) {
if (head->data == target)
return 1;
head = head->next;
}
return 0;
}
struct Node *insertAfter(struct Node *head, int target, struct Node *newNode) {
while (head != NULL) {
if (head->data == target) {
newNode->next = head->next;
head->next = newNode;
return head;
}
head = head->next;
}
return head;
}
struct Node *insertBefore(struct Node *head, int target, struct Node *newNode) {
if (head == NULL || head->data == target) {
newNode->next = head;
return newNode;
}
struct Node *prev = head;
while (prev->next != NULL && prev->next->data != target)
prev = prev->next;
if (prev->next != NULL) {
newNode->next = prev->next;
prev->next = newNode;
return head;
}
return head;
}
struct Node *deleteNode(struct Node *head, int target) {
if (head == NULL)
return head;
if (head->data == target)
return head->next;
struct Node *prev = head;
while (prev->next != NULL && prev->next->data != target)
prev = prev->next;
if (prev->next != NULL) {
struct Node *toDelete = prev->next;
prev->next = prev->next->next;
free(toDelete);
}
return head;
}
struct Node *insertAtHead(struct Node *head) {
struct Node *newNode;
while (1) {
newNode = (struct Node *)malloc(sizeof(struct Node));
printf("请输入新节点的数据:\n");
scanf("%d", &newNode->data);
if (newNode->data == 0) {
printf("输入0退出!\n");
return head;
}
if (head == NULL) {
head = newNode;
newNode->next = NULL;
} else {
newNode->next = head;
head = newNode;
}
}
return head;
}
struct Node *insertAtTail(struct Node *head) {
struct Node *newNode, *temp = head;
while (1) {
newNode = (struct Node *)malloc(sizeof(struct Node));
printf("请输入新节点的数据:\n");
scanf("%d", &newNode->data);
if (newNode->data == 0) {
printf("输入0退出!\n");
return head;
}
if (head == NULL) {
head = newNode;
newNode->next = NULL;
} else {
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
newNode->next = NULL;
}
}
return head;
}
int main() {
struct Node *head = NULL;
head = insertAtTail(head);
traverseList(head);
return 0;
}
五、程序运行
以下为尾插法的运行示例: