C语言指针函数链表复习

指向整型数据的指针类型表示为:int *,读作“指向int的指针”或简称“int指针”

p=&a;//表示把a的地址赋给指针变量p

print(“%d”,*p);//即指针变量p所指向的变量的值,即变量a的值。

运用代码:

#include <stdio.h>
#define N 3
struct Student
{
int num;
char name[20];
float score[3];
float aver;
};

int main(int argc, const char * argv[])
 {
void input(struct Student stu[]);
struct Student max(struct Student stu[]);
void print(struct Student stu);
struct Student stu[N],*p=stu;
input(p);
print(max(p));
return 0;
}
void input(struct Student stu[])
{
int i;
printf("请输入各个学生的信息:学号、姓名、三门课成绩\n");
for (i=0; i<N; i++) {
    scanf("%d%s%f%f%f",&stu[i].num,&stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
    stu[i].aver=(stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3.0;
}
}
struct Student max(struct Student stu[]) {
int i,m=0;
for(i=0;i<N;i++)
{
    if(stu[i].aver>stu[m].aver)m=i;
}
return stu[m];
}
void print(struct Student stud)
{
printf("\n成绩最高的学生是:\n");
printf("学号:%d\n姓名:%s\n三门课成绩:%5.1f,%5.1f,%5.1f\n平均成绩:%6.2f\n",stud.num,stud.name,stud.score[0],stud.score[1],stud.score[2],stud.aver);
}

链表:成绩系统,输入0为结束

#include <stdio.h>
#include <malloc.h>
#define LEN sizeof(struct Student)
struct Student
{
   long num;
    float score;
    struct Student* next;
};
int n;
struct Student* creatn()
{
struct Student *head;
struct Student *p1,*p2;
n=0;
p1=p2=(struct Student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
head=NULL;
while (p1->num!=0) {
    n=n+1;
    if (n==1)
        head=p1;
        if (n==1)
            head=p1;

        else

            p2->next=p1;

    p2=p1;
    p1=(struct Student *)malloc(LEN);
    scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
printf("%d",n);
return (head);
}

    void print(struct Student head)
{
    struct Student *p;
    printf("\nNow,These %d records are :\n",n);

    p=head;
    if(head!=NULL)
    do
    {
        printf("%ld %5.1f\n",p->num,p->score);
        p=p->next;
    }while (p!=NULL);

}
int main(int argc, const char * argv[]) {
struct Student *head;
head=creatn();
printf(head);
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值