首页 文章

添加两个节点并在比较时删除一个节点[关闭]

提问于
浏览
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()
void insert_front(Poly * ppoly,int deg,float coef){Term * t; t =新术语; T-> COEF = COEF; t-> deg = deg; t-> next = * ppoly; * ppoly = t;返回; } void print_list(Poly poly){Term * p; p = poly; if(p == 0)cout << "--- empty list ---"; while(p!= 0){cout << p-> deg << "x^" <coef << " + "; p = p-> next; } cout << endl; } void delete_front(Poly * ppoly){Term * t; if(!is_empty(* ppoly)){// list不为空
t =(* ppoly); * ppoly =(* ppoly) - > 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 回答

  • 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 ;这一步什么都没做,第二个任期仍然存在 .

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

相关问题