使用C语言创建单链表的基本步骤如下:
struct Node {
int data;
struct Node* next;
};
struct Node* head = NULL;
void insert(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
} else {
struct Node* current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
void printList() {
struct Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
insert(1);
insert(2);
insert(3);
printList();
return 0;
}
以上代码将创建一个包含3个节点的单链表,并输出节点的值。输出结果为:1 2 3。你可以在此基础上进一步扩展链表的功能,比如插入、删除和查找节点等操作。