C语言链表插入函数实现
时间: 2023-09-25 18:16:25 浏览: 94
Sure, I'd be happy to help you with that! Here's an example of a simple linked list insertion function in C:
```
void insertNode(node_t **head_ref, int new_data) {
// Allocate memory for new node
node_t *new_node = (node_t*)malloc(sizeof(node_t));
// Set data for new node
new_node->data = new_data;
// Set next pointer to current head
new_node->next = (*head_ref);
// Update head pointer to new node
(*head_ref) = new_node;
}
```
This function takes in a pointer to the head of the linked list and a new data value to insert. It allocates memory for a new node, sets its data to the new value, sets its next pointer to the current head of the list, and updates the head pointer to point to the new node. This effectively inserts the new node at the beginning of the list.
I hope that helps! Let me know if you have any other questions.
阅读全文