下面是单链表(Singly Linked List)的各个基本操作的代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
// 初始化单链表
void initList(Node** head) {
*head = NULL;
}
// 判断单链表是否为空
int isEmpty(Node* head) {
return head == NULL;
}
// 在链表头部插入元素
void insertAtHead(Node** head, int element) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = element;
newNode->next = *head;
*head = newNode;
}
// 在链表尾部插入元素
void insertAtTail(Node** head, int element) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = element;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
} else {
Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
// 在指定位置插入元素
void insertAtPosition(Node** head, int position, int element) {
if (position <= 0) {
printf("Error: Invalid position\n");
return;
}
if (position == 1) {
insertAtHead(head, element);
} else {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = element;
Node* current = *head;
int count = 1;
while (current != NULL && count < position - 1) {
current = current->next;
count++;
}
if (current == NULL) {
printf("Error: Invalid position\n");
return;
}
newNode->next = current->next;
current->next = newNode;
}
}
// 删除链表中第一个出现的指定元素
void deleteElement(Node** head, int element) {
Node* previous = NULL;
Node* current = *head;
while (current != NULL && current->data != element) {
previous = current;
current = current->next;
}
if (current == NULL) {
printf("Error: Element not found\n");
return;
}
if (previous == NULL) {
*head = current->next;
} else {
previous->next = current->next;
}
free(current);
}
// 获取链表长度
int getLength(Node* head) {
int length = 0;
Node* current = head;
while (current != NULL) {
length++;
current = current->next;
}
return length;
}
// 打印链表中的元素
void printList(Node* head) {
printf("Linked List: ");
Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
// 销毁链表,释放内存
void destroyList(Node** head) {
Node* current = *head;
while (current != NULL) {
Node* temp = current;
current = current->next;
free(temp);
}
*head = NULL;
}
int main() {
Node* head;
initList(&head);
insertAtHead(&head, 10);
insertAtHead(&head, 20);
insertAtTail(&head, 30);
insertAtPosition(&head, 2, 40);
printList(head);
deleteElement(&head, 20);
printList(head);
int length = getLength(head);
printf("Length of the linked list: %d\n", length);
destroyList(&head);
return 0;
}
```
这段代码实现了单链表的基本操作,包括初始化单链表 `initList`,判断单链表是否为空 `isEmpty`,在链表头部插入元素 `insertAtHead`,在链表尾部插入元素 `insertAtTail`,在指定位置插入元素 `insertAtPosition`,删除链表中第一个出现的指定元素 `deleteElement`,获取链表长度 `getLength`,打印链表中的元素 `printList`,以及销毁链表,释放内存 `destroyList`。
在 `main` 函数中,我们演示了如何使用这些基本操作来创建链表、插入元素、删除元素、获取链表长度,并打印链表的内容。
需要注意的是,这里使用了指针作为参数,通过传递指向指针的指针来修改头指针的值,以便在插入和删除操作中对链表进行修改。此外,这只是一个简单的示例,实际应用中可能需要根据具体需求进行修改和扩展。
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
// 初始化单链表
void initList(Node** head) {
*head = NULL;
}
// 判断单链表是否为空
int isEmpty(Node* head) {
return head == NULL;
}
// 在链表头部插入元素
void insertAtHead(Node** head, int element) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = element;
newNode->next = *head;
*head = newNode;
}
// 在链表尾部插入元素
void insertAtTail(Node** head, int element) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = element;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
} else {
Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
// 在指定位置插入元素
void insertAtPosition(Node** head, int position, int element) {
if (position <= 0) {
printf("Error: Invalid position\n");
return;
}
if (position == 1) {
insertAtHead(head, element);
} else {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = element;
Node* current = *head;
int count = 1;
while (current != NULL && count < position - 1) {
current = current->next;
count++;
}
if (current == NULL) {
printf("Error: Invalid position\n");
return;
}
newNode->next = current->next;
current->next = newNode;
}
}
// 删除链表中第一个出现的指定元素
void deleteElement(Node** head, int element) {
Node* previous = NULL;
Node* current = *head;
while (current != NULL && current->data != element) {
previous = current;
current = current->next;
}
if (current == NULL) {
printf("Error: Element not found\n");
return;
}
if (previous == NULL) {
*head = current->next;
} else {
previous->next = current->next;
}
free(current);
}
// 获取链表长度
int getLength(Node* head) {
int length = 0;
Node* current = head;
while (current != NULL) {
length++;
current = current->next;
}
return length;
}
// 打印链表中的元素
void printList(Node* head) {
printf("Linked List: ");
Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
// 销毁链表,释放内存
void destroyList(Node** head) {
Node* current = *head;
while (current != NULL) {
Node* temp = current;
current = current->next;
free(temp);
}
*head = NULL;
}
int main() {
Node* head;
initList(&head);
insertAtHead(&head, 10);
insertAtHead(&head, 20);
insertAtTail(&head, 30);
insertAtPosition(&head, 2, 40);
printList(head);
deleteElement(&head, 20);
printList(head);
int length = getLength(head);
printf("Length of the linked list: %d\n", length);
destroyList(&head);
return 0;
}