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

简单!

文章分类

全部博文(366)

文章存档

2013年(51)

2012年(269)

2011年(46)

分类: LINUX

2011-08-17 18:12:57

  1. #include <stdio.h>
  2. #include <malloc.h>

  3. typedef struct _DULNODE_
  4. {
  5.     int num;
  6.     struct _DULNODE_ *prior;
  7.     struct _DULNODE_ *next;
  8. }DULNODE,*DULLINK;

  9. DULLINK Creat_DulList(void)
  10. {
  11.     DULLINK head=NULL,pcurrent=NULL,ptemp=NULL;
  12.     int num=0,total=0;
  13.         
  14.     do
  15.     {
  16.         printf("please input the number: \n");
  17.         scanf("%d",&total);
  18.         if( total == 0 )
  19.             printf("sorry,the number is error!\n");
  20.         else
  21.             printf("\n");
  22.             
  23.     }while( total == 0 );
  24.     
  25.     while(num<total)
  26.     {
  27.         num=num+1;
  28.         
  29.         pcurrent=(DULLINK)malloc(sizeof(DULNODE));
  30.         if( pcurrent==NULL )
  31.         {
  32.             printf("malloc error,please check it!\n");
  33.             break;
  34.         }
  35.         else
  36.             pcurrent->num=num;
  37.         
  38.         if( num == 1)
  39.         {
  40.             head=pcurrent;
  41.             pcurrent->prior=NULL;        
  42.         }
  43.         else
  44.         {
  45.             ptemp->next=pcurrent;
  46.             pcurrent->prior=ptemp;
  47.         }
  48.                 
  49.         ptemp=pcurrent;
  50.     }
  51.     ptemp->next=NULL;
  52.     return(head);
  53. }

  54. void Print_DulInfo(DULLINK head)
  55. {
  56.     DULLINK ptemp;
  57.     
  58.     if( head == NULL)
  59.     {
  60.         printf("error:have no data!\n");
  61.         return;
  62.     }
  63.     
  64.     ptemp=head;
  65.     while(ptemp)
  66.     {
  67.         printf("num: %d\n",ptemp->num);
  68.         ptemp=ptemp->next;
  69.     }
  70.     
  71.     return;
  72. }

  73. DULLINK Insert_DulList(DULLINK head,DULLINK node)
  74. {
  75.     DULLINK pcurrent=NULL,pinsert=NULL;
  76.     
  77.     pcurrent=head;
  78.     pinsert=node;
  79.     
  80.     if( head==NULL )
  81.     {
  82.         head=node;
  83.         head->prior=head->next=NULL;
  84.     }
  85.     else
  86.     {
  87.         while(( pinsert->num>pcurrent->num )&&( pcurrent->next!=NULL ))
  88.         {
  89.             pcurrent=pcurrent->next;
  90.         }
  91.         
  92.         if( pinsert->num<=pcurrent->num )
  93.         {
  94.             if( pcurrent==head)
  95.             {
  96.                 head->prior=pinsert;
  97.                 pinsert->next=head;
  98.                 pinsert->prior=NULL;
  99.                 head=pinsert;
  100.             }
  101.             else
  102.             {
  103.                 pcurrent->prior->next=pinsert;
  104.                 pinsert->prior=pcurrent->prior;
  105.                 pinsert->next=pcurrent;
  106.                 pcurrent->prior=pinsert;
  107.             }
  108.         }
  109.         else
  110.         {
  111.             pcurrent->next=pinsert;
  112.             pinsert->prior=pcurrent;
  113.             pinsert->next=NULL;
  114.         }
  115.         
  116.     }
  117.     
  118.     return(head);
  119. }

  120. DULLINK Delete_DulList(DULLINK head,int num)
  121. {
  122.     DULLINK pcurrent=NULL;
  123.     
  124.     pcurrent=head;

  125.     if( head==NULL )
  126.     {
  127.         printf("error:please check the data!\n");
  128.         return(NULL);
  129.     }
  130.     else
  131.     {
  132.         while( (num!=pcurrent->num)&&(pcurrent->next!=NULL) )
  133.             pcurrent=pcurrent->next;
  134.             
  135.         if(num==pcurrent->num)
  136.         {
  137.             if( pcurrent==head )
  138.             {
  139.                 pcurrent->next->prior=NULL;
  140.                 head=pcurrent->next;                
  141.             }
  142.             else if(pcurrent->next==NULL)
  143.             {
  144.                 pcurrent->prior->next=NULL;
  145.             }
  146.             else
  147.             {
  148.                 pcurrent->prior->next=pcurrent->next;
  149.                 pcurrent->next->prior=pcurrent->prior;
  150.             }
  151.             
  152.             free(pcurrent);
  153.             
  154.             printf("the num have been deletea is: %d\n",num);
  155.         }
  156.         else
  157.             printf("can not find the num: %d\n",num);
  158.     }
  159.         
  160.     return(head);
  161. }

  162. int main(int argc,char **argv)
  163. {
  164.     DULLINK head,ptemp;
  165.     int num=0;
  166.     
  167.     head=Creat_DulList();
  168.     Print_DulInfo(head);    
  169.     
  170.     printf("please input the delete num:\n");
  171.     scanf("%d",&num);
  172.     head=Delete_DulList(head, num);
  173.     Print_DulInfo(head);
  174.     
  175.     ptemp=(DULLINK)malloc(sizeof(DULNODE));
  176.     printf("please input the add num:\n");
  177.     scanf("%d",&num);
  178.     ptemp->num=num;
  179.     head=Insert_DulList(head,ptemp);
  180.     Print_DulInfo(head);
  181.     
  182.     return 0;
  183. }
阅读(4424) | 评论(0) | 转发(0) |
0

上一篇:倒叙输出

下一篇:Linux练习题

给主人留下些什么吧!~~