链表的就地逆置;(假设表长大于2)
代码如下:
#include<stdio.h>
#include<stdlib.h>
struct list{
int num;
struct list *next;
};
struct list *creat()
{
struct list *head,*p,*q;
int n=0;
p=(struct list *)malloc(sizeof(struct list));
head=NULL;
scanf("%d",&p->num);
while(p->num!=0)
{
n=n+1;
if(n==1)
head=p;
else
q->next=p;
q=p;
p=(struct list *)malloc(sizeof(struct list));
scanf("%d",&p->num);
}
q->next=NULL;
return(head);
}
void Output(struct list *head)
{
struct list *p;
p=head;
while(p!=NULL)
{
printf("%d\n",p->num);
p=p->next;
}
}
Rebellious(struct list *head)
{
struct list *p=head,*q=p->next,*s=q->next;
while(s)
{
q->next=p;
p=q;
q=s;
s=s->next;
}
q->next=p;
head->next=NULL;
Output(q);
}
int main(){
printf("请输入需要建立的链表(每输入一个元素回车,以0为结束):\n");
struct list *head;
head=creat();
printf("建立的链表\n");
Output(head);
printf("逆置后的链表\n");
Rebellious(head);
}