文档库 最新最全的文档下载
当前位置:文档库 › #1104 [填空题]链表的倒序

#1104 [填空题]链表的倒序

/** #1104 [填空题]链表的倒序
下面程序,先创建一个链表,然后调用reverse函数,将链表中各结点变为倒序排列。请完成reverse函数,
输入样例

3 (3 students)
1 (code of no.1 student)
98 (score of no.1 student)
4 (code of no.2 student)
99 (score of no.2 student)
5 (code of no.3 student)
87 (score of no.3 student)


输出样例

1 98
4 99
5 87
5 87
4 99
1 98
//*/
#include "stdio.h"
#include "malloc.h"
#define LEN sizeof(struct student)

struct student
{
long num;
int score;
struct student *next;
};

struct student *create(int n)
{
struct student *head=NULL,*p1=NULL,*p2=NULL;
int i;
for(i=1;i<=n;i++)
{ p1=(struct student *)malloc(LEN);
scanf("%ld",&p1->num);
scanf("%d",&p1->score);
p1->next=NULL;
if(i==1) head=p1;
else p2->next=p1;
p2=p1;
}
return(head);
}

void print(struct student *head)
{
struct student *p;
p=head;
while(p!=NULL)
{
printf("%8ld%8d",p->num,p->score);
p=p->next;
printf("\n");
}
}

struct student *reverse(struct student *head)
{
struct student *p1=NULL,*p2=NULL,*p3=NULL;
if(head == NULL) return head;
if(head->next == NULL) return head;
p2=head;
p3=head -> next;
p2->next = NULL;
while(p3!=NULL){
p1 = p3;
p3 = p3->next;
p1->next = p2;
p2 = p1;
}
head = p2;
return head;
}

main()
{
struct student *head,*stu;
int n;
scanf("%d",&n);
head=create(n);
print(head);
head=reverse(head);
print(head);
}

相关文档
相关文档 最新文档