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;
}
發佈了134 篇原創文章 · 獲贊 184 · 訪問量 30萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章