对于输入的若干学生的信息,利用链表进行储存,并将学生的信息逆序输出。
要求将学生的完整信息存放在链表的结点中。通过链表的操作完成信息的逆序输出。
输入格式:
首先输入一个正整数T,表示测试数据的组数,然后是T组测试数据。
每组测试数据首先输入一个正整数n,表示学生的个数(1≤n≤100);然后是n行信息,分别表示学生的姓名(不含空格且长度不超过10的字符串)和年龄(正整数)。
输出格式:
对于每组测试,逆序输出学生信息(参看输出样例)。
输入样例:
1
3
Zhangsan 20
Lisi 21
Wangwu 20
输出样例:
Wangwu 20
Lisi 21
Zhangsan 20
上代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct liu{
char a[100];
struct liu *next;
}liu;
liu* chushi(){
int i,n;
liu* head=(liu*)malloc(sizeof(liu));
liu* p=head;
scanf("%d",&n);
getchar();
for(i=0;i<n;i++){
liu*news=(liu*)malloc(sizeof(liu));
gets(news->a);
news->next=NULL;
p->next=news;
p=news;
}
return head;
}
void daohuan(liu*head){
if(NULL==head)
return ;
liu*p,*q,*r;
p=head->next;
q=NULL;
head->next=NULL;
while(p){
r=p->next;
p->next=q;
q=p;
p=r;
}
head->next=q;
}
void lilist(liu *head) {
if(NULL==head){
return ;
}
//辅助指针变量
liu* p1=head->next;//这里是把head中的数值直接跳过了 ,头节点里的值没用
while(p1!=NULL){
puts(p1->a);
p1=p1->next;
}
}
int main(){
int j;
scanf("%d",&j);
liu*head;
while(j>0){
head=chushi();
daohuan(head);
lilist(head);
j=j-1;
}
}
希望对你们有所帮助!!!!