Chinaunix首页 | 论坛 | 博客
  • 博客访问: 246172
  • 博文数量: 35
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 273
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-27 23:34
个人简介

To follow the path,look to the master,follow the master.

文章分类

全部博文(35)

文章存档

2019年(1)

2018年(1)

2017年(1)

2016年(8)

2015年(24)

分类: C/C++

2015-03-19 23:09:51


此部分为包含头文件 "chain.h"

  1. #ifndef CHAIN_H
  2. #define CHAIN_H
  3. #include "../apue.h"
  4. //指针域
  5. struct list_head     
  6. {
  7.     struct list_head *pre;
  8.     struct list_head *next;
  9. };

  10. //节点
  11. struct node
  12. {
  13.     struct in_addr addr;//数据域
  14.     struct list_head list;
  15. };

  16. #endif

写宏 实现 步骤

  1. 1 #include <stdio.h>
  2.   2
  3.   3 struct node
  4.   4 {
  5.   5 int date;
  6.   6 int age;
  7.   7 };
  8.   8
  9.   9 int main()
  10.  10 {
  11.  11 struct node model;
  12.  12 struct node *pnode;
  13.  13 pnode =&model;
  14.  14 pnode->date=3;
  15.  15 pnode->age=20;
  16.  16
  17.  17 #if 1
  18.  18 printf("%d\n", ((struct node *)((unsigned long )(&(pnode->age)-((unsigned long)&(((struct node *)0)->age))))->age);
  19.  19 
  20.  20 #endif
  21.  21
  22.  22 }




此部分为 chain.c 代码区

  1. #include "./chain.h"
  2. #include "../apue.h"
  3. //初始化节点
  4. void init_node(struct node **phead, int size, struct in_addr addr)
  5. {
  6.     *phead=malloc(size);
  7.     (*phead)->addr=addr;
  8.     (*phead)->list.pre =&(*phead)->list;
  9.     (*phead)->list.next=&(*phead)->list;
  10. }
  11. //向链表出入数据
  12. void insert_head_chain(struct list_head *pre, struct list_head *next, struct list_head *pnew)
  13. {
  14.     pnew->next=next;
  15.     pnew->pre=pre;
  16.     pre->next=pnew;
  17.     next->pre=pnew;
  18. }
  19. void insert_chain(struct list_head *phead, struct list_head *pnew)
  20. {
  21.       //insert_head_chain(phead, pnew, phead->next);
  22.       insert_head_chain(phead->pre, phead, pnew);
  23. }
  24. //创建链表,并向链表中插入数据
  25. void add_chain(struct node *phead, struct in_addr user_addr)
  26. {
  27.     int i=0;
  28.     struct node *pnew=NULL;
  29.   // for(i=0; i<num; i++)
  30.   // {
  31.         init_node(&pnew, sizeof(struct node), user_addr);
  32.         insert_chain(&phead->list, &pnew->list);
  33.   // }
  34. }
  35. #define list_entry(mem_addr, type, member) ((type *)(((unsigned long)mem_addr)-((unsigned long)&( ((type *)0)->member))))
  36. //遍历链表
  37. void show_chain(struct list_head *phead)
  38. {
  39.     struct node *p=NULL;
  40.     struct list_head *index=NULL;
  41.     for(index=phead->next; index!=phead; index=index->next)
  42.     {
  43.        p=list_entry(index, struct node, list);
  44.        printf("%d->", p->addr);
  45.     }
  46.     printf("在线好友\n");
  47.     return ;
  48. }
  49. //搜索好友
  50. //struct in_addr select_chain(struct list_head *phead, struct in_addr user_addr)
  51. in_addr_t select_chain(struct list_head *phead, struct in_addr user_addr)
  52. {
  53.     struct node *p=NULL;
  54.     struct list_head *index=NULL;
  55.     for(index=phead->next; index!=phead; index=index->next)
  56.     {
  57.        p=list_entry(index, struct node, list);
  58.        if(p->addr.s_addr==user_addr.s_addr)
  59.        {
  60.            return user_addr.s_addr;
  61.        }
  62.     }
  63.     return 0;
  64. }
  65. //修改链接
  66. void del_list(struct list_head *del)
  67. {
  68.     del->pre->next=del->next;
  69.     del->next->pre=del->pre;
  70. }

  71. //处理下线好友
  72. int delete_chain(struct list_head *phead, struct in_addr user_addr)
  73. {
  74.     struct node *p=NULL;
  75.     struct list_head *index=NULL;
  76.     for(index=phead->next; index!=phead; index=index->next)
  77.     {
  78.        p=list_entry(index, struct node, list);
  79.        if(p->addr.s_addr==user_addr.s_addr)
  80.        {
  81.             del_list(index);
  82.             free(p);
  83.             return 1;
  84.        }
  85.     }
  86.     return 0;
  87. }


阅读(1398) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:Linux多线程3-4_向线程发送信号

给主人留下些什么吧!~~