所谓有鬼 其实是人们心中有个鬼

让我帮你画个符吧

数据结构 单链表的创建 插入 删除

单链表比较简单,没什么好说的,直接上代码:

view plaincopy to clipboardprint?

   1. #include <stdio.h>  
   2. #include <stdlib.h>  
   3. #include <string.h>  
   4.   
   5. #define NUM 10  
   6.   
   7. typedef struct _chbList  
   8. {  
   9.     int data;  
  10.     struct _chbList *next;  
  11. }chbList;  
  12.   
  13. /**
  14.   *  pList:链表添加至末尾
  15.   *  时间:2009.7.29
  16.   */  
  17. int AddTail( chbList *pList,  chbList *pTail )  
  18. {  
  19.     if( pList == NULL || pTail == NULL )   
  20.         return 0;  
  21.     while( pList->next )  
  22.     {  
  23.         pList = pList->next;   
  24.     }  
  25.     pTail->next = NULL;  
  26.     pList->next = pTail;  
  27.     return 1;  
  28. }  
  29. /**
  30.   *  pList:链表根据位置插入到链表位置
  31.   *  时间:2009.7.29  
  32.   */  
  33. int InsertValue( chbList *pList, chbList *insert, int seq )  
  34. {  
  35.     if( pList == NULL || insert == NULL )  
  36.         return 0;  
  37.     int i = 0;  
  38.     while( pList && i < seq-1    )  
  39.     {  
  40.         pList = pList->next;  
  41.         i++;  
  42.     }  
  43.     insert->next = pList->next;  
  44.     pList->next = insert;  
  45.     return 1;  
  46. }  
  47. /**
  48.   *  pList:链表根据位置删除链表
  49.   *  时间:2009.7.29
  50.   */  
  51. int DeleteValue( chbList *pList, int seq )  
  52. {  
  53.     if( pList == NULL )  
  54.         return 0;  
  55.     chbList *pT;  
  56.     int i = 0;  
  57.     while( pList && i < seq-1 )  
  58.     {  
  59.         pList = pList->next;  
  60.         i++;  
  61.     }  
  62.     pT = pList->next;  
  63.     pList->next = pT->next;  
  64.     free( pT );  
  65.     return 1;  
  66. }  
  67.   
  68. int main( void )  
  69. {  
  70.     chbList *pList,*pTemp;  
  71.     int i;  
  72.   
  73.     pList = ( chbList * )malloc( sizeof( chbList ) );  
  74.     pList->next = NULL;  
  75.     pList->data = 0;  
  76.   
  77.     for( i = 0; i < NUM-1; i++ )  
  78.     {  
  79.         pTemp = ( chbList * )malloc( sizeof( chbList ) );  
  80.         AddTail( pList, pTemp );  
  81.         pTemp->data = i+1;  
  82.     }  
  83.     chbList *insert = ( chbList * )malloc( sizeof( chbList ) );  
  84.     insert->data = -1;  
  85.     InsertValue( pList, insert, 4 );  
  86.   
  87.     DeleteValue( pList, 2 );  
  88.   
  89.     while( pList )  
  90.     {  
  91.         printf( "%d ", pList->data );  
  92.         pList = pList->next;  
  93.     }  
  94.     while( pList )  
  95.     {  
  96.         free( pList );  
  97.         pList = pList->next;  
  98.     }  
  99.     return 0;  
 100. } 

posted on 2010-08-16 15:18  鬼话符  阅读(832)  评论(0)    收藏  举报

导航