程序代码:
st *fun(st *p,int s) { st *p1,*p2,*p3; p1=p->next; p2=p1->next; while(p1->i!=s) { p3=p2->next; p2->next=p1; p1=p2; p2=p3; } p->next->next='\0'; p->next=p1; return p; }
链表逆置
st *fun(st *p,int s) { st *p1,*p2,*p3; p1=p->next; p2=p1->next; while(p1->i!=s) { p3=p2->next; p2->next=p1; p1=p2; p2=p3; } p->next->next='\0'; p->next=p1; return p; }
/*将带头结点的单向链表逆置 例:若原链表从头至尾数据域为 2,4,6,8,10 逆置后,链表从头至尾数据域为 10,8,6,4,2 */ #include "stdio.h" #include "stdlib.h" typedef struct tt { int i; struct tt *next; }st; st *creat() // 输入0结束 { st *p,*s,*r; int t; p=(st *)malloc(sizeof(st)); r=p; scanf("%d",&t); while(t!=0) { s=(st *)malloc(sizeof(st)); s->i=t; r->next=s; r=s; scanf("%d",&t); } r->next='\0'; return p; } st *fun(st *p,int s) // s是最后一个结点的数据域 { st *p1,*p2,*p3; p1=p->next; p2=p1->next; while(p1->i!=s) { p3=p2->next; p2->next=p1; p1=p2; p2=p3; } p->next->next='\0'; p->next=p1; return p; } void main() { st *p,*h,*p1; int s; p=creat(); p1=p->next; printf("%d",p1->i); p1=p1->next; while(p1!='\0') { printf("->%d",p1->i); s=p1->i; p1=p1->next; } printf("\n"); h=fun(p,s); h=h->next; printf("%d",h->i); h=h->next; while(h!='\0') { printf("->%d",h->i); h=h->next; } }