给我用c语言写个代码,完成链表的插入,删除,查找
时间: 2023-05-31 13:05:34 浏览: 95
以下是一个简单的链表结构体定义:
```c
typedef struct Node{
int data;
struct Node *next;
}Node;
```
链表插入:
```c
void insert(Node **head, int data){
Node *new_node = (Node*)malloc(sizeof(Node));
new_node->data = data;
new_node->next = NULL;
if(*head == NULL){
*head = new_node;
return;
}
Node *temp = *head;
while(temp->next != NULL){
temp = temp->next;
}
temp->next = new_node;
}
```
链表删除:
```c
void delete(Node **head, int data){
if(*head == NULL){
return;
}
if((*head)->data == data){
Node *temp = *head;
*head = (*head)->next;
free(temp);
return;
}
Node *temp = *head;
while(temp->next != NULL){
if(temp->next->data == data){
Node *del_node = temp->next;
temp->next = temp->next->next;
free(del_node);
return;
}
temp = temp->next;
}
}
```
链表查找:
```c
Node* search(Node *head, int data){
Node *temp = head;
while(temp != NULL){
if(temp->data == data){
return temp;
}
temp = temp->next;
}
return NULL;
}
```
接下来就可以在主函数中利用这些函数来操作链表了。
阅读全文