C语言通用数据类型链表的构造,(数据域为指针)
和一般的数据结构里面的链表的实现没什么大不同,
在list.h里面只修改一个地方
typedef void * ElemType;
也就是说数据域是一个无类型指针,链表本身不对这个指针有数据访问,在使用链表的时候我们给一个有类型的指针,在操作的时候编译器有规律可循了,接下来只要链表数据访问的函数了,因为数据域是一个指针,因为没有修改TraverseList函数,那么给函数指针传递的一个指向指针的指针,所以修改遍历数据域访问函数如下
int TraverseList(List*,int (*)(ElemType *)); /* 遍历访问,反问某个节点元素用函数处理 */
list.htypedef void * ElemType;
typedef struct node
{
ElemType data;
struct node * next;
}ChainNode;
typedef struct
{
ChainNode *head;
int size;
ChainNode *tail;
}List;
List * CreateList(void);/* 创建链表 */
void DestoryList(List*);/* 销毁链表 */
void ClearList(List*);/* 清空链表 */
int ListAppend(List*,ElemType);/* 追加元素 */
int ListInsert(List*,int,ElemType);/* 加入元素 */
int ListDelete(List *,int);/* 删除第几个元素 */
int GetElem(List*,int,ElemType *);/* 取得第几个元素的值用第三个参数返回 */
ChainNode * GetAddr(List *, int);/* 取得编号为N的元素所在地址 */
int TraverseList(List*,int (*)(ElemType *));/* 遍历访问,反问某个节点元素用函数处理 */
ChainNode * NewChainNode( ElemType);
list.c
#include "list.h"
/*==================*/
/*==================*/
List *CreateList()
{
List * pt = 0;
ElemType data;
pt=(List*)malloc( sizeof(List) );
if( !pt )
return 0;
pt->head = NewChainNode(data );
if( ! pt->head )
{
free(pt);
return 0;
}
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,ElemType data)
{
ChainNode * pt = 0;
ChainNode * newpt = 0;
if( !(plist && plist->head) )
return 0;
newpt = NewChainNode(data);
if( !newpt )
return 0;
plist->tail->next = newpt;
plist->tail = newpt;
return 1;
}
/*==================*/
int ListInsert(List * plist, int n ,ElemType data)
{
ChainNode * pt = 0;
ChainNode * newpt = 0;
pt = GetAddr( plist, n-1 );
if( !(pt) )
return 0;
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;
pt = GetAddr(plist,n);
if( ! pt )
return 0;
*data = pt->data;
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);
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
#include "list.h"
typedef struct {char ch ; int id; char name[10]; int r;} 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}};
void showList(List* plist);
int putElem(ElemType *data);
void main()
{
/**/List * mylist;
int n=0;
myElemType data;
myElemType data2;
myElemType* pdata;
mylist = CreateList();
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 = 54;
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,&pdata) )
printf("[%c %d %s %d] ",pdata->ch,pdata->id,pdata->name,pdata->r);
ClearList(mylist);
showList( mylist);
DestoryList(mylist);
mylist = 0;
showList( mylist);
}
/*==================*/
void showList(List* plist)
{
if( !plist )
return;
TraverseList(plist,putElem);
printf("\n");
}
int putElem(myElemType **data)
{
if( !data )
return 0;
printf("[%c %d %s %d] ",(*data)->ch,(*data)->id,(*data)->name,(*data)->r);
return 1;
}
最后的运行结果,一个[ ] 表示一个结构体所有成员