码迷,mamicode.com
首页 > 其他好文 > 详细

链表实现多项式加法

时间:2015-09-18 18:24:30      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

头文件polynom.h:

 1 #ifndef POLYNOM_H_INCLUDED
 2 #define POLYNOM_H_INCLUDED
 3 
 4 struct Node;    
 5 typedef struct Node *PtrToNode;
 6 typedef PtrToNode Polynom;
 7 typedef PtrToNode Position;
 8 
 9 Polynom CreateEmpty(Polynom L);         //创建一个空的多项式
10 Polynom Add(Polynom P1, Polynom P2);    //将P1和p2相加,结果返回
11 void Insert(Polynom L, int coe, int exp);   //在L最后面插入一个新的元素
12 Polynom InsertItem(Polynom Sum, Polynom P); //将P的剩余部分接到sum最后面
13 void print(Polynom L);  //  打印
14 
15 #endif // POLYNOM_H_INCLUDED

实现文件implementation.c:

 1 #include<stdio.h>
 2 #include "polynom.h"
 3 
 4 struct Node{
 5     int Coefficient;
 6     int Exponent;
 7     PtrToNode Next;
 8 };
 9 
10 Polynom CreateEmpty(Polynom L) {
11     L = malloc(sizeof(struct Node));
12     L->Next = NULL;
13     return L;
14 }
15 
16 Insert(Polynom L, int coe, int exp) {
17     Position TmpCell, P;
18     TmpCell = malloc(sizeof(struct Node));
19     TmpCell->Coefficient = coe;
20     TmpCell->Exponent = exp;
21     TmpCell->Next = NULL;
22     P = L;
23     while(P->Next != NULL)
24         P = P->Next;
25     P->Next = TmpCell;
26 }
27 
28 Polynom InsertItem(Polynom Sum, Polynom L) {
29     Position P;
30     P = Sum;
31     while(P->Next != NULL) {
32         P = P->Next;
33     }
34     P->Next = L;
35     return P;
36 }
37 
38 Polynom Add(Polynom P1, Polynom P2) {
39     Polynom Sum;
40     Sum = CreateEmpty(Sum);
41     P1 = P1->Next;
42     P2 = P2->Next;
43     while( P1 != NULL && P2 != NULL) {
44         if(P1->Exponent > P2->Exponent) {
45             Insert(Sum, P1->Coefficient, P1->Exponent);
46             P1 = P1->Next;
47         } else if(P1->Exponent < P2->Exponent) {
48             Insert(Sum, P2->Coefficient, P2->Exponent);
49             P2 = P2->Next;
50         } else if(P1->Exponent == P2->Exponent) {
51             Insert(Sum, P1->Coefficient + P2->Coefficient, P1->Exponent);
52             P1 = P1->Next;
53             P2 = P2->Next;
54         }
55     }
56     if(P1 != NULL) {
57         InsertItem(Sum, P1);
58     } else if(P2 != NULL) {
59         InsertItem(Sum, P2);
60     }
61     return Sum;
62 }
63 
64 void print(Polynom L) {
65     Position P = L->Next;
66     while(P != NULL) {
67         printf("%d * 10^%d", P->Coefficient, P->Exponent);
68         if(P->Next != NULL)
69             printf(" + ");
70         P = P->Next;
71     }
72 }

测试:

#include <stdio.h>
#include <stdlib.h>
#include "polynom.h"

int main()
{
    Polynom P1;
    Polynom P2;
    Polynom Sum;
    P1 = CreateEmpty(P1);
    P2 = CreateEmpty(P2);

    Insert(P1, 9, 21);
    Insert(P1, 4, 18);
    Insert(P1, 5, 6);
    Insert(P1, 3, 2);
    Insert(P2, 4, 19);
    Insert(P2, 7, 6);
    Insert(P2, 2, 1);

    Sum = Add(P1, P2);
    print(Sum);

    return 0;
}

 

链表实现多项式加法

标签:

原文地址:http://www.cnblogs.com/zsdvvb/p/4819986.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!