#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include<malloc.h>
#include <arpa/inet.h>
?
?//鏈表頭結構
?struct list_head
?{
? ? ?struct list_head *next,*prev;
?};
?
?//真正實現鏈表插入操作
?void _list_add(struct list_head *nnew,struct list_head *prev,struct list_head *next)
?{
? ? ?next->prev = nnew;
? ? ?nnew->next = next;
? ? ?nnew->prev = prev;
? ? ?prev->next = nnew;
?}
?void INIT_LIST_HEAD(struct list_head *list)
?{
? ? ?list->next = list;
? ? ?list->prev = list;
?}
?
?//向鏈表插入一個節點
?void list_add(struct list_head *nnew,struct list_head *head)
?{
? ? ?_list_add(nnew,head,head->next);
?}
?
?
?#define list_for_each(pos,head) \
? ? ?for(pos = (head)->next;pos != (head);pos = pos->next)
?
?#define list_for_each_safe(pos,n,head) \
? ? ?for(pos = (head)->next,n = pos->next;pos != (head);pos = n,n = pos->next)
?
?//根據節點中的一個成員在節點中的偏移量找到節點的起始地址
?#define list_entry(ptr,type,member) \
? ? ?((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
?
?//真正實現鏈表刪除操作
?void _list_del(struct list_head *prev,struct list_head *next)
?{
? ? ?next->prev = prev;
? ? ?prev->next = next;
?}
?
?{
? ? ?_list_del(entry->prev,entry->next);
? ? ?entry->next = NULL;
? ? ?entry->prev = NULL;
?}
#define SERVER_GROUP_NAME_SIZE 32
#define MAX_SERVER_GROUP_SIZE 128
typedef struct server_info {
? ? unsigned int status;
? ? ? ? union {
? ? ? ? struct in_addr addr;
? ? ? ? struct in6_addr addr6;
? ? };
}server_info_t;
typedef struct server_group?
{
struct list_head list;
char ? ? ? ? ? ? ? name[SERVER_GROUP_NAME_SIZE];
? ? server_info_t ? ? ? servers[MAX_SERVER_GROUP_SIZE];
? ? unsigned int ? ? ? ? ? count;
}ser_group_t;
ser_group_t * g_ser_t;
void list_init()
{
g_ser_t = (ser_group_t *)malloc(sizeof(ser_group_t));
INIT_LIST_HEAD(&g_ser_t->list);
}
/*insert link*/
void add_list(ser_group_t* server_new_data)
{
ser_group_t *listnode = NULL;
listnode = (ser_group_t *)malloc(sizeof(ser_group_t));
memset(listnode,0,sizeof(listnode));
memcpy(listnode,server_new_data,sizeof(ser_group_t));
list_add(&listnode->list,&g_ser_t->list);
}
void node_find(ser_group_t* sernode_find)
{
struct ser_group_t *listnode;
struct list_head *pos ;
struct ser_group_t *p = NULL;
struct list_head *t;
int i;
//遍歷鏈表
list_for_each(pos,&g_ser_t->list)
{
i--;
//找出一個節點
p = list_entry(pos,ser_group_t,list);
printf("Node %d's data: %d\n",i,p->count);
}
}
void node_delete(ser_group_t* sernode_del)
{
struct ser_group_t *listnode;
struct list_head *pos ;
struct ser_group_t *p;
struct list_head *t;
/*刪除鏈表*/
#if 0
list_for_each_safe(pos,t,&g_ser_t.list)
{
//刪除節點
list_del(pos);
p = list_entry(pos,struct ser_group_t,list);
//釋放節點的內存
free(p);
}
#endif
/*刪除一個節點*/
?list_for_each_safe(pos, t, &g_ser_t->list)?
? ? ? {?
? ? ? ? /*tmp = list_entry(pos, struct numlist, list);?
//節點相同,刪除
?if(tmp == sernode_del)?
? ? ? ? ? ? ? {?
? ? ? ? ? ? ? printf("node %d delete\n",tmp->num);
? ? ? ? ? ? ? ? ? ? list_del_init(pos);?
? ? ? ? ? ? ? ? ? ? free(tmp);?
? ? ? ? ? ? ? }?
*/
? ? ? ?}
}
int main()
{
ser_group_t test = {0};
test.count = 1;
list_init();
add_list(&test);
node_find(&test);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include<malloc.h>
#include <arpa/inet.h>
?
?//鏈表頭結構
?struct list_head
?{
? ? ?struct list_head *next,*prev;
?};
?
?//真正實現鏈表插入操作
?void _list_add(struct list_head *nnew,struct list_head *prev,struct list_head *next)
?{
? ? ?next->prev = nnew;
? ? ?nnew->next = next;
? ? ?nnew->prev = prev;
? ? ?prev->next = nnew;
?}
?void INIT_LIST_HEAD(struct list_head *list)
?{
? ? ?list->next = list;
? ? ?list->prev = list;
?}
?
?//向鏈表插入一個節點
?void list_add(struct list_head *nnew,struct list_head *head)
?{
? ? ?_list_add(nnew,head,head->next);
?}
?
?
?#define list_for_each(pos,head) \
? ? ?for(pos = (head)->next;pos != (head);pos = pos->next)
?
?#define list_for_each_safe(pos,n,head) \
? ? ?for(pos = (head)->next,n = pos->next;pos != (head);pos = n,n = pos->next)
?
?//根據節點中的一個成員在節點中的偏移量找到節點的起始地址
?#define list_entry(ptr,type,member) \
? ? ?((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
?
?//真正實現鏈表刪除操作
?void _list_del(struct list_head *prev,struct list_head *next)
?{
? ? ?next->prev = prev;
? ? ?prev->next = next;
?}
?
?//刪除鏈表中的一個節點
刪除節點后,內核原始操作會將兩個指針指向自己,這個在實際運用的時候可能會出錯,新分配的內存正好分陪到之前釋放的節點的內存,會使得新的節點自己指向自己,導致死循環,
?void list_del(struct list_head *entry)?{
? ? ?_list_del(entry->prev,entry->next);
? ? ?entry->next = NULL;
? ? ?entry->prev = NULL;
?}
#define SERVER_GROUP_NAME_SIZE 32
#define MAX_SERVER_GROUP_SIZE 128
typedef struct server_info {
? ? unsigned int status;
? ? ? ? union {
? ? ? ? struct in_addr addr;
? ? ? ? struct in6_addr addr6;
? ? };
}server_info_t;
typedef struct server_group?
{
struct list_head list;
char ? ? ? ? ? ? ? name[SERVER_GROUP_NAME_SIZE];
? ? server_info_t ? ? ? servers[MAX_SERVER_GROUP_SIZE];
? ? unsigned int ? ? ? ? ? count;
}ser_group_t;
ser_group_t * g_ser_t;
void list_init()
{
g_ser_t = (ser_group_t *)malloc(sizeof(ser_group_t));
INIT_LIST_HEAD(&g_ser_t->list);
}
/*insert link*/
void add_list(ser_group_t* server_new_data)
{
ser_group_t *listnode = NULL;
listnode = (ser_group_t *)malloc(sizeof(ser_group_t));
memset(listnode,0,sizeof(listnode));
memcpy(listnode,server_new_data,sizeof(ser_group_t));
list_add(&listnode->list,&g_ser_t->list);
}
void node_find(ser_group_t* sernode_find)
{
struct ser_group_t *listnode;
struct list_head *pos ;
struct ser_group_t *p = NULL;
struct list_head *t;
int i;
//遍歷鏈表
list_for_each(pos,&g_ser_t->list)
{
i--;
//找出一個節點
p = list_entry(pos,ser_group_t,list);
printf("Node %d's data: %d\n",i,p->count);
}
}
void node_delete(ser_group_t* sernode_del)
{
struct ser_group_t *listnode;
struct list_head *pos ;
struct ser_group_t *p;
struct list_head *t;
struct ser_group_t *tmp = NULL;
/*刪除鏈表*/
#if 0
list_for_each_safe(pos,t,&g_ser_t.list)
{
//刪除節點
list_del(pos);
p = list_entry(pos,struct ser_group_t,list);
//釋放節點的內存
free(p);
}
#endif
/*刪除一個節點*/
?list_for_each_safe(pos, t, &g_ser_t->list)?
? ? ? {?
? ? ? ? /*tmp = list_entry(pos, struct numlist, list);?
//節點相同,刪除
?if(tmp == sernode_del)?
? ? ? ? ? ? ? {?
? ? ? ? ? ? ? printf("node %d delete\n",tmp->num);
? ? ? ? ? ? ? ? ? ? list_del_init(pos);?
? ? ? ? ? ? ? ? ? ? free(tmp);?
? ? ? ? ? ? ? }?
*/
? ? ? ?}
}
int main()
{
ser_group_t test = {0};
test.count = 1;
list_init();
add_list(&test);
node_find(&test);
return 0;
}