链表
单向动态链表:定义head,p1,p2三个结构体指针
head返回链表初地址,p2用于链接新开辟的p1.
#include <stdio.h>
#include <stdlib.h>
struct stu {
int num;
char name[10];
stu *next;
};
#define LEN sizof(struct stu)
int main() {
stu *wu();
stu *p=wu();
//输出链表
do{
printf("%d %s\n",p->num,p->name);
p=p->next;
}while(p);
return 0;
}
//创建链表
stu *wu() {
stu *head=NULL, *p1, *p2;
p1 = p2 = (stu *) malloc(sizeof(struct stu));
scanf("%d %s", &p1->num, p1->name);
int n = 0;//accout of node
while (p1->num != 0) {
n = n + 1;
if (n == 1) head = p1;
else p2->next = p1;
p2 = p1;
p1 = (stu *) malloc(sizeof(struct stu));
scanf("%d %s", &p1->num, p1->name);
}
p2->next=NULL;
return head;
}