添加两个节点并在比较时删除一个节点

时间:2012-10-10 17:21:10

标签: c++ linked-list nodes

在链接列表中,我想将每个节点与另一个节点进行比较,如果它们与术语相似,则将它们一起添加。我有一个问题,通过链表,然后加在一起。

我的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){         期限* t;         t =新术语;         叔&GT; COEF = COEF;         t-> deg = deg;         t-> next = * ppoly;         * ppoly = t;         返回;     }     void print_list(Poly poly){         期限* p;         p = poly;         if(p == 0)             cout&lt;&lt; “---空名单---”;         while(p!= 0){           cout&lt;&lt; p-> deg&lt;&lt;“x ^”&lt; coef&lt;&lt;“+”;             p = p-&gt; next;         }         cout&lt;&lt; ENDL;     }     void delete_front(Poly * ppoly){       期限* t;       if(!is_empty(* ppoly)){// list is not empty
          t =(* ppoly);           * ppoly =(* ppoly) - &gt; next;           删除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

1 个答案:

答案 0 :(得分: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;这一步没有做,第二个任期仍在那里。

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