user-0 提问: 虽然很不好意思。。但是还是要把代码贴上来/（C语言实现数据结构的Arraylist）

回复:
user: user-1
content: 仅供参考：//链表实现一元多项式的加法减法乘法 #include <stdio.h> #include <stdlib.h> typedef struct node {     float coef;   //系数     int expn;     //指数     struct node *next; } PolyNode;      //多项式节点 polynomial node typedef PolyNode* Polynomial; Polynomial createPolynomial() {  //创建多项式     PolyNode *p, *q, *head = (PolyNode *)malloc(sizeof(PolyNode));   //头节点     head->next = NULL;     float coef;     int expn;     printf("输入该多项式每一项的系数和指数，每项一行，输入0 0结束！\n");     while (scanf("%f %d", &coef, &expn) && coef) {   // 默认，按指数递减排列         if (head->next) {             p = head;             while (p->next && expn < p->next->expn)                 p = p->next;             if (p->next) {                 if (expn == p->next->expn) { //有相同指数的直接把系数加到原多项式                     p->next->coef += coef;                     if (p->next->coef > -0.000001 && p->next->coef < 0.000001) { //若是相加后系数为0，则舍弃该节点                         q = p->next;                         p->next = q->next;                         free(q);                     }                 } else {                     q = (PolyNode*)malloc(sizeof(PolyNode));                     q->coef = coef;                     q->expn = expn;                     q->next = p->next;                     p->next = q;                 }             } else {                 p->next = (PolyNode*)malloc(sizeof(PolyNode));                 p = p->next;                 p->coef = coef;                 p->expn = expn;                 p->next = NULL;             }         } else {             head->next = (PolyNode*)malloc(sizeof(PolyNode));             head->next->coef = coef;             head->next->expn = expn;             head->next->next = NULL;         }     }     return head; } Polynomial multiply(Polynomial poly, float coef, int expn) {  //多项式与指定单项式相乘，该单项式为 coefx^expn     PolyNode *p, *q, *Poly = (PolyNode*)malloc(sizeof(PolyNode));     p = Poly;     q = poly->next;     while (q) {         p->next = (PolyNode*)malloc(sizeof(PolyNode));         p = p->next;         p->coef = (q->coef*coef);         p->expn = (q->expn + expn);         q = q->next;     }     p->next = NULL;     return Poly; } void add(Polynomial poly1, Polynomial poly2) {  //把 poly2 加到 poly1 上     PolyNode *p, *q, *r;     r = poly1;     p = poly1->next;  //指向第一个节点     q = poly2->next;     poly2->next = NULL;     while (p && q) {         if (p->expn > q->expn) {             r->next = p;             p = p->next;             r = r->next;         } else if (p->expn < q->expn) {             r->next = q;             q = q->next;             r = r->next;         } else {             PolyNode *t;             p->coef += q->coef;             if (!(p->coef > -0.000001 && p->coef < 0.000001)) //系数不为0             {                 r->next = p;                 r = r->next;                 p = p->next;             } else {                 t = p;                 p = p->next;                 free(t);             }             t = q;             q = q->next;             free(t);         }     }     if (p)         r->next = p;     if (q)         r->next = q; } Polynomial polySubtract(Polynomial poly1, Polynomial poly2) {  //多项式减法 poly1-poly2形成一个新的多项式     //把poly2的系数取相反数，形成一个新的多项式     Polynomial poly = (PolyNode*)malloc(sizeof(PolyNode)); //构造头节点     PolyNode *p, *q;     p = poly;     q = poly2->next;     while (q) {         p->next = (PolyNode*)malloc(sizeof(PolyNode));         p = p->next;         p->coef = -(q->coef);  //系数取反         p->expn = q->expn;         q = q->next;     }     p->next = NULL;     add(poly, poly1);  //利用加法     return poly; } Polynomial polyAdd(Polynomial poly1, Polynomial poly2) { //多项式相加 poly1+poly2形成一个新的多项式     Polynomial poly = (PolyNode*)malloc(sizeof(PolyNode));  //和多项式的头节点     poly->next = NULL;     PolyNode *p, *q, *r;     r = poly;     p = poly1->next;     q = poly2->next;     while (p&&q) {         if (p->expn > q->expn) {             r->next = (PolyNode*)malloc(sizeof(PolyNode));             r = r->next;             r->coef = p->coef;             r->expn = p->expn;             p = p->next;         } else if (p->expn < q->expn) {             r->next = (PolyNode*)malloc(sizeof(PolyNode));             r = r->next;             r->coef = q->coef;             r->expn = q->expn;             q = q->next;         } else {             float m = p->coef + q->coef;             if (!(m > -0.000001 && m < 0.000001)) {                 r->next = (PolyNode*)malloc(sizeof(PolyNode));                 r = r->next;                 r->coef = m;                 r->expn = p->expn;             }             q = q->next;             p = p->next;         }     }     while (p) {         r->next = (PolyNode*)malloc(sizeof(PolyNode));         r = r->next;         r->coef = p->coef;         r->expn = p->expn;         p = p->next;     }     while (q) {         r->next = (PolyNode*)malloc(sizeof(PolyNode));         r = r->next;         r->coef = q->coef;         r->expn = q->expn;         q = q->next;     }     r->next = NULL;     return poly; } Polynomial polyMultiply(Polynomial poly1, Polynomial poly2) {  //多项式相乘     Polynomial poly = (PolyNode*)malloc(sizeof(PolyNode));  //创建多项式和的头节点     poly->next = NULL;     PolyNode *p;     p = poly2->next;     while (p) {         add(poly, multiply(poly1, p->coef, p->expn));         p = p->next;     }     return poly; } void printPoly(Polynomial poly) {  //打印多项式     if (poly && poly->next) {         PolyNode *p = poly->next;  //p指向第一个节点         while (p->next) {             printf("%gx^%d", p->coef, p->expn);             p = p->next;             if (p && (p->coef > 0))                 printf("+");         }         if (p->expn == 0)             printf("%g", p->coef);   //打印常数项         else             printf("%gx^%d", p->coef, p->expn);         printf("\n");     } } void freePoly(Polynomial poly) {  //释放内存     if (poly && poly->next) {         PolyNode *p, *q;         p = poly;         while (p) {             q = p->next;             free(p);             p = q;         }     }     poly = NULL; } int main() {     printf("用链表实现多项式的加减法\n");     Polynomial poly1, poly2, poly3;     printf("创建多项式一\n");     poly1 = createPolynomial();     printf("多项式一：\n");     printPoly(poly1);     printf("创建多项式二\n");     poly2 = createPolynomial();     printf("多项式二：\n");     printPoly(poly2);     printf("两多项式相加，和为：\n");     poly3 = polyAdd(poly1, poly2);     printPoly(poly3);     freePoly(poly3);     printf("两个多项式相乘，积为：\n");     poly3 = polyMultiply(poly1, poly2);     printPoly(poly3);     freePoly(poly3);     printf("两多项式相减，差为：\n");     poly3 = polySubtract(poly1, poly2);     printPoly(poly3);     freePoly(poly1);     freePoly(poly2);     freePoly(poly3);     system("pause");     return 0; }

