Sponsored Content
Top Forums Programming this code for addind polynomials using linked lists showed segmentation error..any help pls.. Post 302532042 by mscoder on Sunday 19th of June 2011 01:09:52 PM
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..
 

5 More Discussions You Might Find Interesting

1. 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

2. 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

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

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

5. 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
All times are GMT -4. The time now is 12:01 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy