初学数据结构六

  1 Status ListMerge_L(LinkList &A, LinkList &B, LinkList &C)
  2 {
  3     LinkList pa, pb, qa, qb;
  4     pa = A->next;
  5     pb = B->next;
  6     C = A;
  7     while (pa && pb)
  8     {
  9         qa = pa;
 10         qb = pb;
 11         pa = pa->next;
 12         pb = pb->next;
 13         qb->next = qa->next;
 14         qa->next = qb;
 15     }
 16     if (!pa)
 17         qb->next = pb;
 18     pb = B;
 19     free(pb);
 20     return OK;
 21 }
 22 
 23 Status ListMergeOppose_L(LinkLsit &A.LinkList &B, LinkList &C)
 24 {
 25     LinkList pa, pb, qa, qb;
 26     pa = A;
 27     pb = B;
 28     qa = pa;
 29     qb = pb;
 30     pa = pa->next;
 31     pb = pb->next;
 32     A->next = NULL;
 33     C = A;
 34     while (pa && pb)
 35     {
 36         if (pa->data < pb->data)
 37         {
 38             qa = pa;
 39             pa = pa->next;
 40             qa->next = A->next;
 41             A->next = qa;
 42         }
 43         else
 44         {
 45             qb = pb;
 46             pb = pb->next;
 47             qb->next = A->next;
 48             A->next = qb;
 49         }
 50     }
 51     while (pa)
 52     {
 53         qa = pa;
 54         pa = pa->next;
 55         qa->next = A->next;
 56         A->next = qa;
 57     }
 58     while (pb)
 59     {
 60         qb = pb;
 61         pb = pb->next;
 62         qb->next = A->next;
 63         A->next = qb;
 64     }
 65     pb = B;
 66     free(pb);
 67     return OK;
 68 }
 69 
 70 Status ListCross_Sq(SqList&A, SqList &B, SqList &C)
 71 {
 72     int i == , j = 0, k = 0;
 73     while (i < A.length && j < B.length)
 74     {
 75         if (A.elem[i] < B.elem[j])
 76             i++;
 77         else
 78             if (A.elem[i] > B.elem[j])
 79                 j++;
 80             else
 81             {
 82                 ListInsert_Sq(C, k, A.elem[i]);
 83                 i++;
 84                 k++;
 85             }
 86     }
 87     return OK;
 88 }
 89 
 90 Status ListCross_L(LinkList &A, LinkList &B, LinkList &C)
 91 {
 92     LinkList pa, pb, qa, qb, pt;
 93     pa = A;
 94     pb = B;
 95     qa = pa;
 96     qb = pa;
 97     pa = pa->next;
 98     pb = pb->next;
 99     C = A;
100     while (pa && pb)
101     {
102         if (pa->data < pb->data)
103         {
104             pt = pa;
105             pa = pa->next;
106             qa->next = pa;
107             free(pt);
108         }
109         else
110             if (pa->data > pb->data)
111             {
112                 pt = pb;
113                 pb = pb->next;
114                 qb->next = pb;
115                 free(pt);
116             }
117             else
118             {
119                 qa = pa;
120                 pa = pa->next;
121             }
122     }
123     while (pa)
124     {
125         pt = pa;
126         pa = pa->next;
127         qa->next = pa;
128         free(pt);
129     }
130     while (pb)
131     {
132         pt = pb;
133         pb = pb->next;
134         qb->next = qb;
135         free(pt);
136     }
137     pb = B;
138     free(pb);
139     return OK;
140 }
141 
142 
143 Status ListDivideInto2CL(LinkedPoly &L, LinkedPoly &L1)
144 {
145     LinkedPoly p, p1, q, pt;
146     q = L;
147     p = L->next;
148     p1 = L1;
149     while (p != L)
150     {
151         if(p->data.exp % 2 == 0)//.exp字段
152         {
153             pt = p;
154             p = p->next;
155             q->next = p;
156             pt->next = p1->next;
157             p1->next = pt;
158             p1 = p1->next;
159         }
160         else
161         {
162             q = p;
163             p = p->next;
164             }
165     }
166     return oK;
167 }

 

  1 typedef struct
  2 {
  3     int coef;
  4     int exp;
  5 }PolyTerm;
  6 
  7 typedef struct
  8 {
  9     PolyTerm *data;
 10     int last;
 11 }SqPoly;
 12 
 13 Status PolyInit(SqPoly &L)
 14 {
 15     int i;
 16     PolyTerm *p;
 17     cout << "请输入多项式的项数:";
 18     cin >> L.last;
 19     L.data = (PolyTerm*) malloc(L.last*sizeof(PolyTerm));
 20     if (!L.data)
 21         return ERROR;
 22     p = L.data;
 23     for (i = 0; i < L.last; i++)
 24     {
 25         cout << "请输入系数:";
 26         cin >> p->coef;
 27         cout << "请输入指数:";
 28         cin >> p->exp;
 29         p++;
 30     }
 31     return OK;
 32 }
 33 
 34 double PolySum(SqPoly &L, double x0)
 35 {
 36     double Pn, x;
 37     int i, j;
 38     PolyTerm *p;
 39     p = L.data;
 40     for (i = 0, Pn = 0; i < L.last; i++, p++){
 41         for (j = 0, x = 1; j < p->exp; j++)
 42             x = x*x0;
 43         Pn = Pn + p->coef*x;
 44     }
 45     return Pn;
 46 }
 47 
 48 Status PolyMinus(SqPoly &L, SqPoly &L1, SqPoly &L2)
 49 {
 50     PolyTerm *p, *p1, *p2;
 51     p = L.data;
 52     p1 = L1.data;
 53     p2 = L2.data;
 54     int i = 0, j = 0, k = 0;
 55     while (i < L1.last && j < L2.last)
 56     {
 57         if (p1->exp < p1->exp)
 58         {
 59             p->coef = p1->coef;
 60             p++; k++;
 61             p1++; i++;
 62         }
 63         else
 64             if (p1->exp > p2->exp)
 65             {
 66                 p->coef = p2->coef;
 67                 p->exp = p2->exp;
 68                 p++; k++;
 69                 p2++; j++;
 70             }
 71             else
 72             {
 73                 if (p1->coef != p2->coef)
 74                 {
 75                     p->coef = (p1->coef) - (p2->coef);
 76                     p->exp = p1->exp;
 77                     p++; k++;
 78                 }
 79                 p1++; p2++;
 80                 i++; j++;
 81             }
 82     }
 83     if (i<L1.last)
 84         while (i < L1.last)
 85         {
 86             p->coef = p1->coef;
 87             p->exp = p1->exp;
 88             p++; k++;
 89             p1++; i++;
 90         }
 91         if (j<L2.last)
 92             while (j < L2.last)
 93             {
 94                 p - coef = p2->coef;
 95                 p->exp = p2->exp;
 96                 p++; k++;
 97                 p2++; j++;
 98             }
 99             L.last = k;
100             return OK;
101 }

 

posted @ 2013-10-07 17:59  herizai007  阅读(325)  评论(0编辑  收藏  举报