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

adding two nodes and deleting one while comparing it

本文关键字:节点 删除 一个 比较 两个 添加      更新时间:2023-10-16

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

我的主.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<<"sortedn";
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"多边形new_list(){Poly-Poly=0;回归poly;}void insert_front(多边形,int度,浮动系数){期限*t;t=新术语;t->coeffe=coeff;t->deg=deg;t->next=*ppoly;*ppoly=t;回来}void print_list(多边形-多边形){术语*p;p=poly;如果(p==0)cout<lt;"---空列表---";while(p!=0){cout<lt;p->deg<lt;"x^"<coeff<lt;"+";p=p->下一个;}cout<lt;endl;}void delete_front(多边形){期限*t;if(!is_empty(*ppoly)){//list不为空
t=(*ppolly);*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

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

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"answers"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。但CCD_ 11已经等于CCD_ 12;这一步毫无作用,第二任期仍然存在。

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