关于链表逆置问题
#include "stdio.h"#include "malloc.h"
struct number
{
int num;
struct number *next;
};
struct number *head, *p, *r, *newhead;
main()
{
int n=0;
newhead=p=r=head=(struct number *)malloc(sizeof(struct number));
newhead=NULL;
while(n<10)
{
p=(struct number *)malloc(sizeof(struct number));/*建立链表*/
printf("Enter the number:");
scanf("%d", &p->num);
if (head == NULL)
head=p;
else
r->next=p;
r=p;
n++;
}
r->next=NULL;
p=head->next;
do
{
printf("%4d", p->num);/*输出链表*/
p=p->next;
}while(p->next != NULL);
printf("%4d", p->num);
p=r=head;
for(n=0; n<10; n++)/*逆置链表*/
{
p=p->next;
r=p;
if (newhead == NULL)
{
newhead->next=r;
r->next=head;
}
else
{
r->next=newhead->next;
newhead->next=r;
}
}
for(p=newhead->next,n=0; n<10; p=p->next,n++)
{
printf("%4d", p->num);
}
getch();
}