可接受任意类型数据的C语言链表

来源:互联网 发布:帝国cms英文语言包 编辑:程序博客网 时间:2024/05/22 04:36


     今天在宣讲会上讲了一下我所写的那个可以接收任意类型的参数类型的链表,感觉还是没有讲清楚,再次整理一下 ,同样的道理我们可以构造出其它的可以接受任意数据类型的结构,如:可接收任意数据类型的栈,可接收任意数据类型的队列,可接收任意数据类型的树·················

 

 


       现在我们要在链表中存储任意类型的数据(也就是说数据所占字节数是在使用链表的时候确定),既然要能存储任意类型的数据,那么我们的链表的节点和链表的定义就要做一些修改了。

       下图是节点和链表的定义,data是一个ElemType类型的数据,而ElemType是被我们定义成了一个void *,也就是一个空指针。head和tail都是一个ChainNode类型的指针,作为链表的头结点和尾巴节点,头结点本书的数据域是不存放数据的。Nodesize表示要存储的数据类型的字节大小。


这个时候data这个指针才是真正指向了我们存放在链表里面的数据,

实现代码的讲解:

首先我们初始化一个链表mylist

myElemType是我们自己定义的任意类型的数据

       List * mylist;                  

       mylist = CreateList( sizeof(myElemType) );    

       然后我们想要在链表上面附加myElemType类型的数据,可以直接调用ListAppend(mylist ,a);

     //a是一个myElemType类型数据,

       这里的ListAppend第二个参数之所以能接受任意参数是因为它的声明形式是

       int ListAppend(List * plist,...)

       我们在声明的时候已经指示了,除第一个参数外,后面的参数可以是任意类型的,

       下面分析一下在执行这条语句执行后参数在栈空间里面的存储形式,首先入栈的是plist这个指针,存储这个指针位置是下面就是我们1个myElemType类型的数据(大小为Nodesize,初始化链表的时候确定)。

 

 

下面以ListAppend为列子,分析的思想:

           已经知道了节点的内存示意(图一)和调用ListAppend之后栈空间的内存分布(图二) 

 

              

        图一                                                                                    图二

 

             那么首先我们就是要为节点本身分配一款内存(存储两个指针next和data),然后我们还要为data这个指针分配额一块内存,用来存储数据,也就是上面图一蓝色的部分。然后再将此时已经在栈里面的数据通过memcpy函数拷贝到节点指针data所指向的内存(图中的三个红色箭头)。关于memcpy函数:

       void *memcpy( void *dest, const void *src, size_tcount);

       就是从指针src所指向地址拷贝count个字节到另一个指针所dest指向的地址,拷贝个数由参数size确定,我们这里当然可以传入我们在初始化链表的时候给予的数据字节大小的plist->Nodesize;

实际操作如下的:

 

ListAppend函数代码:

[cpp] view plaincopyprint?
  1. int ListAppend(List * plist,...)  
  2. {  
  3.     ChainNode * newpt = 0;  
  4.     void * data;  
  5.     void * pos;  
  6.     pos = &plist + 1;           //这个时候pos指向的实际就是在栈里面的一个myElemType类型数据的首地址  
  7.     if( !(plist && plist->head) )        return 0;   //防止指针为空  
  8.   
  9.     data = (void *)malloc(plist->Nodesize);      //为直接的数据域指针分配空间,大小是(当前Nodesize)  
  10.     if( !data )     return 0;                   //防止指针为空  
  11.   
  12.     memcpy(data,pos,plist->Nodesize);        //将栈里面的一个myElemType类型数据拷贝到data指针所指向的内存  
  13.   
  14.     newpt =  NewChainNode(data);  
  15.     if( !newpt )        return 0;               //防止指针为空  
  16.   
  17.     plist->tail->next = newpt;                //将新建的节点添加到链表最后面  
  18.     plist->tail = newpt;  
  19.     return 1;  
  20. }  
