码迷,mamicode.com
首页 > 其他好文 > 详细

链表的逆置

时间:2018-08-04 11:51:17      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:str   span   link   lse   oid   false   efi   typedef   sizeof   

#include<stdio.h>

#include<stdlib.h>

#define N 5

typedef struct node{

   int  data;

   struct node * next;

}ElemSN;

ElemSN  * Createlink(int a[],int n){  

    int i;

    ElemSN * h=NULL, * p;

    for( i=N-1;i>=0;i--) {

          p=(ElemSN *)malloc(sizeof(ElemSN));

  p->data=a[i];

  p->next=h;

  h=p;

}

    return h;

   }

   void printlink(ElemSN * h){

       ElemSN * p;

       for(p=h;p;p=p->next)

   printf("%d\n",p->data);

   }

  ElemSN * Prelink(ElemSN * h) {

   ElemSN * h1=NULL, * p;     //h1链表的头结点

   while(h){                    //h为空截止,表示链表已经逆置

         p=h;                    //头结点给p

h=h->next;        //头结点后移

p->next=h1;      //头插

h1=p;                //设置头指针

   }   

       return h1;

}

int main(void){

int a[N]={10,20,30,40,50};

ElemSN * head;

        head=Createlink(a,9);

        head=Prelink(head);

printlink(head);

}



链表的逆置

标签:str   span   link   lse   oid   false   efi   typedef   sizeof   

原文地址:http://blog.51cto.com/13645380/2154301

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!