根据输入的学生数据,建立学生信息单链表并输出。
输入格式:
有多组输入,每组有1+n行: 第一行为n,表示n个学生;接下来有n行,每一行有3个数:学号,姓名和成绩。(学号、成绩均为整数,姓名长度小于20)。
输出格式:
输出单链表,注意结尾的换行。
输入样例:
在这里给出一组输入。例如:
3
1001 lilin 98
1002 niqing 87
1003 sdfs 56
结尾无空行
输出样例:
在这里给出相应的输出。例如:
1003,sdfs,56->1002,niqing,87->1001,lilin,98
结尾无空行
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node{
int num;
char name[30];
int score;
struct node *next;
}Node;
int main(){
Node *p,*q,*head,*t,*prep;
head=NULL;
char s[30];
int i,x,g,n;
while(scanf("%d",&n)!=EOF){
head=NULL;
for(i=0;i<n;i++){
scanf("%d%s%d",&x,s,&g);
p=(Node*)malloc(sizeof(Node));
p->num=x;
strcpy(p->name,s);
p->score=g;
p->next=NULL;
if(head==NULL)
head=p;
else
q->next=p;
q=p;
}
p = head;
q = p->next;
while (q) {
prep = p;
p = q;
q= q->next;
p->next = prep;
}
prep=p;
t=prep;
while(t!=head){
printf("%d,%s,%d->",t->num,t->name,t->score);
t=t->next;
}
printf("%d,%s,%d\n",t->num,t->name,t->score);
}
return 0;
}
与之前的反转链表相似,只是这里给定了有几个数据