每個節點包含資料欄位與指向下一節點的指標:
struct node {
int data; // 資料欄位,可為多種型別
struct node *next; // 指標欄位,僅能有一個
};

malloc),直到指標指向 NULL 為止。(struct node*)malloc(sizeof(struct node))設有指標變數 struct node *p, *p1, *head;
head = p1 = p = (struct node *)malloc(sizeof(struct node));

scanf("%d", &p->data); // 輸入 10
p = (struct node *)malloc(sizeof(struct node));

p1->next = p;

p1 = p;
scanf("%d", &p->data); // 輸入 8
p = (struct node *)malloc(sizeof(struct node));
重複步驟 3、4,直到輸入結束條件(如 0),最後設定尾節點 p->next = NULL。
#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct node)
struct node {
int data;
struct node *next;
};
int main() {
struct node *p, *p1, *head;
head = p = (struct node *)malloc(LEN);
scanf("%d", &p->data); // 頭節點資料
while (p->data != 0) { // 0 為結束條件
p1 = p;
p = (struct node *)malloc(LEN);
scanf("%d", &p->data); // 中間節點資料
p1->next = p; // 串聯
}
p->next = NULL; // 尾節點
// 顯示鏈表
p = head;
printf("鏈表資料成員為:");
while (p->next != NULL) {
printf("%d", p->data);
p = p->next;
}
printf("%d\n", p->data);
return 0;
}
