-4

Good day! I am using Borland C++ to make my assignment in DSA, and I am looking for an example to Add and Multiply Polynomials using Linked List Implementation.

Luckily, I found 1 which suits to my assignment.

But the only problem is that it gives me an error which it states :

"Cannot convert 'void*' to 'polyNode*'

Help me fix this problem.

Here is the sample code:

#include<stdio.h>
#include<malloc.h>
typedef struct polyNode *polyPointer;

struct polyNode {
int coef;
int expon;
polyPointer link; } polyNode;

void printPoly(polyPointer);
void insertTerm(int,int,polyPointer);
polyPointer addPoly(polyPointer,polyPointer);

main()
{
    polyPointer p1,p2,p3;
    p1=malloc(sizeof(polyNode));
    p1->link=NULL;

    p2=malloc(sizeof(polyNode));
    p2->link=NULL;

    insertTerm(2,2,p1);
    insertTerm(5,5,p1);
    insertTerm(7,4,p2);
    insertTerm(-3,0,p2);
    insertTerm(4,4,p2);
    p3=addPoly(p1,p2);
    printf("\n");             
    printPoly(p3);
    printf("\n");
    printPoly(p2);
    printf("\n");
    printPoly(p1);
}

void insertTerm(int coef,int expon,polyPointer root)
{
    if(root->link)
    {                  
        while(root->link && root->link->expon>=expon)
        root=root->link;
    }

    if(root->expon==expon)
    {
        root->coef+=coef;
        return;
    }

    polyPointer temp;
    temp=malloc(sizeof(polyNode));
    temp->coef=coef;
    temp->expon=expon;

    temp->link=root->link;
    root->link=temp;
}

void printPoly(polyPointer root)
{
    printf("\n");
    while(root->link)
    {
        printf("(%dx^%d)+",root->link->coef,root->link->expon);
        root=root->link;
    }
}

polyPointer addPoly(polyPointer p1, polyPointer p2)
{
    polyPointer p3;
    p3=malloc(sizeof(struct polyNode));
    p3->link=NULL;

    while(p1->link && p2->link)
    {
        while(p2->link && (p1->link->expon >= p2->link->expon))
        {
            insertTerm(p2->link->coef,p2->link->expon,p3);
            p2=p2->link;
        }

        while(p2->link && p1->link && (p2->link->expon >= p1->link->expon))
        {
            insertTerm(p1->link->coef,p1->link->expon,p3);printf("1");
            p1=p1->link;
        }
    }

    while(p1->link)
    {
        insertTerm(p1->link->coef,p1->link->expon,p3);
        p1=p1->link;
    }

    while(p2->link)
    {
        insertTerm(p2->link->coef,p2->link->expon,p3);
        p2=p2->link;
    }

    return p3;
}
alk
  • 69,737
  • 10
  • 105
  • 255
  • 2
    can you post some code? – Sameer Naik Jul 11 '16 at 06:10
  • Post and format the code **in your question**. And heed this: There better be more than just that, as the first error we'll get as-shown will be the compiler not having a clue what a `polyPointer` is. [Minimal, *complete*, and verifiable example](https://stackoverflow.com/help/mcve) is on the menu. – WhozCraig Jul 11 '16 at 06:18
  • Add the code by editing your original question, do not add it like this in the comments. – Rishikesh Raje Jul 11 '16 at 06:18
  • 2
    It the error occurs on line `p1=malloc(sizeof(polyNode));` it just means that you are compiling this as C++. **C and C++ are different languages**... Just ensure that you source has a `.c` extension and that the compiler is configured as a C compiler. If it occurs on another line, please say what line it is – Serge Ballesta Jul 11 '16 at 06:41
  • Hello Serge! what compiler did you use? I am using borland and changes the source to .c but still it gives me the same error. thank you :) – Florence Taburda Jul 11 '16 at 07:04
  • Serge! Right now I am using turbo c and my source is at .c extension, I have an error on line 40 and line 56. – Florence Taburda Jul 11 '16 at 07:09
  • And line 40 and 56 are which line of the code you show, please? *sigh* – alk Jul 11 '16 at 08:42
  • My guts tells me your assignment is in **C++** not **C** and you found only **C** template for it (rather then code it yourself) Why not port it to **C++** ? rewrite `malloc/free` to `new/delete` ... Also I strongly advice to read this [bds 2006 C hidden memory manager conflicts](http://stackoverflow.com/a/18016392/2521214) as you are using `struct` so add the solution to it to avoid problems. – Spektre Jul 14 '16 at 07:46
  • C allows an untyped `void*` to be assigned to a typed pointer variable. C++ does not. Thus `p1=malloc(...)` is valid in C but is invalid in C++, a type-cast is needed: `p1=(polyPointer)malloc(...)`. The Borland C++ compiler compiles a `.c` file as C unless you specify the `-P` switch to force compiling as C++. – Remy Lebeau Jul 28 '16 at 18:16

1 Answers1

1

Compilers will issue warnings or errors depending on compiler settings (you could possibly set it so that this is no longer flagged as an error). But I would do the following ... malloc returns a void* and this provokes a type mismatch since you assign it to a polyPointer. If you cast the return value to the appropriate type, this should eliminate the error as in:

p1= (polyPointer)malloc(sizeof(polyNode));

idem for all other allocations.

clarasoft-it
  • 201
  • 1
  • 5