首页 > 学院 > 开发设计 > 正文

c语言指针链表

2019-11-08 03:06:51
字体:
来源:转载
供稿:网友

#include<stdio.h>#include<stdlib.h>struct NODE{	int num;	char name[20];	char sex;	float score;	struct NODE *next;};struct NODE *creat(){	int n;	struct NODE *head, *pf, *pb;	int i;	PRintf("how many student:");	scanf("%d", &n);	for (i = 0; i < n; i++)	{		pb = (struct NODE*)malloc(sizeof(struct NODE));		printf("input the %d student:/n", i + 1);		scanf("%d", &(pb->num));		scanf("%s", (pb->name));		scanf("%c");		scanf("%c", &(pb->sex));		scanf("%f", &(pb->score));		if (i == 0) pf = head = pb;		else pf->next = pb;		pb->next = NULL;		pf = pb;	}	return(head);}struct NODE *findmax(struct NODE *n){	if (NULL == n)	{		printf("错误:链表为空/n");		exit(-1);	}	struct NODE *head;	struct NODE *max;	max = n;	head = n;	while (1)	{		if (head->score > max->score)max = head;		head = head->next;		if (head == NULL)break;	}	return(max);}void prn(struct NODE *p){	printf("the max is:/n%d %s %c %g", p->num, p->name, p->sex, p->score);}void clear(struct NODE *head){	NODE *next;  	if (head == NULL)	{		printf("clearList函数执行,链表为空/n");		return;	}	while (head->next != NULL)	{		next = head->next;		free(head);		head = next;	}}int main(){	NODE *p, *head;	head = creat();//创建链表	p = findmax(head);//查找成绩最好的学生	prn(p);  //打印信息	clear(head);//释放空间}


上一篇:数据库连接

下一篇:指针小结

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表