写出C语言代码,创建一个由6个结点的单向链表,实现增加、删除、查找、移动、显示结点的基本功能。
时间: 2024-01-31 15:02:45 浏览: 74
以下是一个实现单向链表的C语言代码,包括增加、删除、查找、移动和显示结点的基本功能:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义链表结构体
struct Node {
int data;
struct Node* next;
};
// 创建链表
struct Node* createLinkedList(int arr[], int n) {
struct Node* head = NULL;
struct Node* tail = NULL;
for (int i = 0; i < n; i++) {
struct Node* node = (struct Node*) malloc(sizeof(struct Node));
node->data = arr[i];
node->next = NULL;
if (head == NULL) {
head = node;
tail = node;
} else {
tail->next = node;
tail = node;
}
}
return head;
}
// 插入结点
struct Node* insertNode(struct Node* head, int data, int pos) {
struct Node* node = (struct Node*) malloc(sizeof(struct Node));
node->data = data;
node->next = NULL;
if (pos == 1) {
node->next = head;
head = node;
} else {
struct Node* prev = head;
for (int i = 1; i < pos - 1 && prev != NULL; i++) {
prev = prev->next;
}
if (prev == NULL) {
printf("Invalid position\n");
} else {
node->next = prev->next;
prev->next = node;
}
}
return head;
}
// 删除结点
struct Node* deleteNode(struct Node* head, int pos) {
if (head == NULL) {
printf("List is empty\n");
} else {
if (pos == 1) {
struct Node* temp = head;
head = head->next;
free(temp);
} else {
struct Node* prev = head;
for (int i = 1; i < pos - 1 && prev != NULL; i++) {
prev = prev->next;
}
if (prev == NULL || prev->next == NULL) {
printf("Invalid position\n");
} else {
struct Node* temp = prev->next;
prev->next = temp->next;
free(temp);
}
}
}
return head;
}
// 查找结点
int searchNode(struct Node* head, int data) {
int pos = 1;
struct Node* curr = head;
while (curr != NULL) {
if (curr->data == data) {
return pos;
}
pos++;
curr = curr->next;
}
return -1; // 没有找到
}
// 移动结点
struct Node* moveNode(struct Node* head, int pos1, int pos2) {
if (pos1 == pos2) {
return head;
}
struct Node* prev1 = NULL;
struct Node* curr1 = head;
for (int i = 1; i < pos1 && curr1 != NULL; i++) {
prev1 = curr1;
curr1 = curr1->next;
}
if (curr1 == NULL) {
printf("Invalid position\n");
return head;
}
struct Node* prev2 = NULL;
struct Node* curr2 = head;
for (int i = 1; i < pos2 && curr2 != NULL; i++) {
prev2 = curr2;
curr2 = curr2->next;
}
if (curr2 == NULL) {
printf("Invalid position\n");
return head;
}
if (prev1 != NULL) {
prev1->next = curr1->next;
} else {
head = curr1->next;
}
if (prev2 != NULL) {
prev2->next = curr1;
} else {
head = curr1;
}
curr1->next = curr2;
return head;
}
// 显示链表
void displayLinkedList(struct Node* head) {
struct Node* curr = head;
while (curr != NULL) {
printf("%d ", curr->data);
curr = curr->next;
}
printf("\n");
}
int main() {
int arr[] = {1, 2, 3, 4, 5, 6};
int n = sizeof(arr) / sizeof(int);
struct Node* head = createLinkedList(arr, n);
displayLinkedList(head);
head = insertNode(head, 7, 1);
displayLinkedList(head);
head = insertNode(head, 8, 4);
displayLinkedList(head);
head = deleteNode(head, 1);
displayLinkedList(head);
head = deleteNode(head, 3);
displayLinkedList(head);
int pos = searchNode(head, 5);
if (pos == -1) {
printf("Node not found\n");
} else {
printf("Node found at position %d\n", pos);
}
head = moveNode(head, 2, 5);
displayLinkedList(head);
return 0;
}
```
阅读全文