免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1918 | 回复: 2
打印 上一主题 下一主题

[C++] c/c++链表问题 [复制链接]

论坛徽章:
5
金牛座
日期:2015-07-03 13:32:00卯兔
日期:2015-07-03 13:32:17程序设计版块每日发帖之星
日期:2015-11-29 06:20:0015-16赛季CBA联赛之同曦
日期:2015-12-15 09:36:06CU十四周年纪念徽章
日期:2016-07-06 17:18:48
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-05-18 22:58 |只看该作者 |倒序浏览
本帖最后由 seanking1987 于 2015-05-18 22:58 编辑

想写一个链表,用list去链入不同struct的练习。
代码如下:
list.h:
  1. #ifndef __LIST_H_
  2. #define __LIST_H_

  3. typedef struct node {
  4.         struct node* prev;
  5.         struct node* next;
  6. }NODE;

  7. typedef struct cat {
  8.         NODE* node;
  9.         int age;
  10.         char name[20];
  11. }CAT;

  12. typedef struct dog {
  13.         NODE* node;
  14.         int age;
  15.         char name[20];
  16.         int height;
  17. }DOG;

  18. typedef struct list {
  19.         NODE node;
  20.         int data;
  21. }LIST;

  22. #endif //__LIST_H_
复制代码
list.c:
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "list.h"

  5. void lstinit(LIST *list)
  6. {
  7.         list->data = 0;
  8.         list->node.prev = NULL;
  9.         list->node.next = NULL;
  10. }

  11. void listadd(LIST *list,NODE* pnode)
  12. {
  13.         NODE *tail = list->node.prev;
  14.         pnode->prev = tail;
  15.         pnode->next = NULL;
  16.         if (tail)
  17.                 tail->next = pnode;
  18.         else
  19.                 list->node.next = pnode;
  20.         list->node.prev = pnode;
  21. }

  22. void findage(LIST *list,int age)
  23. {
  24.         NODE *node = list->node.next;
  25.         printf("%p\n",list->node.next);
  26.         if(node)
  27.         {
  28.                 CAT *cat = (CAT *)node;
  29.                 if (cat->age == age)
  30.                 {
  31.                         printf("the %d age animal is cat:%s\n",cat->age,cat->name);
  32.                         return;
  33.                 }
  34.                 DOG *dog = (DOG *)node;
  35.                 if (dog->age == age)
  36.                 {
  37.                         printf("the %d age animal is cat:%s\n",dog->age,dog->name);
  38.                         return;
  39.                 }
  40.                 printf("no %d age animal\n",age);
  41.         }
  42. }


  43. int main(int argc,char *argv[])
  44. {
  45.         CAT *cat = (CAT *)malloc(sizeof(CAT));
  46.         DOG *dog = (DOG *)malloc(sizeof(DOG));
  47.         LIST *list = (LIST *)malloc(sizeof(LIST));
  48.         cat->age = 2;
  49.         strcpy(cat->name,"maomi");
  50.         dog->age = 3;
  51.         strcpy(dog->name,"dahuang");
  52.         dog->height = 80;
  53.         printf("%d:%s,%d:%s\n",cat->age,cat->name,dog->age,dog->name);
  54.         lstinit(list);
  55.         listadd(list,(NODE *)cat);
  56.         listadd(list,(NODE *)dog);
  57.         printf("%d:%s,%d:%s\n",cat->age,cat->name,dog->age,dog->name);
  58.         printf("%p,%p,%p,%p\n",cat,list->node.next,dog,list->node.next->next);
  59.         findage(list,2);
  60.         free(cat);
  61.         free(dog);
  62.         free(list);
  63.         return 0;
  64. }

复制代码
输出结果:
  1. ============================
  2. 2:maomi,3:dahuang
  3. 145514536:maomi,0:dahuang
  4. 0x8ac6008,0x8ac6008,0x8ac6028,0x8ac6028
  5. 0x8ac6008
  6. no 2 age animal
复制代码
这边不能理解的是,为什么listadd后,cat和dog的age值就变了。
求大神指教~

论坛徽章:
5
金牛座
日期:2015-07-03 13:32:00卯兔
日期:2015-07-03 13:32:17程序设计版块每日发帖之星
日期:2015-11-29 06:20:0015-16赛季CBA联赛之同曦
日期:2015-12-15 09:36:06CU十四周年纪念徽章
日期:2016-07-06 17:18:48
2 [报告]
发表于 2015-05-19 09:56 |只看该作者
本帖最后由 seanking1987 于 2015-05-19 09:56 编辑

结贴~把list.h的cat和dog的NODE* 改为NODE就可以了
list.h
  1. #ifndef __LIST_H_
  2. #define __LIST_H_

  3. typedef struct node {
  4.         struct node* prev;
  5.         struct node* next;
  6. }NODE;

  7. typedef struct cat {
  8.         NODE node;
  9.         int age;
  10.         char name[20];
  11. }CAT;

  12. typedef struct dog {
  13.         NODE node;
  14.         int age;
  15.         char name[20];
  16.         int height;
  17. }DOG;

  18. typedef struct list {
  19.         NODE node;
  20.         int data;
  21. }LIST;

  22. #endif //__LIST_H_
