大佬们帮我看一下Reverse函数那哪出错了,函数实现的功能是链表逆转。
int main(){struct student *head,*p,*q,*n,*test;
struct student * Reverse(struct student * L);
int m=0;
p=q=(struct student *)malloc(sizeof(struct student));
scanf("%d%d",&p->num,&p->score);
head=NULL;
while(p->num!=0)
{
m=m+1;
if(m==1)
head=p;
else{
q->next=p;
q=p;
p=(struct student *)malloc(sizeof(struct student));
scanf("%d%d",&p->num,&p->score);
}}
q->next=NULL;
test=Reverse(head);
}
struct student * Reverse(struct student * L){
if(L==NULL) return NULL;
if(L->next==NULL) return L;
struct student *p,*r;
p=L;
r=p->next;
p->next=NULL;
while(r!=NULL){
r->next=p;
p=r;
r=r->next;
}
return r;
}
struct NODE *sll_reverse(struct NODE *first)
{
Node *newfirst;
Node *p = first;
Node *q = first;
if(first == NULL)
return NULL;
//让p指向最后一个元素,q指向倒数第二个
while(p->link != NULL){
q = p;
p = p->link;
}
newfirst = p;
//箭头反转
while(q != first){
p->link = q;
p = q;
}
q->link = NULL;
return newfirst;
}
页:
[1]