利用動態鏈表記錄從標準輸入輸入的學生信息(學號、姓名、性別、年齡、得分、地址)
其中,學號長度不超過20, 姓名長度不超過40, 性別長度爲1, 地址長度不超過40
00630018 zhouyan m 20 10.0 28#460
輸入的最後以"end"結束 輸出 將輸入的內容倒序輸出
每行一條記錄,按照
學號 姓名 性別 年齡 得分 地址
的格式輸出 樣例輸入
00630018 zhouyan m 20 10 28#4600 0063001 zhouyn f 21 100 28#460000 0063008 zhoyan f 20 1000 28#460000 0063018 zhouan m 21 10000 28#4600000 00613018 zhuyan m 20 100 28#4600 00160018 zouyan f 21 100 28#4600 01030018 houyan m 20 10 28#4600 0630018 zuyan m 21 100 28#4600 10630018 zouan m 20 10 28#46000 end樣例輸出
10630018 zouan m 20 10 28#46000 0630018 zuyan m 21 100 28#4600 01030018 houyan m 20 10 28#4600 00160018 zouyan f 21 100 28#4600 00613018 zhuyan m 20 100 28#4600 0063018 zhouan m 21 10000 28#4600000 0063008 zhoyan f 20 1000 28#460000 0063001 zhouyn f 21 100 28#460000
00630018 zhouyan m 20 10 28#4600
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<stdlib.h>
#define LEN sizeof (struct stu)
struct stu
{
char num[100];
char name[50];
char sex[3];
char age[100];
char score[100];
char adre[50];
struct stu *next;//最好全部都用字符數組,而且數組不能太小,否則會wa
};
struct stu *creat(void)
{
struct stu *p1,*p2,*head;
head=NULL;
int i=0;
p1=(struct stu*)malloc(LEN);
while(1)
{
i++;
scanf("%s",p1->num);
if(strcmp(p1->num,"end")==0)
break;
else
{
scanf("%s",p1->name);
scanf("%s",p1->sex);
scanf("%s",p1->age);
scanf("%s",p1->score);
scanf("%s",p1->adre);
p1->next=NULL;
if(head==NULL)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct stu*)malloc(LEN);
}
}
return head;
}
struct stu *swap(struct stu *head)
{
struct stu *p,*q,*t;
p=head;
q=NULL;
while(p!=NULL)
{
t=p->next;
p->next=q;
q=p;
p=t;
}
return q;
}//這個函數就是單項鍊表逆轉函數
void print(struct stu *p)
{
struct stu *p1;
p1=p;
while(p1!=NULL)
{
printf("%s %s %s %s %s %s\n",p1->num,p1->name,p1->sex,p1->age,p1->score,p1->adre);
p1=p1->next;
}
}
int main()
{
struct stu *p,*head;
head=creat();
p=swap(head);
print(p);
return 0;
}