请大佬帮我看一下这个C++程序错在哪了?没有编译错误但不能输出,谢谢大佬

#include<iostream>usingnamespacestd;typedefstructSNode*Polynomial;structSNode{intcoef... #include <iostream>
using namespace std;
typedef struct SNode* Polynomial;
struct SNode{
int coefficient;//系数
int index;//指数
Polynomial next;
};
Polynomial attach(int num1=0,int num2=0){
Polynomial list = new(struct SNode);
list->coefficient = num1;
list->index = num2;
list->next = NULL;
return list;
}
Polynomial ReadyPoly(int n){
int number1,number2;
Polynomial list,head = list;
cout<<"Please input the coeddicients and index"<<endl;
while(n--){
cin>>number1>>number2;
list = attach(number1,number2);
list = list->next;
}
return head;
}
Polynomial add( Polynomial & list1,Polynomial & list2){
Polynomial list;
Polynomial start = list;
while(list1||list2){
if((list1->index)>(list2->index)){
list = attach(list1->coefficient,list1->index);
cout<<"larger"<<endl;
}else if(list1->index==list2->index){
int coef = list1->coefficient+list2->coefficient;
list = attach(coef,list1->index);
}else{
list = attach(list2->coefficient,list2->index);
}
list = list->next;
list2 = list2->next;
list1 = list1->next;
}
return start;
}
void printPoly(Polynomial & list){
while(list){
cout<<"( "<<list->coefficient<<","<<list->index<<" )"<<endl;
list = list->next;
}
}
int main()
{
int count,n=2;
Polynomial list1=NULL,list2=NULL;
cout<<"Please input times of input."<<endl;
cin>>count;
list1 = ReadyPoly(count);
cin>>count;
list2 = ReadyPoly(count);
Polynomial list=NULL;
list = add(list1,list2);
printPoly(list);
return 0;
}
初学数据结构和C++还望大家指导,谢谢!
展开
 我来答
帐号已注销
2017-12-03 · TA获得超过463个赞
知道小有建树答主
回答量:598
采纳率:65%
帮助的人:275万
展开全部
/**
 * 编译器:vs2010
 * @fencaide 2017年12月3日12:42:30
 */
#include <iostream>
using namespace std;
typedef struct SNode* Polynomial;
struct SNode{
    int coefficient;//系数 
    int index;//指数 
    Polynomial next;
};
Polynomial attach(int coefficient=0, int index=0){
    Polynomial list = new(struct SNode);
    list->coefficient = coefficient;
    list->index = index;
    list->next = NULL;
    return list;
}
Polynomial ReadyPoly(int n){
    int number1,number2; 
Polynomial head = NULL;
Polynomial list = NULL;
    cout<<"Please input the coeddicients and index"<<endl;
    while(n--){
        cin>>number1>>number2;
        Polynomial elem = attach(number1, number2);
if(NULL == head){
//第一次赋值head
list = elem;
head = list;
}
else{
list->next = elem;
list = list->next;
}
    }
    return head;
}
/*
Polynomial CopyPoly(Polynomial &from){
Polynomial iterator = from;
Polynomial head = NULL;
Polynomial list = NULL;
while(iterator->next){
Polynomial elem = attach(iterator->coefficient, iterator->index);
if(NULL == head){
//第一次赋值head
list = elem;
head = list;
}
else{
list->next = elem;
list = list->next;
}
    }
return head;
}*/

Polynomial add( Polynomial & list1,Polynomial & list2){
    Polynomial head = NULL;
Polynomial elem = NULL;
Polynomial list = NULL;
//Polynomial List = CopyPoly(list1);
//这里假设list1 和list2 都是合并同类项且按照次数由高到低排序后的多项式
while(1){
if(NULL == list1 && NULL == list2){
break;
}
else if(NULL == list1){
list->next = list2;
break;
}
else if(NULL == list2){
list->next = list1;
break;
}
else if(list1->index == list2->index){
elem = attach(list1->coefficient + list2->coefficient, list1->index);
//都后移
list1 = list1->next;
list2 = list2->next;
}
else if(list1->index > list2->index){
elem = attach(list1->coefficient, list1->index);
//1后移
list1 = list1->next;
}
else{
elem = attach(list2->coefficient, list2->index);
//2后移
list2 = list2->next;
}

//添加到list中去
if(NULL == head){
//第一次赋值head
list = elem;
head = list;
}
else{
list->next = elem;
list = list->next;
}
}
return head;
}

void printPoly(Polynomial &list){
Polynomial p = list;
    while(p){
        cout<<"( "<<p->coefficient<<","<<p->index<<" )"<<endl;
        p = p->next;
    } 
}

void gc(Polynomial &list){
Polynomial p = list;
cout<<"Program is free memory..."<<endl;
while(p){
Polynomial q = p;
p = p->next;
delete q;
}
}


int main()
{
    int count,n=2;
    Polynomial list1=NULL,list2=NULL;
    cout<<"Please input times of input."<<endl;
    cin>>count;
    list1 = ReadyPoly(count);
printPoly(list1);

cout<<"Please input times of input."<<endl;
    cin>>count;
    list2 = ReadyPoly(count);
printPoly(list2);

cout<<"Add result is:"<<endl;
    Polynomial list = add(list1, list2);;
    printPoly(list);

gc(list1);
gc(list2);
gc(list);

getchar();
getchar();
    return 0;
}

推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式