Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7554055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:13:59+00:00 2026-05-30T11:13:59+00:00

Shortly: I have two linear linked lists which represent a polynomial. I have to

  • 0

Shortly: I have two linear linked lists which represent a polynomial. I have to multiply them. I’ve wrote everything down here. The only problem is that I get a segmentation fault on one line (if(n.grad<r->a.grad) – also marked in the code below).

I’ve tried this program in Borland and it works!

In CodeBlocks or MinGW it simply crashes.

#include <iostream>

using namespace std;

struct poli
{
    int grad;
    float coe;
};
struct Nod
{
    poli a;
    Nod *adr;
};
Nod *v,*sf,*v1,*vs,*vp;
void add_first(Nod *&v, poli n)
{
    if(v)
    {
        Nod *p;
        p=new Nod;
        p->a=n;
        p->adr=v;
        v=p;
    }
    else
    {
        v=new Nod;
        sf=v;
        v->a=n;
        v->adr=0;
    }
}
void add_last(Nod *&v, poli n)
{
    if(!v)
    {
        v=new Nod;
        v->a=n;
        v->adr=0;
    }
    else
    {
        Nod *p,*sf;
        sf=v;
        while(sf->adr)
            sf=sf->adr;
        p=new Nod;
        p->a=n;
        p->adr=0;
        sf->adr=p;
        sf=p;
    }
}
void add_before(Nod *v, int val, poli val_add)
{
    Nod *p, *q;
    if(v->a.grad==val)
    {
        p=new Nod;
        p->a=val_add;
        p->adr=v;
        v=p;
    }
    else
    {
        p=v;
        while(p->adr->a.grad!=val&&p->adr->adr)
            p=p->adr;
        if(p->adr->a.grad==val)
        {
            q=new Nod;
            q->a=val_add;
            q->adr=p->adr;
            p->adr=q;
        }
    }
}
void produs(Nod *v, Nod *v1, Nod *&vp)
{
    Nod *p,*q,*r;
    int gasit;
    poli n;
    p=v;
    while(p)
    {
        q=v1;
        while(q)
        {
            n.grad=p->a.grad+q->a.grad;
            n.coe=p->a.coe*q->a.coe;
            r=vp;
            gasit=0;
            while(r)
            {
                if(n.grad==r->a.grad)
                {
                    r->a.coe+=n.coe;
                    gasit=1;
                }
                r=r->adr;
            }
            if(!gasit)
            {
                r=vp;
                if(n.grad<r->a.grad)  /////////////// HERE I get the call stack
                    add_first(vp,n);
                else
                {
                    while(r->adr&&n.grad>r->adr->a.grad)
                        r=r->adr;
                    if(r->adr&&n.grad<r->adr->a.grad)
                        add_before(vp,r->adr->a.grad,n);
                    else
                        add_last(vp,n);
                }
            }
            q=q->adr;
        }
        p=p->adr;
    }
}
void tipar(Nod *v)
{
    Nod *p;
    p=v;
    while(p)
    {
        cout<<"+"<<p->a.coe<<"x^"<<p->a.grad;
        p=p->adr;
    }
}
int main()
{
    int n,m,i;
    poli a;
    cout<<"Cate elemente are polinomu' 1?";
    cin>>n;
    for(i=0;i<n;i++)
    {
        cout<<"Baga gradu'";
        cin>>a.grad;
        cout<<"Introdu-mi coe";
        cin>>a.coe;
        add_last(v,a);
        add_last(vs,a);
    }
    cout<<"Cate elemente are polinomu' 2?";
    cin>>m;
    for(i=0;i<m;i++)
    {
        cout<<"Baga gradu'";
        cin>>a.grad;
        cout<<"Introdu-mi coe";
        cin>>a.coe;
        add_last(v1,a);
    }
    produs(v,v1,vp);
    tipar(vp);
    return 0;
}

Here is the call stack window contents:

#0 004016C4 produs (v=0x4d25a0, v1=0x4d26b8, vp=@0x474018) at F:\Programe\ma ballz(23.02) (F:\Programe\suma polinom\main.cpp:142)
#1 004019F2 main () at F:\Programe\ma ballz(23.02) (F:\Programe\suma polinom\main.cpp:195)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T11:14:01+00:00Added an answer on May 30, 2026 at 11:14 am

    looks like vp is NULL, or garbage, and then you do r = vp…
    Probably NULL because it didn’t fall in the loop.
    It has a garbage value because you never initialize it with a value. You just declare it in the beginning.

    Anyway you should learn to give meaningful names to your variables so it will be more readable and therefor maintainable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have two lists named Categories and Products.I paged Categories List in a dataview(1
I have a small project that I will be working on shortly that collects
I am shortly starting a project, which requires messages to be held in a
Okay, I will shortly be starting down the path of windows mobile development. I
I want to make a notification system. Shortly.. to compare two dates, the only
I have a PHP based web application which is currently only using one webserver
i simply have two fields. dtStartTime and dtStartDate. I want to do a query
We have a Windows Service which runs on 2003 Server. It opens a source
I suspect that I will shortly have a need to write an integration library
I have below probably a bunch of problems but let's simplify them with easier

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.