user: user-2
content: #include <stdio.h> #include <stdlib.h> #include <assert.h>  typedef int elem_type;  typedef struct poly_key {     elem_type ratio;     elem_type power; }poly_obj, *poly_handle;  typedef struct poly_node {     poly_obj poly;     struct poly_node *next; }polynomial_obj, *polynomial_handle;  typedef int (*pfunc)(poly_obj, poly_obj);   static polynomial_handle create_node(void) {     polynomial_handle pnew;      pnew = (polynomial_handle)malloc(sizeof(polynomial_obj));     if (!pnew) {         fprintf(stderr, "malloc error!\n");         return NULL;     }     pnew->poly.ratio = 0;     pnew->poly.power = 0;     pnew->next = NULL;      return pnew; }  /* Create a polynomial */ void create_polynomial_link(polynomial_handle *phead) {     polynomial_handle p, q;      while (1) {         p = create_node();         printf("Please input two numbers(ratio and power): ");         scanf("%d%d", &p->poly.ratio, &p->poly.power);         if (p->poly.ratio == 0)    /* if ratio is equal zero, then break from while.*/             break;         if (!(*phead)) {             *phead = p;             q = *phead;             continue;         }         q->next = p;         q = p;     }     free(p); }   void show_polynomial_link(polynomial_handle phead) {     polynomial_handle pcur;      pcur = phead;     while (pcur) {         printf("%dx^%d ", pcur->poly.ratio, pcur->poly.power);         pcur = pcur->next;     }     putchar(10); }  int get_length_polynomial(polynomial_handle phead) {     int cnt = 0;     polynomial_handle pcur = phead;      while (pcur) {         cnt++;         pcur = pcur->next;     }      return cnt; }  /* flag: 0: compare by ratio, 1: compare by power*/ static int check_cond(polynomial_handle pnode, int val, int flag) {     assert(pnode);      if (flag)         return (pnode->poly.power == val);     else         return (pnode->poly.ratio == val); }   void delete_polynomail(polynomial_handle *phead, int val, int flag) {     polynomial_handle pcur, prev;      pcur = prev = *phead;     while (pcur) {         if (check_cond(pcur, val, flag)) {             if (pcur == *phead) {                 *phead = (*phead)->next;                 free(pcur);                 pcur = *phead;                 continue;             }             prev->next = pcur->next;             free(pcur);             pcur = prev->next;         } else {             prev = pcur;             pcur = pcur->next;         }     } }  static int compare_as_ratio(poly_obj a, poly_obj b) {     return (a.ratio - b.ratio); }  static int compare_as_power(poly_obj a, poly_obj b) {     return (a.power - b.power); }  void bubble_asc_sort(polynomial_handle phead, pfunc cmp) {     polynomial_handle p, q;     poly_obj tmp;      for (p = phead; p; p = p->next)         for (q = phead; q->next; q = q->next)             if (cmp(q->poly, q->next->poly) > 0) {                 tmp = q->poly;                 q->poly = q->next->poly;                 q->next->poly = tmp;             } }  /* insert node before phead */ void insert_polynomail_node(polynomial_handle *phead, poly_obj param) {     polynomial_handle pnew;      pnew = create_node();      pnew->poly = param;      pnew->next = *phead;     *phead = pnew; }  void destroy_polynomail_link(polynomial_handle phead) {     polynomial_handle pcur, prev;      pcur = phead;      while (pcur) {         prev = pcur->next;         free(pcur);         pcur = prev;     } }  int main(void) {     int len, flag, val;     polynomial_handle phead = NULL;     poly_obj poly;      create_polynomial_link(&phead);     show_polynomial_link(phead);      printf("Add one term(ratio, power):  ");     scanf("%d%d", &poly.ratio, &poly.power);     insert_polynomail_node(&phead, poly);     printf("After insert: \n");     show_polynomial_link(phead);      len = get_length_polynomial(phead);     printf("The term of polynomial is %d\n", len);       bubble_asc_sort(phead, compare_as_ratio);     printf("After sort as ratio: \n");     show_polynomial_link(phead);      bubble_asc_sort(phead, compare_as_power);     printf("After sort as power: \n");     show_polynomial_link(phead);      printf("Del terms, del ratio or power(0, 1): ");     scanf("%d", &flag);     printf("Please input a number: ");     scanf("%d", &val);      delete_polynomail(&phead, val, flag);     printf("After delete: \n");     show_polynomial_link(phead);      destroy_polynomail_link(phead);     return 0; }   链表表示多项式，如有问题可以继续讨论～加油吧～

