c链表反转

来源:互联网 发布:广州网店美工培训 编辑:程序博客网 时间:2024/05/17 08:13
#include <stdio.h>#include <stdlib.h>typedef struct node{    int data;    struct node *next;}List;List* init(int len){    List *h,*p,*r;    h=(List*)malloc(sizeof(List));h->next=NULL;           int i;for (i=0;i<len;i++){p=(List*)malloc(sizeof(List));p->data=i+1;p->next=NULL;if(!h->next)    h->next=p;else    r->next=p;r=p;}                                        /*建立链表*///p=h->next;//while(p){//    printf("%d  ",p->data);//p=p->next;}return h;}List* inver(List*head){    List *p,*r;    //h=(List*)malloc(sizeof(List));        p=head->next;    while(p->next)    {         r=p->next;p->next=r->next;r->next=head->next;head->next=r;    }    p=head->next;    while(p){                printf("%d  ",p->data);            p=p->next;}    return head;}                                      /*反转链表*/main(){    List *head,*head2,*p;    int len=5;    head=init(len);p=head->next;    while(p){                printf("%d  ",p->data);            p=p->next;}    head2=inver(head);}


原创粉丝点击