#include "stdio.h"
#include "string.h"
#include "stdlib.h"
typedef struct list
{
void *data;
struct list *next;
}List;
List* getList(int len,char* initData);
void freeList(List* phead);
int main()
{
List *hList,*pList,*pdel;
hList=getList(15,"123456");
pList=hList;
while(pList!=NULL)
{
printf("%s\n",(char *)pList->data);
pList=pList->next;
}
freeList(hList);
getchar();
return 0;
}
List* getList(int len,char* initData)
{
List *first,*pList,*item;
char *dd,no[2];
int i;
dd=(char *)malloc(10);
if(dd==NULL)
printf("malloc failed!");
strcpy(dd,"123456");
first=(List *)malloc(sizeof(List));
first->data=(void *)dd;
first->next=NULL;
pList=first;
for(i=1;i<len;i++)
{ item=(List *)malloc(sizeof(List));
dd=(char *)malloc(10);
strcpy(dd,"123456");
sprintf(no,":%d",i);
strcat(dd,no);
item->data=(void *)dd;
item->next=NULL;
pList->next=item;
pList=item;
}
return first;
}
void freeList(List* phead)
{
List *pList,*pdel;
pList=phead;
while(pList!=NULL)
{
free(pList->data);
pList->data=NULL;
pdel=pList;
pList=pList->next;
pdel->next=NULL;
free(pdel);
}
}