编写一个算法将一个单链表逆转,要求在原表上进行,不允许重新建表
来源:百度知道 编辑:UC知道 时间:2024/06/21 04:50:07
RT 谢谢啦!
list_p reverse(list_p *lp) /*反转列表*/
{
list_p f=*lp,m=NULL,l=NULL;
while (f)
{
m=f->next ;
f->next=l;
l=f;
f=m;
}
return *lp=l;
}
/*调用示例*/
//---------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
typedef struct List{
int data;
struct List *next;
} list,*list_p;
list_p init(int *s,int n) /*构建链表*/
{
list_p rh=NULL,p,np;
int i;
for (i = 0; i<n; i++) {
np=(list_p)malloc(sizeof(list));
if (!rh) {
rh=p=np;
p->next=NULL;
}
else{
np->next =NULL;
p->next=np;
p=np;
}
np->data=s[i];
}
return rh;
}
list_p reverse(list_p *lp) /*反转列表*/
{
list_p f=*lp,m=NULL,l=NULL;
while (f)<