list head select,update,insert

目录 未分类


list_test_code.rar

#include <stdio.h>
#include "list.h"
#define DEBUG_PRINT printf

/*video link infomation*/
typedef struct
{
struct list_head p;
char quality[24]; /*video quality: hd720/medium/small*/
char type[36];
char *link; /*video link*/
} video_link;

static struct list_head video_list;

int add_videolist(struct video_link *video_list)
{
static int i = 0;
video_link *vl = (video_link*)malloc(sizeof(video_link));
memset(vl, 0, sizeof(video_link));
sprintf(vl->quality,”%d”,++i);
if (i%3 == 0)
{
strcpy(vl->type,”*TEST*”);
}
else
{
strcpy(vl->type,”#####”);
}
list_add_tail(&vl->p, video_list);

return 1;
}

int delete_videolist(video_link *vl, int cnt)
{
int i = 0;
struct list_head *pos = NULL;
struct list_head *n = NULL;
video_link *entry = NULL;

if (vl == NULL) { return 0; }
DEBUG_PRINT(“Delete:\n” );
DEBUG_PRINT(“====================================\n” );
if (!list_empty(&vl->p))
{
list_for_each_safe(pos, n, &vl->p)
{
entry = list_entry(pos, video_link, p);
if (cnt > 0 && ++i == cnt)
{
if (entry && cnt)
{
list_del(&entry->p);
free(entry);
DEBUG_PRINT(“[%s-%d]i:%d\n”,__FUNCTION__,__LINE__,i);
}
break;
}
}
}

DEBUG_PRINT(“\n” );
return 1;
}

int update_videolist(video_link *vl, int ipos, char *quality)
{
int i = 0;
struct list_head *pos = NULL;
struct list_head *n = NULL;
video_link *entry = NULL;

if (vl == NULL) { return 0; }
DEBUG_PRINT(“Update:\n” );
DEBUG_PRINT(“====================================\n” );
if (!list_empty(&vl->p))
{
list_for_each_safe(pos, n, &vl->p)
{
entry = list_entry(pos, video_link, p);
if (ipos > 0 && ++i == ipos)
{
if (entry && ipos)
{
memset(entry->quality, 0, 36);
strcpy(entry->quality, quality);
DEBUG_PRINT(“[%s-%d]i:%d\n”,__FUNCTION__,__LINE__,i);
}
break;
}
}
}

DEBUG_PRINT(“\n” );
return 1;
}

int select_videolist(video_link *vl, char *type)
{
int i = 0;
struct list_head *pos = NULL;
struct list_head *n = NULL;
video_link *entry = NULL;

if (vl == NULL) { return 0; }
DEBUG_PRINT(“Select:%s\n”, type);
DEBUG_PRINT(“====================================\n” );
if (!list_empty(&vl->p))
{
list_for_each_safe(pos, n, &vl->p)
{
entry = list_entry(pos, video_link, p);
if (entry)
{
if (strstr(entry->type, type))
{
DEBUG_PRINT(“[%s-%d]quality:%s type:%s\n”,__FUNCTION__,__LINE__,entry->quality,entry->type);
}
}
}
}

DEBUG_PRINT(“\n” );
return 1;
}

int show_videolist(video_link *vl)
{
int i = 0;
struct list_head *pos = NULL;
struct list_head *n = NULL;
video_link *entry = NULL;

if (vl == NULL) { return 0; }
DEBUG_PRINT(“Show all:\n” );
DEBUG_PRINT(“====================================\n” );
if (!list_empty(&vl->p))
{
list_for_each_safe(pos, n, &vl->p)
{
entry = list_entry(pos, video_link, p);
DEBUG_PRINT(“[%s-%d]quality:%s type:%s\n”,__FUNCTION__,__LINE__,entry->quality, entry->type);
}
}

DEBUG_PRINT(“\n” );
return 1;
}

void main(void)
{

INIT_LIST_HEAD(&video_list);
for (int i = 0; i<10; i++) add_videolist(&video_list);
show_videolist(&video_list);
delete_videolist(&video_list,5);
update_videolist(&video_list, 8, “update OK”);
select_videolist(&video_list,”TEST”);
show_videolist(&video_list);

}