目前只完成了打印和插入。
思路借鉴了库函数qsort,由用户程序提供比较和具体的打印函数,List函数执行具体的操作,插入,删除,打印,遍历等等等。
其他几个函数,几乎已经没有难度了,随便写写了。

程序代码:
//测试:
#include <stdio.h>
#include "GenericityList.h"
int
COM( void *a, void *b );
void
PRINT( void * );
struct a
{
int a;
double b;
char c;
};
int
main( void )
{
List Root;
struct a b;
int ix;
Root = NULL;
for( ix = 0; ix < 20; ++ix )
{
b.a = 'a' + ix;
b.b = 'a' + ix;
b.c = 'a' - ix;
Insert( &Root, &b, sizeof( struct a ), COM );
}
Print( Root, PRINT );
return 0;
}
int
COM( void *a, void *b )
{
return 1;
}
void
PRINT( void *a )
{
printf("%d %c %lf",( int )( ( ( struct a * )a )->a ),( char )( ( ( struct a * )a )->b ),( double )( ( ( struct a * )a )->c ) );
}

程序代码:
//接口:
#ifndef _GENERICITY_LIST_
#define _GENERICITY_LIST_
struct List{
void *Element;
struct List *Link;
};
typedef struct List *List;
void
Insert( List *RootPP, void *Value, int size , int (*compare)( void *, void * ) );
void
Print( List RootP, void (*)( void * ) );
#endif

程序代码:
//实现:
#include "GenericityList.h"
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>
void
Insert( List *RootPP, void *Value, int size , int (*compare)( void *, void * ) )
{
List Next;
List NewCell;
while( NULL != ( Next = *RootPP ) && 0 < compare( Next->Element, Value ) )
RootPP = &Next->Link;
NewCell = ( List )malloc( sizeof( struct List ) );
assert( NULL != NewCell );
NewCell->Element = malloc( size );
assert( NULL != NewCell->Element );
memmove( NewCell->Element, Value, size );
NewCell->Link = Next;
*RootPP = NewCell;
}
void
Print( List RootP, void (*print)( void * ) )
{
while( NULL != RootP )
{
print( RootP->Element );
RootP = RootP->Link;
printf( "\n" );
}
}
[此贴子已经被作者于2017-4-24 23:43编辑过]