现在的位置: 首页 > 综合 > 正文

…….

2013年09月15日 ⁄ 综合 ⁄ 共 3176字 ⁄ 字号 评论关闭

 

 

Code:
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3.   
  4. struct list  
  5. {  
  6.     int ch;  
  7.     struct list *next;  
  8. };  
  9.   
  10. typedef struct list LIST;  
  11. typedef struct list *LISTPTR;  
  12.   
  13. LISTPTR add_list(int,LISTPTR);  
  14. void show_list(LISTPTR);  
  15. void free_list(LISTPTR);  
  16.   
  17. int main()  
  18. {  
  19.     LISTPTR first=NULL;  //head poi-nter  
  20.     int i=0;  
  21.     int ch;  
  22.     char trash[256];  
  23.     while(i++<5)  
  24.     {  
  25.         ch=0;  
  26.         printf("/n Enter character %d",&i);  
  27.     do  
  28.     {  
  29.         printf("/nMust be a to z:");  
  30.         ch=getc(stdin);  
  31.       
  32.         gets(trash);  
  33.     }while((ch<'a'||ch>'z')&&(ch<'A'||ch>'Z'));  
  34.   
  35.     first=add_list(ch,first);  
  36.     }  
  37.     show_list(first);  
  38.     free_list(first);  
  39.       
  40.     return 0;  
  41. }  
  42.   
  43.   
  44. LISTPTR add_list(int ch,LISTPTR first)  
  45. {  
  46.     LISTPTR new_rec =NULL;  
  47.     LISTPTR tmp_rec  =NULL;  
  48.     LISTPTR prev_rec =NULL;  
  49.       
  50.   
  51.     new_rec=(LISTPTR)malloc(sizeof(LIST));  
  52.     if(!new_rec)  
  53.     {  
  54.         printf("/nUnable to allocate memory/n");  
  55.         exit(1);  
  56.     }  
  57.   
  58.     //set new link's data  
  59.     new_rec->ch=ch;  
  60.     new_rec->next=NULL;  
  61.     if(first==NULL)         //adding first link to list  
  62.     {  
  63.      first=new_rec;  
  64.      new_rec->next=NULL;  
  65.     }  
  66.     else  
  67.     {  
  68.         if(new_rec->ch <  first->ch)  
  69.         {  
  70.         new_rec->next=first;  
  71.         first=new_rec;            
  72.         }         
  73.         else  
  74.         {   
  75.             tmp_rec=first->next;  
  76.             prev_rec=first;  
  77.             if(tmp_rec==NULL)  
  78.             {  
  79.                  prev_rec->next=new_rec;  
  80.             }  
  81.             else  
  82.             {  
  83.                 while((tmp_rec->next!=NULL))  
  84.                 {  
  85.                     if(new_rec->ch < tmp_rec->ch)  
  86.                     {  
  87.                     new_rec->next=tmp_rec;  
  88.                     prev_rec->next=new_rec;  
  89.                     break;  
  90.                     }  
  91.                     else  
  92.                     {  
  93.                     tmp_rec=tmp_rec->next;  
  94.                     prev_rec=prev_rec->next;  
  95.                     }  
  96.   
  97.                 }  
  98.                 if(tmp_rec->next==NULL)  
  99.                 {  
  100.   
  101.                 if(new_rec->ch<tmp_rec->ch)  
  102.                 {  
  103.                     new_rec->next=tmp_rec;  
  104.                     prev_rec->next=new_rec;  
  105.   
  106.                 }  
  107.                 else  
  108.                 {  
  109.                     tmp_rec->next=new_rec;  
  110.                     new_rec->next=NULL;  
  111.                 }  
  112.                
  113.                 }  
  114.             }  
  115.         }  
  116.     }  
  117.  return (first);  
  118. }  
  119. void show_list(LISTPTR first)  
  120. {  
  121.     LISTPTR cu;  
  122.     int counter=1;  
  123.     printf("/n/n Rec addr position Data Next rec addr/n");  
  124.     printf("========== ========== ============ ======/n");  
  125.     cu=first;  
  126.     while(cu!=NULL)  
  127.     {  
  128.         printf("%X       ",cu);  
  129.         printf("%2i  %c  ",counter++,cu->ch);  
  130.         printf("%X       /n",cu->next);  
  131.         cu=cu->next;  
  132.         printf("========== ========== ============ ======/n");  
  133.     }  
  134.       
  135.  }  
  136. void free_list(LISTPTR first)  
  137. {  
  138.     LISTPTR cu,next_rec;  
  139.     cu=first;  
  140.     while(cu!=NULL)  
  141.     {  
  142.       next_rec=cu->next;  
  143.       free(cu);  
  144.       cu=next_rec;  
  145.     }  
  146. }  

add_list() 分开写还能看懂,就这样写一堆了之后看着头都大了,感觉看了半天,脑子都不够用了 。

抱歉!评论已关闭.