一直以来,工作也3年多了,链表这个东西感觉上是比较难的,之前都是做c++,qt什么的,也用不到,有现成的list,自己也是几乎用不到就不去写,现在用c开发,就很常见了,这个一定要学会的,下面是自己写的一个单项链表,还算比较顺手,特此记录,这里要说一下,网上的链表节点查找都是查找某一特定位置的节点信息函数,而不是根据某一值,获取其节点的函数,所以现在我自己做了改良
.c
void ftplist_init()
{
if( NULL == pause_list ){
pause_list = (Ftplist_p)malloc(sizeof(Ftplist_t));
pause_list->next = NULL;
}
else{
ftplist_clean_node(pause_list);
}
}
void ftplist_set_pause(char* fidOfilename,int type,int isTrans)
{
Ftplist_p tmpp ,n ;
n = pause_list;
while( NULL != n->next){
tmpp = n->next;
n = tmpp;
if( (tmpp->type == type) && (strcmp(tmpp->filename,fidOfilename) == 0) ){
tmpp->isTrans = isTrans;
break;
}
}
}
void ftplist_insert_node(Ftplist_t fileinfo)
{
Ftplist_p q ,n ,ne;
q = pause_list;
n = pause_list->next;
while( NULL != n){
q = n;
n = q->next;
}
ne = (Ftplist_p)malloc(sizeof(Ftplist_t));
memcpy(ne,&fileinfo,sizeof(fileinfo));
q->next = ne;
}
void ftplist_free_node(Ftplist_t fileinfo)
{
char *fid = NULL, *filename =NULL;
Ftplist_p tmpp ,n,pre ;
fid = fileinfo.fid;
filename = fileinfo.filename;
pre = pause_list;
n = pre;
while( NULL != n->next){
tmpp = n->next;
pre = n;
n = tmpp;
// printf("fid:%s,filename:%s,tmpp->fid:%s,tmpp->filename:%s\n",fid,filename,tmpp->fid,tmpp->filename);
if( (strcmp(tmpp->fid,fid) == 0) || (strcmp(tmpp->filename,filename) == 0) ){
pre->next = n->next;
free(tmpp);
break;
}
}
}
void ftplist_free_node_up(char* filename,int type)
{
Ftplist_p tmpp ,n,pre ;
pre = pause_list;
n = pre;
while( NULL != n->next){
tmpp = n->next;
pre = n;
n = tmpp;
// printf("fid:%s,filename:%s,tmpp->fid:%s,tmpp->filename:%s\n",fid,filename,tmpp->fid,tmpp->filename);
if( (tmpp->type == type) || (strcmp(tmpp->filename,filename) == 0) ){
pre->next = n->next;
free(tmpp);
break;
}
}
}
int ftplist_search_node(char* filename,int type)
{
int isTrans = 0;
Ftplist_p tmpp ,n ;
n = pause_list;
while( NULL != n->next){
tmpp = n->next;
n = tmpp;
if( (tmpp->type == type) && (strcmp(tmpp->filename,filename) == 0) ){
isTrans = tmpp->isTrans ;
break;
}
}
return isTrans;
}
int ftplist_isPause_node(char* filename,int type)
{
return ftplist_search_node(filename,type);
}
void ftplist_clean_node(Ftplist_p flist)
{
Ftplist_p n,q ;
n = flist;
while( NULL != n->next){
q = n->next;
n->next = q->next;
free(q);
}
}
void ftplist_print_node(Ftplist_p flist)
{
int i = 0;
Ftplist_p q ,n ;
q = flist;
n = flist->next;
while( NULL != n){
i++;
q = n;
n = q->next;
printf("第%d条记录:fid=%s,filename=%s,type=%d,isTrans=%d\n",
i,q->fid,q->filename,q->type,q->isTrans);
}
}
.h
typedef struct file_trans_pause_list{
char fid[25];
char filename[1024];
int type;
int isTrans;
struct file_trans_pause_list* next;
}Ftplist_t,*Ftplist_p;
extern Ftplist_p pause_list;
extern void ftplist_init();
/**
* @brief
*
* @param fidOfilename
* @param type 1上传 2下载
* @param isTrans 0暂停 1开始
*/
extern void ftplist_set_pause(char* fidOfilename,int type,int isTrans);
extern void ftplist_insert_node(Ftplist_t fileinfo);
extern void ftplist_free_node(Ftplist_t fileinfo);
extern void ftplist_free_node_up(char* filename,int type);
extern int ftplist_search_node(char* filename,int type);
/**
* @brief
*
* @param filename
* @return int 0暂停 1开始
*/
extern int ftplist_isPause_node(char* filename,int type);
extern void ftplist_clean_node(Ftplist_p flist);
extern void ftplist_print_node(Ftplist_p flist);
特此记录。