链式存储

来源:互联网 发布:chrome ubuntu 16.04 编辑:程序博客网 时间:2024/05/14 07:28

点击(此处)折叠或打开

  1. /*
  2.  *题二:设计并实现以下算法:给出用单链表存储多项式的结构,
  3.  *利用后接法生成多项式的单链表结构,实现两个多项式相加的运算,
  4.  *并就地逆置加相后的多项式链式。
  5.  */

  6. #include <stdio.h>
  7. #include <malloc.h>
  8. #include <conio.h>
  9. #define EQUAL 1
  10. #define ok 1
  11. #define error 0
  12. #define overflow -1
  13. #define LIST_INIT_STZE 100
  14. #define LISTINCREMENT 10

  15. typedef struct STU {
  16.     int e;
  17.     int c;
  18. }ElemType;

  19. typedef struct LNODE {
  20.     ElemType data;
  21.     struct LNODE *next;
  22. }LNode, *LinkList;

  23. int Init(LinkList *L)
  24. {
  25.     *L=(LNode *)malloc(sizeof(LNode));
  26.     if(!L)
  27.         exit(error);
  28.     (*L)->next=NULL;
  29.     return ok;
  30. }

  31. int PrintList (LinkList L)
  32. {
  33.     LinkList p;
  34.     p=L->next;
  35.     while(p)
  36.     {
  37.         printf ("%d*X^%d",p->data.c,p->data.e);
  38.         if (p->next!=NULL)
  39.             printf ("+");
  40.         p=p->next;
  41.     }
  42.     printf ("\n");
  43. }

  44. int CreateMult (LinkList L)
  45. {
  46.     int n;
  47.     int i;
  48.     ElemType e;
  49.     scanf ("%d", &n);
  50.     for (i=1;i<=n;i++)
  51.     {
  52.         printf ("e of %d elem:", i);
  53.         scanf ("%d", &e.e);
  54.         printf ("c of %d elem:", i);
  55.         scanf ("%d", &e.c);
  56.         Insert (L, e);
  57.     }
  58. }

  59. int Convert (LinkList *L)
  60. {
  61.     LinkList p,q,r;
  62.     p=*L;
  63.     q=p->next;
  64.     r=q->next;
  65.     while (q)
  66.     {
  67.         q->next=p;
  68.         if (q->next==(*L))
  69.             q->next=NULL;
  70.         p=q;
  71.         q=r;
  72.         r=q->next;
  73.     }
  74.     (*L)->next=p;
  75.     return 0;
  76. }

  77. int Insert (LinkList L, ElemType e)
  78. {
  79.     LinkList p,s;
  80.     p=L;
  81.     while (p->next!=NULL)
  82.         p=p->next;
  83.     s=(LinkList)malloc(sizeof(LNode));
  84.     s->data=e;
  85.     s->next=NULL;
  86.     p->next=s;
  87. }

  88. int CompareE (ElemType e1, ElemType e2)
  89. {
  90.     if (e1.e==e2.e)
  91.         return 1;
  92.     else
  93.         return 0;
  94. }

  95. int Less_Compare (ElemType e1, ElemType e2)
  96. {
  97.     
  98.     if (e1.e<e2.e)
  99.         return 1;
  100.     else
  101.         return 0;
  102. }

  103. int Compute (LinkList la, LinkList lb, LinkList lc)
  104. {
  105.     ElemType e;
  106.     LinkList pa;
  107.     LinkList pb;
  108.     pa=la->next;
  109.     pb=lb->next;
  110.     while(pa&&pb)
  111.     {
  112.         if (CompareE (pa->data,pb->data))
  113.         {
  114.             e.c=(pa->data.c)+(pb->data.c);
  115.             e.e=pa->data.e;
  116.             Insert (lc, e);
  117.             pa=pa->next;
  118.             pb=pb->next;
  119.         }
  120.         else if(Less_Compare(pa->data,pb->data))
  121.         {
  122.             Insert (lc, pb->data);
  123.             pb=pb->next;
  124.         }
  125.         else
  126.         {
  127.             Insert (lc, pa->data);
  128.             pa=pa->next;
  129.         }
  130.     }
  131.     while (pa)
  132.     {
  133.         Insert(lc, pa->data);
  134.         pa=pa->next;
  135.     }
  136.     while (pb)
  137.     {
  138.         Insert(lc, pb->data);
  139.         pb=pb->next;
  140.     }
  141. }

  142. int main ()
  143. {
  144.     LinkList la, lb, lc;

  145.     clrscr();
  146.     Init (&la);
  147.     printf ("Create la:");
  148.     CreateMult (la);

  149.     clrscr();
  150.     Init (&lb);
  151.     printf ("Create lb:");
  152.     CreateMult (lb);

  153.     clrscr();
  154.     printf ("Show la:");
  155.     PrintList (la);
  156.     printf ("Show lb:");
  157.     PrintList (lb);


  158.     Init (&lc);
  159.     Compute (la, lb, lc);
  160.     printf ("Show lc:");
  161.     PrintList (lc);
  162.     Convert (&lc);
  163.     PrintList (lc);
  164.     getch();
  165. }

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

上一篇:链式存储

下一篇:栈的应用

相关热门文章
  • 建筑工程管理软件信息管理的目...
  • 块设备驱动程序设计
  • !优惠HP8561E HP8563E频谱分析...
  • !中秋热卖AgilentE4421B信号源...
  • ipcam的几个概念
  • test123
  • 编写安全代码——小心有符号数...
  • 使用openssl api进行加密解密...
  • 一段自己打印自己的c程序...
  • sql relay的c++接口
  • 怎么样找出BIND中查询并发量多...
  • 可有人在实际的openstack生产...
  • 如下makefile如何编写
  • sqlldr 参数配置
  • 讨论一下各位所管理的mysql生...
给主人留下些什么吧!~~