用链表实现多项式相加,而且有框架
Node::Node(double v, Node* n){
/*Your code goes here */
value=v;
next=n;
}
Node* Poly::getHead() {
/*Change code below*/
return head;
}
Poly::Poly(){
/*Your code goes here*/
head=NULL;
}
void Poly::append(double v){
/*Your code goes here */
Node newNode(v,NULL);
Node* NodePtr=getHead();
if(NodePtr==NULL){
head=&newNode;
}
else{
while(NodePtr->next!=NULL){
NodePtr=NodePtr->next;
}
NodePtr->next=&newNode;
}
}
int Poly::getLength(){
Node* temp;
int count=0;
temp=head;
if(temp==NULL){
count=0;
}
else{
while(temp!=0){
count++;
temp=temp->next;
}
}
return count;
}
Poly::Poly(const Poly &object){
Node *copyList,*ptr;
double value;
copyList=object.head;
if(!copyList){
head=nullptr;
}
ptr=head;
while (copyList)
{
value = copyList->value;
ptr=new Node(value, nullptr);
ptr=ptr->next;
copyList=copyList->next;
}
}
void Poly::print(){
Node* temp = getHead();
while(temp!=0){
printf("%.2f", temp->value);
temp = temp->next;
}
}
必须要用到node class和 poly class,所以以上是我写的代码
Node::Node(double v, Node* n){
/*Your code goes here */
value=v;
next=n;
}
Node* Poly::getHead() {
/*Change code below*/
return head;
}
Poly::Poly(){
/*Your code goes here*/
head=NULL;
}
void Poly::append(double v){
/*Your code goes here */
Node newNode(v,NULL);
Node* NodePtr=getHead();
if(NodePtr==NULL){
head=&newNode;
}
else{
while(NodePtr->next!=NULL){
NodePtr=NodePtr->next;
}
NodePtr->next=&newNode;
}
}
int Poly::getLength(){
Node* temp;
int count=0;
temp=head;
if(temp==NULL){
count=0;
}
else{
while(temp!=0){
count++;
temp=temp->next;
}
}
return count;
}
Poly::Poly(const Poly &object){
Node *copyList,*ptr;
double value;
copyList=object.head;
if(!copyList){
head=nullptr;
}
ptr=head;
while (copyList)
{
value = copyList->value;
ptr=new Node(value, nullptr);
ptr=ptr->next;
copyList=copyList->next;
}
}
void Poly::print(){
Node* temp = getHead();
while(temp!=0){
printf("%.2f", temp->value);
temp = temp->next;
}
}
必须要用到node class和 poly class,所以以上是我写的代码