0

在链接列表中,我想将每个节点与另一个节点进行比较,如果它们相似,则将它们加在一起。我在浏览链接列表然后添加在一起时遇到问题。

我的 main.cpp:

  #include <cstdlib>
  #include "list.h"
  int main(){
  Poly poly=new_list();
  Poly poly2=new_list();
  Poly merged= new_list();
  int n;
  int deg;
  float coef;
  n=1;
  while (n==1)
     {
      cout<<"Enter coefficient ";
      cin>> coef;
      cout<<"Enter degree ";
      cin>>deg;
      insert_front(&poly,coef,deg);
      cout<<"Enter 1 to continue or 0 to break ";
      cin>>n;
  }
  print_list(poly);
  cout<<"sorted\n";
  reduce(poly);

}

这是我的头文件:

//list.h                                                                       
#include <iostream>
using namespace std;
#ifndef LIST_H
#define LIST_H
struct Term {
    int deg;
    float coef;
    Term *next;
};
typedef Term* Poly;
Poly new_list();
void insert_front(Poly* ppoly,int deg, float coef);
void print_list(Poly poly);
void delete_front(Poly* ppoly);
bool is_empty(Poly poly);
Poly merge(Poly *ppoly,Poly *ppoly2);
void split_list(Poly* ppoly,Poly *ppoly2);
void mergesort(Poly* pl);
void reduce(Poly poly);
#endif

我拥有将用户系数和多项式的度数并将它们打印出来的所有功能,还将它们从最低度到最高度归并排序。

功能:

list.cpp
#include "list.h" Poly new_list(){ Poly poly = 0; 返回聚;} void insert_front(Poly* ppoly,int deg, float coef){ Term* t; t = 新术语;t->coef=coef; t->度=度;t->下一个 = *ppoly; *多聚 = t; 返回; } void print_list(Poly poly){ 术语* p; p = 聚; if(p == 0) cout << "--- 空列表 ---"; while(p !=0){ cout << p->deg<<"x^"<coef<<" + "; p = p->下一个;} cout << endl; } void delete_front(Poly* ppoly){ 期限* t; if( !is_empty(*ppoly) ){ // 列表不为空
t = (*ppoly); *ppoly = (*ppoly)->下一个;删除 t; } }

bool is_empty(Poly poly){
  return (poly == 0); //return true if list empty                                                                                                                                                                                 
}

Poly merge(Poly* ppoly, Poly* ppoly2){
    Term **pp;
    Poly merged, list1,list2;
    merged= new_list();
    list1 = *ppoly;
    list2 = *ppoly2;
    pp= &merged;
    while(list1 != NULL && list2 != NULL){
        if(list2->coef > list1->coef){
           *pp = list1;
           list1 = list1->next;
           (*pp)->next = NULL;
        }else{
           *pp = list2;
           list2 = list2->next;
           (*pp)->next = NULL;
        }
        pp = &( (*pp)->next );
    }
    if(list1 != NULL)
        *pp = list1;
    if(list2 != NULL)
        *pp = list2;

    *ppoly = NULL;
    *ppoly2 = NULL;
    return merged;
}
void split_list(Poly* ppoly, Poly* ppoly2){
    Poly l1= *ppoly;
    Poly l2= *ppoly;
    Poly* pp = &l1;
    while( l2 != NULL){
        l2 = l2->next;
        if(l2 != NULL){
            l2 = l2->next;
            pp = &((*pp)->next);
        }
    }
    l2 = *pp;
    (*pp) = NULL;
    *ppoly=l1;
    *ppoly2=l2;
}
void mergesort(Poly* pl){
    Poly l1 = *pl;
    Poly l2 = new_list();
    Poly merged = new_list();
    if(l1 == NULL || l1->next == NULL)
        return; //sorted or empty                                                                                                                                                                                                 
    split_list(&l1,&l2);
    mergesort(&l1);
    mergesort(&l2);
    merged = merge(&l1,&l2);
    *pl = merged;
}
void reduce(Poly poly){
  mergesort(&poly);
  print_list(poly);
  int i=0;
  cout<<"combining like terms:"<<endl;
  Term* p;
  p=poly;
  if (p==0)
    cout<<"---empty list---";
  while(i=0){
    if (poly->coef==(poly->next)->coef){
      p->deg=(poly->deg)+((poly->next)->deg);
      poly=p;
      i=1;
    }
  }
  print_list(poly);
}

我已经这样做了几天,但无法正常工作。问题出在reduce()功能上。

例如,如果我有:2x^2+2x^2+4x^2+3x^5,它将打印8x^2+3x^5.

4

1 回答 1

1

这里有很多问题。让我们从一个简单的案例开始:

int main()
{
  Poly poly=new_list();

  insert_front(&poly,2,5);
  insert_front(&poly,2,5);

  reduce(poly);
  print_list(poly); // we hope for 4x^5

  return(0);
}

...但我们得到 2x^5。(请注意,如果可能,您应该单独测试一个函数——不需要交互性或合并或所有其他东西。)

现在看看reduce

void reduce(Poly poly){
  mergesort(&poly);
  print_list(poly);
  int i=0;
  cout<<"combining like terms:"<<endl;
  Term* p;
  p=poly;
  if (p==0)
    cout<<"---empty list---";
  while(i=0){
    if (poly->coef==(poly->next)->coef){
      p->deg=(poly->deg)+((poly->next)->deg);
      poly=p;
      i=1;
    }
  }
  print_list(poly);
}

你有“ceof”和“degree”错误的方式,但这只是变量命名的问题(尽管这让我的眼睛受伤)。

while(i=0)在我认为你的意思的地方使用while(i==0)。如所写,它是一个赋值为 的赋值0,因此控制永远不会进入循环。假设我们修复它以便我们进入循环:

int i=0;
Term* p;
p=poly;
while(i==0){
  if (poly->coef==(poly->next)->coef){
    p->deg=(poly->deg)+((poly->next)->deg);
    poly=p;
    i=1;
  }
}

如果前两项不匹配,则i保持为零,我们将永远留在循环中。
如果前两项确实匹配,i=1并且我们离开循环,则不会考虑其他项。
在离开循环之前,我们修改第一项——然后设置poly=p. 但是poly已经等于p; 这一步什么都不做,第二个任期还在。

我希望这足以让你朝着正确的方向前进。

于 2012-10-10T18:04:59.423 回答