使用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。你可以在此基礎上進一步擴展鏈表的功能,比如插入、刪除和查找節點等操作。