结构体数组和结构体链表基本操作
结构体数组
#include<stdio.h>
#include<stdlib.h>
struct Student{
int num;
char name[10];
int score;
};
int main()
{
struct Student student[10];
int n;
scanf("%d\n",&n);
for(int i=0;i<n;i++)
{
scanf("%d %s %d",&student[i].num,&student[i].name,&student[i].score);
}
for(int i=0;i<n;i++)
{
printf("%d %s %d\n",student[i].num,student[i].name,student[i].score);
}
}
结构体链表 有两种:
#include<stdio.h>
#include<stdlib.h>
struct link{
int num;
char name[10];
int score;
struct link *next;
};
int main()
{
int n;
scanf("%d\n",&n);
struct link *head;
head=(struct link*)malloc(sizeof(struct link));
head->next=NULL;
for(int i=0;i<n;i++)
{
struct link *p;
p=(struct link*)malloc(sizeof(struct link));
scanf("%d %s %d",&p->num,&p->name,&p->score);
p->next=head->next;
head->next=p;
}
struct link *pmove;
pmove=head->next;
for(int i=0;i<n;i++)
{
printf("%d %s %d\n",pmove->num,pmove->name,pmove->score);
pmove=pmove->next;
}
}
#include<stdio.h>
#include<stdlib.h>
struct Student{
int num;
char name[10];
int score;
};
struct link{
struct Student data;
struct link *next;
};
int main()
{
int n;
scanf("%d\n",&n);
struct link *head;
head=(struct link*)malloc(sizeof(struct link));
head->next=NULL;
for(int i=0;i<n;i++)
{
struct link *p;
p=(struct link*)malloc(sizeof(struct link));
scanf("%d %s %d",&p->data.num,&p->data.name,&p->data.score);
p->next=head->next;
head->next=p;
}
struct link *pmove;
pmove=head->next;
for(int i=0;i<n;i++)
{
printf("%d %s %d\n",pmove->data.num,pmove->data.name,pmove->data.score);
pmove=pmove->next;
}
}
|