Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4040959
  • 博文数量: 366
  • 博客积分: 9916
  • 博客等级: 中将
  • 技术积分: 7195
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-29 23:27
个人简介

简单!

文章分类

全部博文(366)

文章存档

2013年(51)

2012年(269)

2011年(46)

分类: LINUX

2011-08-17 12:23:48

      将一个链表倒序。
 
  1. #include <stdio.h>
  2. #include <malloc.h>


  3. typedef struct _STUDENT_INFO_
  4. {
  5.     int num;
  6.     struct _STUDENT_INFO_ *next;
  7. }STUDENT_INFO;


  8. STUDENT_INFO *Creat_List(void)
  9. {
  10.     STUDENT_INFO *head=NULL;
  11.     STUDENT_INFO *pcurrent=NULL,*ptemp=NULL;
  12.     int stu_num=0,n=0;
  13.     
  14.     do
  15.     {
  16.         printf("please input the number of student:");
  17.         scanf("%d",&stu_num);
  18.         if( stu_num == 0 )
  19.             printf("sorry,the number is error!\n");
  20.         else
  21.             printf("\n");
  22.             
  23.     }while( stu_num == 0 );
  24.        
  25.     while( n<stu_num )
  26.     {
  27.         n=n+1;
  28.                          
  29.         pcurrent=(STUDENT_INFO *)malloc(sizeof(STUDENT_INFO));
  30.         if( pcurrent == NULL )
  31.         {
  32.             printf("malloc error!\n");
  33.             break;
  34.         }
  35.         else
  36.            pcurrent->num=stu_num-n+1;
  37.         
  38.         if( n == 1 )
  39.             head=pcurrent;
  40.         else
  41.             ptemp->next=pcurrent;

  42.         ptemp=pcurrent;
  43.     }
  44.     
  45.     ptemp->next=NULL;
  46.     return(head);
  47. }

  48. STUDENT_INFO *Reverse_Output(STUDENT_INFO *head)
  49. {
  50.     STUDENT_INFO *pcurrent=NULL,*ptemp=NULL,*pnext=NULL;
  51.     
  52.     if(head == NULL)
  53.     {
  54.         printf("no infomation to been printed,please check it!\n");
  55.         return(NULL);
  56.     }
  57.     
  58.     for( pcurrent=head;pcurrent; )
  59.     {        
  60.         pnext=pcurrent->next;
  61.         
  62.         if( pcurrent==head )
  63.             pcurrent->next=NULL;
  64.         else if( pcurrent->next==NULL )
  65.         {
  66.             pcurrent->next=ptemp;
  67.             head=pcurrent;
  68.             break;
  69.         }
  70.         else
  71.             pcurrent->next=ptemp;
  72.                 
  73.         ptemp=pcurrent;
  74.         pcurrent=pnext;
  75.     }
  76.     
  77.     return(head);
  78. }

  79. void Print_Infor(STUDENT_INFO *head)
  80. {
  81.     STUDENT_INFO *ptemp;
  82.     
  83.     if(head==NULL)
  84.     {
  85.         printf("Information error,please check it!\n");
  86.         return;
  87.     }
  88.     
  89.     ptemp=head;    
  90.     while(ptemp)
  91.     {
  92.         printf("Number: %d\n",ptemp->num);
  93.         ptemp=ptemp->next;
  94.     }
  95.     
  96.     return;
  97. }

  98. int main(int argc,char *argv[])
  99. {
  100.     STUDENT_INFO *head=NULL;
  101.     
  102.     head=Creat_List();
  103.     Print_Infor(head);
  104.     printf("\n");
  105.     head=Reverse_Output(head);
  106.     Print_Infor(head);
  107.     
  108.     return 0;
  109. }
阅读(1196) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~