优点:
缺点:
优点:
缺点:
相关术语:
[注]:确定一个链表只需要头指针一个参数即可。
单向动态链表示例:
/**
* 单向动态链表测试
*/
# include <stdio.h>
# include <string.h>
# include <mm_malloc.h>
struct Student {
char name[10]; //链表数据
float score;
struct Student * pNext; //指针数据
};
int main() {
int len;
struct Student * create_stu_list(int);
void traverse_stu_list(struct Student *);
printf("please input the num of stu: ");
scanf(" %d", &len);
//创建链表
struct Student *pHead = create_stu_list(len);
//遍历链表
traverse_stu_list(pHead);
return 0;
}
struct Student * create_stu_list(int len) {
/*
pHead是头指针
pre指针的作用是开辟动态存储空间
post指针的作用是创建各结点的链接
*/
struct Student *pHead, *pre, *post;
//首结点
pre = (struct Student *) malloc(sizeof(struct Student));
pHead = pre;
post = pre;
printf("please input the information of stu:\n");
printf("name=");
scanf(" %s", pre->name);
printf("score=");
scanf(" %f", &pre->score);
int i = 1;
while (i<len) {
//开辟新的内存空间
pre = (struct Student *) malloc(sizeof(struct Student));
//赋值
printf("name=");
scanf(" %s", pre->name);
printf("score=");
scanf(" %f", &pre->score);
//勾连
post->pNext = pre;
post = pre;
i++;
}
post->pNext = NULL; //最后的尾结点指针不指向任何结点
return pHead;
}
void traverse_stu_list(struct Student *pHead) {
struct Student *p;
printf("[while] the student info is as follows: \n");
p = pHead;
while (p!=NULL) {
printf("%-15s %-5.1f\n", p->name, p->score);
p = p->pNext;
}
printf("[for] the student info is as follows: \n");
for (p=pHead; p!=NULL; p=p->pNext) {
printf("%-15s %-5.1f\n", p->name, p->score);
}
}