Chinaunix首页 | 论坛 | 博客
  • 博客访问: 68618
  • 博文数量: 17
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 154
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-12 22:56
个人简介

不卑不亢

文章分类

全部博文(17)

文章存档

2016年(1)

2015年(13)

2014年(3)

分类: C/C++

2015-03-21 14:22:41


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. typedef struct _node_ {
  4.     int coef;
  5.     int exp;
  6.     struct _node_ *next;
  7. }linknode_t, *linklist;

  8. linklist linklist_init();
  9. int linklist_insert(linklist h, int coef, int exp);
  10. int linklist_show(linklist h);
  11. int linklist_union(linklist h1, linklist h2);

  12. int main()
  13. {
  14.     linklist h1 = NULL;
  15.     linklist h2 = NULL;
  16.     int i ,j;
  17.     int a[][2] = {{5, 0}, {2, 1}, {9, 4}, {3, 16}, {8, 8}, {-30, 27}};
  18.     int b[][2] = {{23, 16}, {6, 1}, {-8, 8}, {11, 11}, {25, 25}, {27, 27}};
  19.     
  20.     h1 = linklist_init();
  21.     h2 = linklist_init();

  22.     for (i = 0; i < sizeof(a) / sizeof(a[0]); i++)
  23.         linklist_insert(h1, a[i][0], a[i][1]);
  24.     for (j = 0; j < sizeof(b) / sizeof(b[0]); j++)
  25.         linklist_insert(h2, b[j][0], b[j][1]);

  26.     linklist_show(h1);
  27.     putchar('\n');
  28.     linklist_show(h2);

  29.     puts("");
  30.     linklist_union(h1, h2);
  31.     linklist_show(h1);

  32.     return 0;
  33. }

  34. linklist linklist_init()
  35. {
  36.     linklist h = NULL;

  37.     h = (linklist)malloc(sizeof(linknode_t));

  38.     if (NULL == h) {
  39.         printf("fail to malloc!\n");
  40.         return NULL;
  41.     }

  42.     h->next = NULL;

  43.     return h;
  44. }

  45. int linklist_insert(linklist h, int coef, int exp)
  46. {
  47.     linklist p = h;
  48.     linklist new = NULL;

  49.     while (p->next && p->next->exp < exp)
  50.         p = p->next;

  51.     new = (linklist)malloc(sizeof(linknode_t));
  52.     
  53.     new->coef = coef;
  54.     new->exp = exp;
  55.     new->next = p->next;
  56.     p->next = new;

  57.     return 0;
  58. }

  59. int linklist_show(linklist h)
  60. {
  61.     linklist p = h->next;

  62.     while (p) {
  63.         printf("(%d,%d)\n", p->coef, p->exp);
  64.         p = p->next;
  65.     }

  66.     return 0;
  67. }

  68. int linklist_union(linklist h1, linklist h2)
  69. {
  70.     linklist p = h1->next, q = h2->next, r = h1, s;
  71.     h1->next = NULL;
  72.     
  73.     while (p && q) {
  74.         if (p->exp < q->exp) {
  75.             r->next = p;
  76.             p = p->next;
  77.             r = r->next;
  78.             r->next = NULL;
  79.         } else if (p->exp == q->exp) {
  80.             p->coef += q->coef;
  81.             if (0 == p->coef) {
  82.                 printf("==0\n");
  83.                 s = p;
  84.                 p = p->next;
  85.                 free(s);
  86.                 s = NULL;
  87.             } else {
  88.                 r->next = p;
  89.                 p = p->next;
  90.                 r = r->next;
  91.                 r->next = NULL;
  92.             }
  93.             s = q;
  94.             q = q->next;
  95.             free(s);
  96.             s = NULL;
  97.         } else {
  98.             r->next = q;
  99.             q = q->next;
  100.             r = r->next;
  101.             r->next = NULL;
  102.         }
  103.     }

  104.     if (p)
  105.         r->next = p;
  106.     else
  107.         r->next = q;

  108.     return 0;
  109. }


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

上一篇:约瑟夫问题

下一篇:循环队列

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