数据结构题集(C语言版) P81 1.5 #include <stdio.h> #include <stdlib.h> typedef struct PolyNode { float c; int n; struct PolyNode *next; }PolyNode; PolyNode *Init(int *m) { PolyNode *p, *r, *temp, *L; int i, n = *m; L = (PolyNode *)malloc(sizeof(PolyNode)); L->next = NULL; printf("Input the terms:/n"); for(i = 0;i < n;i++) { r = L; p = (PolyNode*)malloc(sizeof(PolyNode)); scanf("%f,%d,", &(p->c), &(p->n)); p->next = NULL; if(i != 0) { while(r) { if(r == L) r = r->next; else { if(p->n > r->n) { p->next = r; L->next = p; break; } else { if(p->n < r->n) { temp = r; r = r->next; if(r == NULL) temp->next = p; } else { r->c += p->c; free(p); (*m)--; break; } } } } } else { r->next = p; r = r->next; } } return L; } void Create() { printf("Input the number of terms in Polynomial:"); PolyNode *L, *r; int *m, n; m = &n; scanf("%d",&n); L = Init(m); r = L; printf("%d/n", *m); while(r) { if(r == L) r = r->next; printf("%f,%d,", r->c, r->n); r = r->next; } } void Add() { printf("A+B/n"); PolyNode *p, *q, *r, *temp, *LA, *LB, *LC; int x, y, *a, *b; int n; a = &x; b = &y; printf("Input the number of terms in Polynomial A:"); scanf("%d", &x); LA = Init(a); printf("Input the number of terms in Polynomial B:"); scanf("%d", &y); LB = Init(b); n = *a + *b; p = LA->next; q = LB->next; LC = (PolyNode *)malloc(sizeof(PolyNode)); LC->next = NULL; r = LC; while(p && q) { if(p->n > q->n) { r->next = p; p = p->next; r = r->next; } else { if(p->n < q->n) { r->next = q; q = q->next; r = r->next; } else { n--; r->next = p; p->c += q->c; p = p->next; temp = q; q = q->next; r = r->next; free(temp); } } } if(p) r->next = p; if(q) r->next = q; temp = r = LC; while(r) { if(r == LC) r = r->next; else { if(r->c == 0) { n--; temp->next = r->next; } else temp = r; r = r->next; } } if(LC->next == NULL) { printf("0"); return; } r = LC; printf("%d/n", n); while(r) { if(r == LC) r = r->next; printf("%f,%d,", r->c, r->n); r = r->next; } } void Substract() { printf("A-B/n"); PolyNode *p, *q, *r, *temp, *LA, *LB, *LC; int x, y, *a, *b; int n; a = &x; b = &y; printf("Input the number of terms in Polynomial A:"); scanf("%d", &x); LA = Init(a); printf("Input the number of terms in Polynomial B:"); scanf("%d", &y); LB = Init(b); temp = LB->next; while(temp) { temp->c *= (-1); temp = temp->next; } n = *a + *b; p = LA->next; q = LB->next; LC = (PolyNode *)malloc(sizeof(PolyNode)); LC->next = NULL; r = LC; while(p && q) { if(p->n > q->n) { r->next = p; p = p->next; r = r->next; } else { if(p->n < q->n) { r->next = q; q = q->next; r = r->next; } else { n--; r->next = p; p->c += q->c; p = p->next; temp = q; q = q->next; r = r->next; free(temp); } } } if(p) r->next = p; if(q) r->next = q; temp = r = LC; while(r) { if(r == LC) r = r->next; else { if(r->c == 0) { n--; temp->next = r->next; } else temp = r; r = r->next; } } if(LC->next == NULL) { printf("0"); return; } r = LC; printf("%d/n", n); while(r) { if(r == LC) r = r->next; printf("%f,%d,", r->c, r->n); r = r->next; } } void main() { int i; printf("Please input a number to select a function:/n"); printf("1 Create a polynomial;/n"); printf("2 Add two polynomials;/n"); printf("3 Substract two polynomials;/n"); scanf("%d", &i); switch (i) { case 1:Create();break; case 2:Add();break; case 3:Substract();break; } }