int ListAppend(List * plist,...){ChainNode * newpt = 0;void * data;void * pos;pos = &plist + 1;//这个时候pos指向的实际就是在栈里面的一个myElemType类型数据的首地址if( !(plist && plist->head) )return 0;//防止指针为空data = (void *)malloc(plist->Nodesize);//为直接的数据域指针分配空间,大小是(当前Nodesize)if( !data )return 0;//防止指针为空memcpy(data,pos,plist->Nodesize);//将栈里面的一个myElemType类型数据拷贝到data指针所指向的内存newpt =  NewChainNode(data);if( !newpt )return 0;//防止指针为空plist->tail->next = newpt;//将新建的节点添加到链表最后面plist->tail = newpt;return 1;}


 

 

 


 代码如下

list.h
[cpp] view plaincopyprint?
  1. typedef  void * ElemType;  
  2.   
  3.   
  4. typedef struct node  
  5. {  
  6.     ElemType data;  
  7.     struct node * next;  
  8.   
  9. }ChainNode;  
  10.   
  11. typedef struct  
  12. {  
  13.     ChainNode *head;  
  14.   
  15.     int Nodesize;  
  16.   
  17.     ChainNode *tail;  
  18. }List;  
  19.   
  20. List * CreateList(int); /* 创建链表 */  
  21.   
  22. void DestoryList(List*);    /* 销毁链表 */  
  23.   
  24. void ClearList(List*);      /* 清空链表 */  
  25.   
  26. int ListAppend(List*,...);      /* 追加元素 */  
  27.    
  28. int ListInsert(List*,int,...);      /* 加入元素 */  
  29.    
  30. int ListDelete(List *,int);     /* 删除第几个元素 */  
  31.   
  32. int GetElem(List*,int,ElemType *);  /* 取得第几个元素的值用第三个参数返回 */  
  33.   
  34. ChainNode * GetAddr(List *, int);   /* 取得编号为N的元素所在地址 */  
  35.   
  36. int TraverseList(List*,int (*)(ElemType )); /* 遍历访问,反问某个节点元素用函数处理 */  
  37.   
  38. ChainNode * NewChainNode( ElemType);  
  39.   
  40. List *CreateList(int size )  
  41. {  
  42.     List * pt = 0;  
  43.     ElemType data = 0;  
  44.     pt=(List*)malloc( sizeof(List) );  
  45.       
  46.     if( !pt )  
  47.         return 0;  
  48.     pt->head = NewChainNode(data );  
  49.     if( ! pt->head )  
  50.     {  
  51.         free(pt);  
  52.         return 0;  
  53.     }  
  54.   
  55.     pt->Nodesize = size;  
  56.   
  57.     pt->tail = pt->head;  
  58.     return pt;   
  59. }  
  60. /*==================*/  
  61. void DestoryList(List * plist)  
  62. {  
  63.     ClearList(plist);  
  64.       
  65.     free(plist->head);  
  66.     plist->head = 0;  
  67.    
  68.     free(plist);  
  69.     plist = 0;  
  70.       
  71. }  
  72.   
  73. /*==================*/  
  74.   
  75. int ListAppend(List * plist,...)  
  76. {  
  77.     ChainNode * newpt = 0;  
  78.     void * data;  
  79.     void * pos;  
  80.     pos = &plist + 1;  
  81.   
  82.     if( !(plist && plist->head) )  
  83.         return 0;  
  84.   
  85.     data = (void *)malloc(plist->Nodesize);  
  86.     if( !data )  
  87.         return 0;  
  88.   
  89.     memcpy(data,pos,plist->Nodesize);  
  90.     newpt =  NewChainNode(data);  
  91.   
  92.     if( !newpt )  
  93.         return 0;  
  94.           
  95.     plist->tail->next = newpt;  
  96.     plist->tail = newpt;  
  97.     return 1;  
  98. }  
  99.   
  100. /*==================*/  
  101. int ListInsert(List * plist, int n ,...)  
  102. {  
  103.     ChainNode * pt = 0;  
  104.     ChainNode * newpt = 0;  
  105.     void * data;  
  106.     void *pos = &plist + 2;  
  107.     pt = GetAddr( plist, n-1 );  
  108.     if( !(pt) )  
  109.         return 0;  
  110.     data = (void*)malloc(plist->Nodesize);  
  111.           
  112.     if( !data )  
  113.         return 0;  
  114.     memcpy(data,pos,plist->Nodesize);  
  115.   
  116.     newpt = NewChainNode(data);  
  117.     if( !newpt )  
  118.         return 0;  
  119.   
  120.     if ( pt->next == plist->tail )  
  121.         plist->tail = newpt;  
  122.   
  123.     newpt->next = pt->next;  
  124.   
  125.     pt->next = newpt;  
  126.   
  127.     return 1;  
  128.   
  129. }  
  130.   
  131. /*==================*/  
  132. int GetElem(List * plist,int n,ElemType *data)  
  133. {  
  134.     ChainNode * pt = 0;   
  135.   
  136.   
  137.     if( !data )  
  138.         return 0;  
  139.   
  140.     pt = GetAddr(plist,n);  
  141.     if( ! pt )  
  142.         return 0;     
  143.   
  144.     memcpy(data, pt->data ,plist->Nodesize);        
  145.   
  146.     return 1;   
  147. }  
  148.   
  149. /*==================*/  
  150. int TraverseList(List* plist,int (*f)(ElemType ))  
  151. {  
  152.     ChainNode * pt = 0;  
  153.     int a=0;  
  154.       
  155. /**/  
  156.     if( !(plist && plist->head) )  
  157.         return 0;  
  158.     for( a = 0 ,pt = plist->head->next; pt ; pt = pt->next )  
  159.     {  
  160.         if( ! f( (pt->data)) )  
  161.             return a+1;  
  162.         a++;  
  163.     }  
  164.     return 0;  
  165. }  
  166.   
  167. /*==================*/  
  168. void ClearList(List * plist)  
  169. {  
  170.     while ( ListDelete(plist,1) );  
  171. }  
  172.   
  173. /*==================*/  
  174. int ListDelete( List * plist,  int n )  
  175. {  
  176.     ChainNode * pt =0;  
  177.     ChainNode * pf=0;  
  178.       
  179.     if( !plist->head->next )  
  180.         return 0;  
  181.   
  182.     pt = GetAddr( plist,n-1 );  
  183.   
  184.     if ( pt->next == plist->tail )  
  185.         plist->tail = pt;  
  186.   
  187.     if( !(pt && pt->next ))  
  188.         return 0;  
  189.       
  190.     pf = pt->next;  
  191.     pt->next = pt->next->next;  
  192.       
  193.     free(pf->data);  
  194.     free(pf);  
  195.       
  196.     return 1;  
  197.       
  198. }  
  199.   
  200. ChainNode * GetAddr(List * plist,int n)  
  201. {  
  202.     ChainNode * pt = 0;  
  203.     int a = 0;  
  204.       
  205.     if( n < 0)   return 0;  
  206.       
  207.     pt = plist->head;  
  208.       
  209.     while( pt && a < n )  
  210.     {  
  211.         pt = pt->next;  
  212.         a++;  
  213.     }  
  214.     return pt;  
  215. }  
  216.   
  217. /*==================*/  
  218. ChainNode * NewChainNode(ElemType data)  
  219. {  
  220.     ChainNode * pChain=0;  
  221.     pChain = ( ChainNode * )malloc( sizeof(ChainNode) );  
  222.       
  223.     if( ! pChain )  return 0;  
  224.   
  225.     pChain->data=data;  
  226.     pChain->next=0;  
  227.       
  228.     return pChain;  
  229. }  
typedef  void * ElemType;typedef struct node{ElemType data;struct node * next;}ChainNode;typedef struct{ChainNode *head;int Nodesize;ChainNode *tail;}List;List * CreateList(int);/* 创建链表 */void DestoryList(List*);/* 销毁链表 */void ClearList(List*);/* 清空链表 */int ListAppend(List*,...);/* 追加元素 */ int ListInsert(List*,int,...);/* 加入元素 */ int ListDelete(List *,int);/* 删除第几个元素 */int GetElem(List*,int,ElemType *);/* 取得第几个元素的值用第三个参数返回 */ChainNode * GetAddr(List *, int);/* 取得编号为N的元素所在地址 */int TraverseList(List*,int (*)(ElemType ));/* 遍历访问,反问某个节点元素用函数处理 */ChainNode * NewChainNode( ElemType);List *CreateList(int size ){List * pt = 0;ElemType data = 0;pt=(List*)malloc( sizeof(List) );if( !pt )return 0;pt->head = NewChainNode(data );if( ! pt->head ){free(pt);return 0;}pt->Nodesize = size;pt->tail = pt->head;return pt; }/*==================*/void DestoryList(List * plist){ClearList(plist);free(plist->head);plist->head = 0; free(plist);plist = 0;}/*==================*/int ListAppend(List * plist,...){ChainNode * newpt = 0;void * data;void * pos;pos = &plist + 1;if( !(plist && plist->head) )return 0;data = (void *)malloc(plist->Nodesize);if( !data )return 0;memcpy(data,pos,plist->Nodesize);newpt =  NewChainNode(data);if( !newpt )return 0;plist->tail->next = newpt;plist->tail = newpt;return 1;}/*==================*/int ListInsert(List * plist, int n ,...){ChainNode * pt = 0;ChainNode * newpt = 0;void * data;void *pos = &plist + 2;pt = GetAddr( plist, n-1 );if( !(pt) )return 0;data = (void*)malloc(plist->Nodesize);if( !data )return 0;memcpy(data,pos,plist->Nodesize);newpt = NewChainNode(data);if( !newpt )return 0;if ( pt->next == plist->tail )plist->tail = newpt;newpt->next = pt->next;pt->next = newpt;return 1;}/*==================*/int GetElem(List * plist,int n,ElemType *data){ChainNode * pt = 0;if( !data )return 0;pt = GetAddr(plist,n);if( ! pt )return 0;memcpy(data, pt->data ,plist->Nodesize);return 1; }/*==================*/int TraverseList(List* plist,int (*f)(ElemType )){ChainNode * pt = 0;int a=0;/**/if( !(plist && plist->head) )return 0;for( a = 0 ,pt = plist->head->next; pt ; pt = pt->next ){if( ! f( (pt->data)) )return a+1;a++;}return 0;}/*==================*/void ClearList(List * plist){while ( ListDelete(plist,1) );}/*==================*/int ListDelete( List * plist,  int n ){ChainNode * pt =0;ChainNode * pf=0; if( !plist->head->next )return 0;pt = GetAddr( plist,n-1 );if ( pt->next == plist->tail )plist->tail = pt;if( !(pt && pt->next ))return 0;pf = pt->next;pt->next = pt->next->next;free(pf->data);free(pf);return 1;}ChainNode * GetAddr(List * plist,int n){ChainNode * pt = 0;int a = 0;if( n < 0)return 0;pt = plist->head;while( pt && a < n ){pt = pt->next;a++;}return pt;}/*==================*/ChainNode * NewChainNode(ElemType data){ChainNode * pChain=0;pChain = ( ChainNode * )malloc( sizeof(ChainNode) );if( ! pChain )return 0;pChain->data=data;pChain->next=0;return pChain;}


uselist.c

 

[cpp] view plaincopyprint?
  1. #include "list.h"   
  2. /*提供两种数据测试*/  
  3. typedef   struct {char ch ; int id; char name[10]; int r;} myElemType;  
  4.   
  5. /*  
  6. typedef  char myElemType; 
  7. */  
  8.    
  9. myElemType a[20] ={{'a',1,"niei",2},{'b',2,"aini",2},{'c',3,"love",2},{'d',4,"jack",2},{'e',5,"alice",2},{'f',6,"ben",2},{'g',7,"carlo",2},{'h',8,"mason",2}};  
  10.   
  11. /* 
  12. myElemType a[20]="Hello world!"; 
  13. */  
  14.   
  15. void showList(List* );  
  16.   
  17. int putElem( myElemType *);  
  18. void main()  
  19. {  
  20.     List * mylist;  
  21.     int n=0;  
  22.     myElemType data;  
  23.     myElemType data2;  
  24.   
  25.     myElemType* pdata;    
  26.     mylist = CreateList( sizeof(myElemType) );  
  27.    
  28.     if( ! mylist)  
  29.     {     
  30.         printf("error");  
  31.         return;  
  32.     }  
  33.     for( n = 0 ;n < 8 ;n++)  
  34.         ListAppend(mylist ,a[n]);  
  35.   
  36.     showList( mylist);  
  37.       
  38.     data.ch = '*';  
  39.     data.id = 8;  
  40.     strcpy(data.name , "1223");  
  41.     data.r = 2;  
  42.       
  43.   
  44. /*  a[0]='E'; 
  45.     a[1]='r'; 
  46.     a[2]='r'; 
  47.     a[3]='o'; 
  48.     a[4]='r'; 
  49. */  ListInsert(mylist,1,data);  
  50.     showList( mylist);  
  51.   
  52. /**/    data2.ch = 'A';  
  53.     data2.id = 54;  
  54.     strcpy(data2.name , "bill");  
  55.     data2.r = 4;  
  56.     ListInsert(mylist,7,data2);  
  57.     showList( mylist);  
  58.   
  59.     ListDelete(mylist,7);  
  60.     showList( mylist);  
  61.       
  62.   
  63.     ListDelete(mylist,1);  
  64.     showList( mylist);  
  65.   
  66.     if (GetElem(mylist,5,&data2) )  
  67.     /*  printf("[%c %d %s %d] ",data2.ch,data2.id,data2.name,data2.r);*/  
  68.         printf("[%c]",data2);  
  69.       
  70.     ClearList(mylist);  
  71.     showList( mylist);  
  72.   
  73.     DestoryList(mylist);  
  74.     mylist = 0;  
  75.   
  76.     showList( mylist);  
  77.   
  78. }  
  79. /*==================*/  
  80. void showList(List* plist)  
  81. {  
  82.     if( !plist  )  
  83.         return;  
  84.     TraverseList(plist,(int(*)(void *))putElem);   
  85.     printf("\n");  
  86. }  
  87. /*输出字符*/  
  88. /* 
  89. int putElem(myElemType *data) 
  90. { 
  91.   
  92.     if( ! ( data) ) 
  93.         return 0; 
  94.   
  95.     printf("%c",*data); 
  96.     return 1; 
  97. } 
  98. */  
  99. /*输出结构体*/  
  100. /**/  
  101. int putElem(myElemType *data)  
  102. {  
  103.   
  104.     if( ! ( data) )  
  105.         return 0;  
  106.     printf("[%c %d %s %d] ",(data)->ch,(data)->id,(data)->name,(data)->r);   
  107.    
  108.     return 1;  
  109. }  
#include "list.h"/*提供两种数据测试*/typedef   struct {char ch ; int id; char name[10]; int r;} myElemType;/* typedef  char myElemType;*/ myElemType a[20] ={{'a',1,"niei",2},{'b',2,"aini",2},{'c',3,"love",2},{'d',4,"jack",2},{'e',5,"alice",2},{'f',6,"ben",2},{'g',7,"carlo",2},{'h',8,"mason",2}};/*myElemType a[20]="Hello world!";*/void showList(List* );int putElem( myElemType *);void main(){List * mylist;int n=0;myElemType data;myElemType data2;myElemType* pdata;mylist = CreateList( sizeof(myElemType) ); if( ! mylist){printf("error");return;}for( n = 0 ;n < 8 ;n++)ListAppend(mylist ,a[n]);showList( mylist);data.ch = '*';data.id = 8;strcpy(data.name , "1223");data.r = 2;/*a[0]='E';a[1]='r';a[2]='r';a[3]='o';a[4]='r';*/ListInsert(mylist,1,data);showList( mylist);/**/data2.ch = 'A';data2.id = 54;strcpy(data2.name , "bill");data2.r = 4;ListInsert(mylist,7,data2);showList( mylist);ListDelete(mylist,7);showList( mylist);ListDelete(mylist,1);showList( mylist);if (GetElem(mylist,5,&data2) )/*printf("[%c %d %s %d] ",data2.ch,data2.id,data2.name,data2.r);*/printf("[%c]",data2);ClearList(mylist);showList( mylist);DestoryList(mylist);mylist = 0;showList( mylist);}/*==================*/void showList(List* plist){if( !plist  )return;TraverseList(plist,(int(*)(void *))putElem); printf("\n");}/*输出字符*//*int putElem(myElemType *data){ if( ! ( data) )return 0; printf("%c",*data);return 1;}*//*输出结构体*//**/int putElem(myElemType *data){if( ! ( data) )return 0;printf("[%c %d %s %d] ",(data)->ch,(data)->id,(data)->name,(data)->r);  return 1;}


 

最后的运行结果,一个[ ] 表示一个结构体所有成员


转:http://blog.csdn.net/lilien1010/article/details/7786164

更多0
原创粉丝点击