this code for addind polynomials using linked lists showed segmentation error..any help pls..


 
Thread Tools Search this Thread
Top Forums Programming this code for addind polynomials using linked lists showed segmentation error..any help pls..
# 1  
Old 06-19-2011
this code for addind polynomials using linked lists showed segmentation error..any help pls..

the error occurs in the function() "add" used...
Code:
#include<stdio.h>
#include<malloc.h>
struct node
{
   int exp;
   int coef;
   struct  node * link;
};

struct node * create_list(struct node *,int,int);
void display(struct node *);
struct node * add(struct node *,struct node *);

int main()
{
        int i,n,co,ex,n2,i2,co2,ex2,n1;
        struct node *p1;
        struct node *p2;
        struct node *r;
        r=NULL;
        p1=NULL;
        p2=NULL;
        printf("\nHow many elements needed for first polyn?");
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
        printf("\nEnter the coeffecient and exponent respectively,in descending order of exponent\n");
        scanf("%d %d",&co,&ex);
        p1=   create_list(p1,co,ex);                /*creating first linked-list*/
        }
         printf("\nHow many elements needed for second polyn?");
        scanf("%d",&n2);
        for(i2=0;i2<n2;i2++)
        {
        printf("\nEnter the coeffecient and exponent respectively,in descending order of exponent\n");
        scanf("%d %d",&co2,&ex2);
        p2=   create_list(p2,co2,ex2);             /*creating 2nd polynomial linked-list*/
        }
        printf("the first polnomial is::\n");
        display(p1);				/*displaying*/
        printf("the second is::\n");
        display(p2);
   
        r=add(p1,p2);
   
        display(r);
        return(0);

}

struct node*  create_list(struct node *start,int c,int e)
{

        struct node *tmp;
        struct node *ptr;
        tmp=malloc(sizeof(struct node));
        tmp->coef=c;
        tmp->exp=e;
        tmp->link=NULL;

        if(start==NULL)
                {
                        start=tmp;
                }
        else

                {
                ptr=start;
                while(ptr->link!=NULL)
                {
                ptr=ptr->link;
                }
                ptr->link=tmp;

                }

        return(start);
}

void display(struct node *s)
{
 struct node *ptr=s;
 if(s==NULL)
{
        printf("You did not enter any elements\n");
}

else
{       printf("\nThe data  is::");
        while(ptr!=NULL)
{
        printf("+\t%d*x^%d\t",ptr->coef,ptr->exp);
        ptr=ptr->link;


}
}
}



struct node * add(struct node *a1_strt,struct node *a2_strt)
{
struct node *a3;
a3=malloc(sizeof(struct node));
struct node *a1=a1_strt;
struct node *a2=a2_strt;


        while(a1!=NULL && a2!=NULL)
        {
           if(a1->exp==(a2->exp))
                {
                a3->exp=a1->exp;
                a3->coef=(a1->coef)+(a2->coef);
                a3=a3->link;
                a1=a1->link;
		a2=a2->link;
                }
          else if((a1->exp)>(a2->exp))
                {
                  a3->exp=a1->exp;
                  a3->coef=a1->coef;
                  a3=a3->link;
		  a1=a1->link;
                }
        else
                {
                  a3->exp=a2->exp;
                  a3->coef=a2->coef;
                  a3=a3->link;
		  a2=a2->link;
                }
        }


           if(a1==NULL)
                {
                while(a2!=NULL)
                {
                  a3->exp=a2->exp;
                  a3->coef=a2->coef;
                  a3=a3->link;
		  a2=a2->link;
                }
                }
          else
                {
                 while(a1!=NULL)
                {
                 a3->exp=a1->exp;
                 a3->coef=a1->coef;
                 a3=a3->link;
	       	a1=a1->link;
                }
                }

return(a3);
}


Last edited by mscoder; 06-19-2011 at 02:25 PM..
# 2  
Old 06-19-2011
I only had a quick peek at your code, but this jumps out at me from your add function:

Code:
     while(a1!=NULL||a2!=NULL)
     {
           if(a1->exp==(a2->exp))
           {
                a3->exp=a1->exp;
                a3->coef=(a1->coef)+(a2->coef);

If a1 is NULL but a2 is not, you'll attempt to use a1 in the first if and that will segfault as you try to reference memory at 0.

Do you mean while( a1 != NULL && a2 != NULL) by chance?

A side note...
It would also be helpful in future to post the command and arguments you are passing to the programme when it is having issues. If the problem is triggered by data, someone building and testing the code you post might not see the issue.
# 3  
Old 06-19-2011
thanks for pointing out the "&" mistake in loop..but the code still shows segmentation error in the same functon()...
# 4  
Old 06-19-2011
Problem 1: You malloc a3 initially and fail to initialise ae->link. When you use it in your while, the second time through who knows what a3 is pointing to. Could be a legit address which then will contain corrupted data since you'll over write it, or a bad address which causes your seg-fault when you attempt to update what a3 points at.

Problem 2: You need to alloc more instances of a3 and add them to your list as you go.
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. OS X (Apple)

Segmentation fault when reading out a linked list

Hey everyone, this is my first time posting, so hopefully i won't commit some kind of egregious faux pas. :) Anyways, I'm trying to create and read back a simple linked list in C++. So far, I think I've built it and filled it with 10 different arrays of characters of about 22 characters each.... (2 Replies)
Discussion started by: gravity black
2 Replies

2. Programming

I need C++ Code for single linked list

I need C++ Code for single linked list With operations as 1)insert at any position 2)delete any 3)change the data of any position (2 Replies)
Discussion started by: girija
2 Replies

3. Shell Programming and Scripting

Format data to columns addind spaces

Hi all, I have a problem to format data from different database queries into one look. The input data are as follows, every line has the same number of values but a different number of characters: adata, bdata, cdata, ddata fffdata, gdata, hdata, idata jdata, kdata, ... (6 Replies)
Discussion started by: old_mike
6 Replies

4. Programming

Problem with linked lists

I am working on a problem for which I need to use a linked list of a sort. For this particular application I need each node to refer to a set of other nodes. A simplified version of the code looks as follows: #include <stdio.h> #include <stdlib.h> struct record { int id; struct record... (1 Reply)
Discussion started by: brinch
1 Replies

5. Shell Programming and Scripting

why is this code generating syntax error?pls help

#!/bin/sh copy() { source=`stat -c %s $1` dest=0 cd $2 while ;do cp $1 $2 & pct=`((100 * $dest) / $source )` dest=`dest+1` echo -en ".$pct%\b\b\b" sleep 1 done } echo "starting now" copy /file1 /tmp (3 Replies)
Discussion started by: wrapster
3 Replies
Login or Register to Ask a Question