像一个表格``
数学 英语 化学 物理
张三92 85 68 75
王二54 88 98 45
李四61 79 81 40
在TC里运行出来就是
像一个表格``
数学 英语 化学 物理
张三92 85 68 75
王二54 88 98 45
李四61 79 81 40
在TC里运行出来就是
这个好像有点麻烦.不过是对的哟.
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#define NULL 0
struct student
{
char name[200];
int math;
int english;
int physic;
int chemistry;
struct student *next;
};
struct student *creat()//链表的初使化
{
struct student *head;
head=(struct student *)malloc(sizeof(struct student));
head->next=NULL;
return head;
}
struct student *insert(struct student *head)//插入数据
{
struct student *p,*q;
//q=(struct student *)malloc(sizeof(struct student));
int i=1;
q=head;
while(i<=1)
{
p=(struct student *)malloc(sizeof(struct student));
p->next=NULL;
printf("please input the %dth number:\n",i);
printf("Name :");
scanf("%s",p->name);
printf("Math :");
scanf("%d",&p->math);
printf("English :");
scanf("%d",&p->english);
printf("Physic :");
scanf("%d",&p->physic);
printf("Chemistry :");
scanf("%d",&p->chemistry);
q->next=p;
q=p;
i++;
}
return head;
}
void print(struct student *head)//打印链表
{
struct student *r;
r=head->next;
printf("\nName Math English Physic Chemistry\n");
while (r!=NULL)
{
printf("%-8s",r->name);
printf("%-8d%-8d%-8d%-8d\n",r->math,r->english,r->physic,r->chemistry);
r=r->next;
}
printf("\n");
}
int main(void)
{
struct student *head,*p;
head=creat();
p=insert(head);
print(p);
return 0;
}