您的位置:首页 > 编程语言 > C语言/C++

C语言指针函数链表复习

2016-03-13 13:44 288 查看
指向整型数据的指针类型表示为:int *,读作“指向int的指针”或简称“int指针”


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


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

运用代码:

[code]#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
,*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为结束

[code]#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: