链表逆置问题
这个问题我看下论坛也有很多的解答,可是我对比之后还是不明白我错哪里了~求前辈们给指导啊~
先谢过了~
#include<stdio.h>
typedef struct stu
{
int a;
struct stu *next;
}STU;
void main()
{
int n,i;
STU *p,*head,*q;
p=head=new STU;
scanf("%d",&n);
/*建立链表*/
for(i=0;i<n;i++)
{
scanf("%d",&p->a);
p->next=new STU;
p=p->next;
}
/* 输出链表 */
for (p = head; p != NULL; p = p->next)
printf("%d", p->a);
printf("\n");
/* 逆置链表 */
p = head;
head = q = NULL;
while (p != NULL)
{
if (head == NULL)
{
head = p;
p = p->next;
head->next = NULL;
}
else
{
q = p->next;
p->next = head;
head = p;
p = q;
}
}
/* 输出链表 */
for (p = head; p != NULL; p = p->next)
printf("%d", p->a);
printf("\n");
}