用c语言链表的基本操作 链表的定义 链表的初始化 链表的插入 链表的显示 链表的删除 链表的查找
时间: 2024-04-05 19:27:40 浏览: 76
链表是一种常用的数据结构,用于存储组织数据。它由一系列节点组成,每个节点包含数据和指向下一个节点的指针。
链表的基本操作如下:
1. 链表的定义:链表由节点组成,每个节点包含数据和指向下一个节点的指针。可以使用结构体来定义链表节点的结构。
```c
struct Node {
int data;
struct Node* next;
};
```
2. 链表的初始化:初始化一个空链表,即将头节点指针设置为NULL。
```c
struct Node* head = NULL;
```
3. 链表的插入:在链表中插入一个新节点,可以在链表的头部、尾部或指定位置插入。
- 在头部插入新节点:
```c
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = newData;
newNode->next = head;
head = newNode;
```
- 在尾部插入新节点:
```c
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));newNode->data = newData;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
} else {
struct Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
```
- 在指定位置插入新节点:
```c
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = newData;
struct Node* temp = head;
int position = 1;
while (temp != NULL && position < desiredPosition) {
temp = temp->next;
position++;
}
if (temp == NULL) {
printf("Invalid position\n");
} else {
newNode->next = temp->next;
temp->next = newNode;
}
```
4. 链表的显示:遍历链表并打印每个节点的数据。
```c
struct Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
```
5. 链表的删除:删除链表中的一个节点,可以根据节点的值或位置进行删除。
- 根据节点的值删除:
```c
struct Node* temp = head;
struct Node* prev = NULL;
int valueToDelete;
printf("Enter the value to delete: ");
scanf("%d", &valueToDelete);
while (temp != NULL && temp->data != valueToDelete) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) {
printf("Value not found\n");
} else if (prev == NULL) {
head = temp->next;
free(temp);
} else {
prev->next = temp->next;
free(temp);
}
```
- 根据位置删除:
```c
struct Node* temp = head;
struct Node* prev = NULL;
int positionToDelete;
printf("Enter the position to delete: ");
scanf("%d", &positionToDelete);
int currentPosition = 1;
while (temp != NULL && currentPosition < positionToDelete) {
prev = temp;
temp = temp->next;
currentPosition++;
}
if (temp == NULL) {
printf("Invalid position\n");
} else if (prev == NULL) {
head = temp->next;
free(temp);
} else {
prev->next = temp->next;
free(temp);
}
```
6. 链表的查找:根据节点的值或位置查找链表中的一个节点。
- 根据节点的值查找:
```c
struct Node* temp = head;
int valueToFind;
printf("Enter the value to find: ");
scanf("%d", &valueToFind);
while (temp != NULL && temp->data != valueToFind) {
temp = temp->next;
}
if (temp == NULL) {
printf("Value not found\n");
} else {
printf("Value found\n");
}
```
- 根据位置查找:
```c
struct Node* temp = head;
int positionToFind;
printf("Enter the position to find: ");
scanf("%d", &positionToFind);
int currentPosition = 1;
while (temp != NULL && currentPosition < positionToFind) {
temp = temp->next;
currentPosition++;
}
if (temp == NULL) {
printf("Invalid position\n");
} else {
printf("Value at position %d is %d\n", positionToFind, temp->data);
}
```
阅读全文
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![cpp](https://img-home.csdnimg.cn/images/20250102104920.png)
![cpp](https://img-home.csdnimg.cn/images/20250102104920.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)