-1

I have written the following code to print the 2 polynomial using linked list.When i run this program it prints nothing in the output.And also please tell if i pass the value from main() in this way then, when again the function is called will my start1 and start2 will be changed or they will remain NULL as initialized.

#include <iostream>
using namespace std;
struct Node
{
    int coeff;
    int exp;
    Node* next;
};
void create_Poly(int x,  int y , Node *start)
{
    Node *temp,*ptr;
    if(start==NULL)
    {
        temp=new Node;
        temp->coeff=x;
        temp->exp=y;
        temp->next=NULL;
    }
    else
    {
            ptr = start;
            while(ptr->next!=NULL)
            {
                ptr=ptr->next;
            }
             temp = new Node;
             temp->coeff=x;
             temp->exp=y;
             temp->next=NULL;
             ptr->next=temp;
    }
    //return start;
}
void display(Node *start)
{
    Node * print = start;
    while(print!=NULL)
    {
        cout<<print->coeff<<"^"<<print->exp<<"+";
        print=print->next;
    }
    cout<<endl;
}
int main()
{
    struct Node * start1=NULL,*start2=NULL;
    create_Poly(3,2,start1);
    create_Poly(3,2,start1);
    create_Poly(3,2,start1);
    display(start1);
    create_Poly(4,2,start2);
    create_Poly(4,2,start2);
    display(start2);
}
Ayush Mishra
  • 49
  • 1
  • 7
  • In `create_Poly()`, you pass the pointer `Node *start` by value. Changing contents of `start` inside `create_Poly()` will become effective but changing `start` ifself not. ("passed by value"). To change this, make this a reference: `Node *&start`. I guess the not printing (the other question) is a follow-up of this. – Scheff's Cat Sep 22 '18 at 10:34
  • 1
    Btw. `start` is never changed in `create_Poly()`. So, even with `Node *&start`, it will always execute the "then" branch of `if(start==NULL)`. (There might be more things I've not yet seen. I warmly recommend to run your code step-wise in a debugger to check what (really) happens.) – Scheff's Cat Sep 22 '18 at 10:39
  • when i am changing Node * start as Node *&start should i change **create_Poly(3,2,start1)**...ie.. while calling should i also change something? – Ayush Mishra Sep 22 '18 at 10:40
  • 1
    Nope. You don't need to change anything on call side. The compiler knows internally what to do if something is passed by reference instead of value. – Scheff's Cat Sep 22 '18 at 10:41
  • 1
    No, you don't have to change anything in the call to `create_Poly()`. btw. `NULL` is obsolete in C++, use the typesafe `nullptr` instead. – Swordfish Sep 22 '18 at 10:41

1 Answers1

0

it worked as being told by @Scheff the start is never changed i changed the start and here is the code

#include <iostream>
using namespace std;
struct Node
{
    int coeff;
    int exp;
    Node* next;
};
void create_Poly(int x,  int y , Node *&start)
{
    Node *temp,*ptr;
    if(start==NULL)
    {
        temp=new Node;
        temp->coeff=x;
        temp->exp=y;
        temp->next=NULL;
        start=temp;
    }
    else
    {
            ptr = start;
            while(ptr->next!=NULL)
            {
                ptr=ptr->next;
            }
             temp = new Node;
             temp->coeff=x;
             temp->exp=y;
             temp->next=NULL;
             ptr->next=temp;
    }
    //return start;
}
void display(Node *start)
{
    Node * print = start;
    while(print!=NULL)
    {
        cout<<print->coeff<<"^"<<print->exp<<"+";
        print=print->next;
    }
    cout<<endl;
}
int main()
{
    struct Node * start1=NULL,*start2=NULL;
    create_Poly(3,2,start1);
    create_Poly(3,2,start1);
    create_Poly(3,2,start1);
    display(start1);
    create_Poly(4,2,start2);
    create_Poly(4,2,start2);
    display(start2);
}

Output:

3^2+3^2+3^2+
4^2+4^2+

Live Demo on coliru

Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
Ayush Mishra
  • 49
  • 1
  • 7
  • Sorry, as you didn't react I copied your code to coliru to have a look. (I was curious.) After having done that, I thought I simply could add this to your answer. I hope it is OK. – Scheff's Cat Sep 22 '18 at 10:51
  • yes, you can do that, and meanwhile it was you only because of whom i figured out the mistake so no problem @Scheff – Ayush Mishra Sep 22 '18 at 11:08