复制代码
list.c
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "list.h"

  5. void lstinit(LIST *list)
  6. {
  7.         list->data = 0;
  8.         list->node.prev = NULL;
  9.         list->node.next = NULL;
  10. }

  11. void listadd(LIST *list,NODE* pnode)
  12. {
  13.         //CAT *cat = (CAT *)pnode;
  14.         //printf("%p--%s:%d\n",cat,cat->name,cat->age);
  15.         NODE* tail = list->node.prev;
  16.         pnode->prev = tail;
  17.         pnode->next = NULL;
  18.         if (tail)
  19.                 tail->next = pnode;
  20.         else
  21.                 list->node.next = pnode;
  22.         list->node.prev = pnode;
  23.         //printf("%p--%s:%d\n",cat,cat->name,cat->age);
  24.         //printf("%p--%s:%d\n",list->node.next,((CAT *)list->node.next)->name,((DOG *)list->node.next)->age);
  25. }

  26. void findage(LIST *list,int age)
  27. {
  28.         NODE *node = list->node.next;
  29.         //printf("%p\n",list->node.next);
  30.         while (node)
  31.         {
  32.                 CAT *cat = (CAT *)node;
  33.                 //printf("%p:%d %s\n",cat,cat->age,cat->name);
  34.                 if (cat->age == age)
  35.                 {
  36.                         printf("the %d age animal is cat:%s\n",cat->age,cat->name);
  37.                         return;
  38.                 }
  39.                 DOG *dog = (DOG *)node;
  40.                 if (dog->age == age)
  41.                 {
  42.                         printf("the %d age animal is cat:%s\n",dog->age,dog->name);
  43.                         return;
  44.                 }
  45.                 node = node->next;
  46.         }
  47.         printf("no %d age animal\n",age);
  48. }


  49. int main(int argc,char *argv[])
  50. {
  51.         CAT* cat = (CAT *)malloc(sizeof(CAT));
  52.         DOG* dog = (DOG *)malloc(sizeof(DOG));
  53.         LIST* list = (LIST *)malloc(sizeof(LIST));
  54.         cat->age = 2;
  55.         strcpy(cat->name,"maomi");
  56.         dog->age = 3;
  57.         strcpy(dog->name,"dahuang");
  58.         dog->height = 80;
  59.         lstinit(list);
  60.         //printf("%s:%d,%s:%d\n",cat->name,cat->age,dog->name,dog->age);
  61.         listadd(list,(NODE *)cat);
  62.         listadd(list,(NODE *)dog);
  63.         //printf("%s:%d,%s:%d\n",cat->name,cat->age,dog->name,dog->age);
  64.         //printf("%p,%p,%p,%p\n",cat,list->node.next,dog,list->node.next->next);
  65.         findage(list,2);
  66.         findage(list,3);
  67.         findage(list,4);
  68.         free(cat);
  69.         free(dog);
  70.         free(list);
  71.         return 0;
  72. }
复制代码
运行结果:
the 2 age animal is cat:maomi
the 3 age animal is cat:dahuang
no 4 age animal

论坛徽章:
11
未羊
日期:2013-12-16 12:45:4615-16赛季CBA联赛之青岛
日期:2016-04-11 19:17:4715-16赛季CBA联赛之广夏
日期:2016-04-06 16:34:012015亚冠之卡尔希纳萨夫
日期:2015-11-10 10:04:522015亚冠之大阪钢巴
日期:2015-07-30 18:29:402015亚冠之城南
日期:2015-06-15 17:56:392015亚冠之卡尔希纳萨夫
日期:2015-05-15 15:19:272015亚冠之山东鲁能
日期:2015-05-14 12:38:13金牛座
日期:2014-12-04 15:34:06子鼠
日期:2014-10-16 13:40:4715-16赛季CBA联赛之八一
日期:2016-07-22 09:41:40
3 [报告]
发表于 2015-05-22 19:31 |只看该作者
可以啊, 能自己想到这个思路
  1. struct list_head {
  2.     struct list_head *next, *prev;
  3. };

  4. #define LIST_HEAD_INIT(name) { &(name), &(name) }

  5. #define LIST_HEAD(name) \
  6. struct list_head name = LIST_HEAD_INIT(name)

  7. #define INIT_LIST_HEAD(ptr) do { \
  8.     (ptr)->next = (ptr); (ptr)->prev = (ptr); \
  9. } while (0)

  10. static void inline __list_add(struct list_head * new1,
  11.                        struct list_head * prev,
  12.                        struct list_head * next)
  13. {
  14.     next->prev = new1;
  15.     new1->next = next;
  16.     new1->prev = prev;
  17.     prev->next = new1;
  18. }

  19. #define list_add(new1, head) \
  20. do { \
  21.     __list_add(new1, head, (head)->next); \
  22. } while (0)

  23. #define list_add_tail(new1, head) \
  24. do { \
  25.     __list_add(new1, (head)->prev, head); \
  26. } while (0)

  27. static void inline __list_del(struct list_head * prev,
  28.                        struct list_head * next)
  29. {
  30.     next->prev = prev;
  31.     prev->next = next;
  32. }

  33. #define list_del(entry) \
  34. do { \
  35.     __list_del((entry)->prev, (entry)->next); \
  36. } while (0)

  37. #define list_del_init(entry) \
  38. do { \
  39.     __list_del((entry)->prev, (entry)->next); \
  40.     INIT_LIST_HEAD((entry)); \
  41. } while (0)

  42. static int  inline list_empty(struct list_head *head)
  43. {
  44.     return head->next == head;
  45. }

  46. static inline int list_is_singular(struct list_head *head)
  47. {
  48.         return !list_empty(head) && (head->next == head->prev);
  49. }

  50. static void inline list_merge(struct list_head *one, struct list_head *two)
  51. {
  52.     one = one->prev;
  53.     one->next->prev = two->prev;
  54.     two->prev->next = one->next;
  55.     one->next = two;
  56.     two->prev = one;
  57. }

  58. static void inline list_split(struct list_head *one, struct list_head *two)
  59. {
  60.     struct list_head* entry = two->prev;
  61.     one->prev->next = two;
  62.     two->prev = one->prev;
  63.     one->prev = entry;
  64.     entry->next = one;
  65. }

  66. #define list_entry(ptr, type, member) container_of(ptr, type, member)
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP