1.简单的静态链表
typedef struct student
{
int num;
float score;
struct student* next;
}SL;
int main()
{
SL a, b, c, * p, * head;
a.num = 1, a.score = 89.5;
b.num = 2, b.score = 56.4;
c.num = 3, c.score = 93;
p = &a;
a.next = &b;
b.next = &c;
c.next = NULL;
head = p;
do
{
printf("%d %.2f\n", head->num, head->score);
head = head->next;
} while (head!= NULL);
}
2.简单的动态链表
typedef struct student
{
int num;
float score;
struct student* next;
}SL;
int main()
{
SL* head, * p;
head = p = (SL*)malloc(sizeof(SL));//头和尾都指向第一个节点,同时申请第一个节点的空间
printf("请输入第一个节点信息\n");
scanf("%d %f", &p->num, &p->score);
printf("请输入第二个节点信息\n");
p = (SL*)malloc(sizeof(SL));//申请节点二的空间
scanf("%d %f", &p->num, &p->score);
head->next = p;
p->next = NULL;// head > p > NULL
p = head;//重置p回到初始,方便等下遍历输出
printf("%d %.2f\n", p->num, p->score);
p = p->next;
printf("%d %.2f", p->num, p->score);//若不写p = head是无法实现这里的输出
return 0;
}