链表的逆置大问题
#include <stdio.h>#include <stdlib.h>
struct student {
int old;
struct student *next;
};
struct student *create();
struct student *f(struct student *head);
void output(struct student *head);
void main()
{
struct student *head;
head=create();
head=f(head);
output(head);
}
struct student *create()
{
struct student *head,*p,*q;
int n,i=0;
head=(struct student *)malloc(sizeof(struct student));
p=(struct student *)malloc(sizeof(struct student));
printf("plase input 节点数:");
scanf("%d",&n);
printf("plase input 年龄:");
scanf("%d",&p->old);
head->next=p;
p->next=NULL;
while(1)
{
if(i==n)
break;
i++;
q=(struct student *)malloc(sizeof(struct student));
if(i<n){
printf("plase input 年龄:");
scanf("%d",&q->old);
p->next=q;
p=q;
}
}
free(q);
p->next=NULL;
return(head);
}
struct student *f(struct student *head)
{
struct student *p,*q,*s;
int i=0;
p=head->next;
q=p->next;
s=q->next;
while(s!=NULL)
{
q->next=p;
p=q;
q=s;
s=s->next;
}
q->next=p;
head->next=q;
p->next->next=NULL;
return(head);
}
void output(struct student *head)
{
struct student *p=head->next ;
while(p!=NULL)
{
printf("%d\n",p->old);
p=p->next;
}
}
为什么3个节点是好的,多了就不行了