35、链表的倒序:下面程序,先用create函数创建一个链表,然后调用print函数将链表中各结点输出。例如:有4个结点,建立后输出的顺序为:
1 65 2 70 3 85 4 90
你这个输出也没看出是倒序啊?
如果需要倒序,可以利用递归方法实现
typedef struct _linker
{
int id;
int score;
_linker *next;
}linker;
linker *head = NULL;
void create()
{
linker *q = NULL;
while(1)
{
printf("请输入学号和成绩,两者都为0表示结束:");
int id,score;
scanf("%d%d",&id,&score);
if(id==0 && score ==0)
break;
linker *p = (linker*)malloc(sizeof(linker));
if(head == NULL)
head = p;
else
q->next = p;
q = p;
}
}
void print(linker *p)
{
if(p == NULL)
return;
print(p->next);
printf("%d %d ",p->id,p->score);
}
int main()
{
create();
print(head);
return 0;
}
参考:
#include "stdio.h"
struct student {
char name[20];
int score[3];
}student1;
void Display(struct student *p,int j) {
int i;
scanf("%s",&p->name);
fflush(stdin);
for(i=0;i<j;i++){
scanf("%d",&p->score[i]);
}
printf("%s,%d,%d,%d",p->name,p->score[0],p->score[1],p->score[2]);
}
int main() {
struct student *v = (struct student*)malloc(sizeof(struct student));
Display(v,3);
return 0;
}