user: user-3

user: user-1
content: 偶遇到类似问题都是用 “每次用/*...*/注释掉不同部分再重新编译，直到定位到具体语法出错的位置。” 的方法解决的。

user: user-2
content: 看到一句要提高难度，看过你的代码，我还是建议不要提高难度了。 1.代码没有条理性，函数定义后面加数字这是一个不好的定义方式，每个函数都有自己的独立功能，如果功能相近不建议写两个，在进行细分。 2. 函数返回值和接收返回值的类型不匹配，这个在编译阶段就会提出警告甚至是错误；建议，先把警告和错误去掉； 3. 函数的结束方式：1）正常退出，就是所有语句都执行完毕；2）执行逻辑错误，比如malloc失败，exit退出；3）return返回退出；若return了，那么直接从该函数里退出了，自然后面的语句就不会执行了，但是程序里把很多关键的语句放在return后面执行； 4. 注释太多，注释多不如把代码写的通俗易懂，就是在定义变量，函数的时候能够做到见名知义，这样就不需要写那么多注释了，如果真要写那么多，建议你写成文档，用于代码说明； 5. 结构体可以直接赋值的，在排序中看到你的结构体交换数据还是先将结构体元素拷贝，然后在每个元素交换；如果一个结构体有50多个成员，那怎么交换两个结构体的值？ 6.个人建议，代码不一定要写完了所有功能再编译，而是一个功能写完，编译运行没问题在进行下一个功能；当然，需要在写之前先做好设计；如果一下子写完，再一下子出了很多error或警告，处理起来可能就会比较麻烦。当然，如果自己的能力达到一定程度了，可以考虑。但是即使这样，我还是建议一点一点的实现，步步为营，稳打稳进。 以上都是个人见解，如有说的重的，请包涵～

user: user-4
content: 都是编译的小问题，我总结下：  1： 函数使用前，必须能看到它的声明，但是你有些函数使用的时候都看不见声明，虽然在后面有定义 解决方法：把函数的定义放在前面，或者在最开始处声明函数，在后面定义  2：实参和返回值，跟函数的形参和返回值不匹配报错，例如 //main char judge; int choice;  judge=create_poly(&poly1,&poly2); 返回值要求char *，你却给了个char； 参数要求multinomial_add *，你却传了个 multinomial_add **  3：使用未定义的东西 //main case 3:judge = Funcdetele_poly_oly_1(poly1); printf("%s\n", judge);  break; case 4:judge = Funcdetele_poly_oly_2(poly2); printf("%s\n", judge);  break;  红色部分在程序之中根本找不到它们的声明或者定义，那么它们是什么？  4：其他的一些小问题 return之后你还有其他语句，那些语句永远不会被执行，你留着有什么用