逆序链表的C程序

一个简单的逆序链表操作的C程序

先把head置NULL,再逐一反向就OK了。

#include  < stdio.h >
#include  < stdlib.h >

typedef 
struct  List {   
  
int data;   
  
struct List *next;   
}
List;   

/*  创建链表  */
List 
* list_create( void )   
{   
  List 
*head,*tail,*p;   
  
int e;   
  head
=(List *)malloc(sizeof(List));   
  tail
=head;   
  printf(
" List Create,input numbers(end of 0):");   
  scanf(
"%d",&e);   
  
while(e)
  
{   
    p
=(List *)malloc(sizeof(List));   
    p
->data=e;   
    tail
->next=p;   
    tail
=p;   
    scanf(
"%d",&e);
  }
   
  tail
->next=NULL;   
  
return   head;   
  }

    
/*  逆序函数  */
List  
* list_reverse(List  * head)  
{
  List 
*p,*temp;
  p 
= head;
  head 
= NULL;
  
while(p)
  
{
    temp 
= p;
    p 
= p->next;
    temp
->next = head;
    head 
= temp;
  }

}


int  main( void )   
{   
  
struct List *head,*p;   
  
int d;   
  head
=list_create();   
  printf(
" ");   
  
for(p=head->next;p;p=p->next)   
  printf(
"--%d--",p->data);   /*end of Create   */

  head
=list_reverse(head);   
  printf(
" ");   
  
for(p=head;p->next;p=p->next)   
  printf(
"--%d--",p->data);
  printf(
" ");
  
/*getch();*/
  
/*system("pause");*/
  
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值