Star Rider
Star Rider

Reputation: 107

Why passing an argument of structure pointer in a function which creates link list node isn't working?

I'm trying to make link list which represents polynomial. Each node consists of coefficient,power of x,link to the next node I have tried this program without passing arguments it worked . but when when tried passing arguments the link list isn't being created. pls help to rectify my program..

*****THIS IS MY PROGRAM WITH STRUCTURE POINTERS AS AN ARGUMENT IN THE INSERT FUNCTION*****


#include<stdio.h>
#include<stdlib.h>

struct node
{
float coff;
int expo;
struct node *next;
};

struct node *start1=NULL,*current1=NULL;

void insert(struct node *start,struct node *current)
{
struct node *new_node;

new_node=(struct node*)malloc(sizeof(struct node));

if(start==NULL)
{
    start=new_node;
    current=new_node;
}
else
{
    current->next=new_node;
    current=new_node;
}

printf("\nEnter coefficient:");
scanf("%f",&new_node->coff);
printf("\nEnter power of x:");
scanf("%d",&new_node->expo);
new_node->next=NULL;
}
void show(struct node *start)
{
struct node *ptr;
ptr=start;

    while(ptr!=NULL)
    {
        printf(" %.2fx^%d",ptr->coff,ptr->expo);
        ptr=ptr->next;
    }
}
void find(float scoff,int sexpo,struct node *start)
{
//scoff=search coefficient, sexpo=search exponent
int flag=0;
    struct node *ptr;
    ptr=start;

    while(ptr!=NULL)
    {
        if(ptr->coff==scoff && ptr->expo==sexpo)
        {
          printf("your term found-> %.1fx^%d",ptr->coff,ptr->expo);
          flag=1;
          break;
        }
        else
        ptr=ptr->next;
    }

    if(flag==0)
        printf("\nSorry term couldn't be found!!");
    else
        printf("\nSearch success!!");

 }
 int main()
 {
 int c=1,ex=0;
 float cf=0;
 while(1)
 {
    insert(start1,current1);
    printf("\nWant to continue(1/0)?");
    scanf("%d",&c);
        if(c==0)
            break;
 }
 show(start1);
 while(1)
 {
    printf("\nEnter coff and expo respectively:");
    scanf("%f%d",&cf,&ex);
    find(cf,ex,start1);

    printf("\nWant to continue(1/0)?");
    scanf("%d",&c);
        if(c==0)
            break;
 }

 return 0;
 }

Upvotes: 0

Views: 49

Answers (1)

blazs
blazs

Reputation: 4845

The problem is that you're modifying a local copy of the start and current pointers (i.e. the copies passed as the function arguments), thus not actually changing the linked list. You should instead modify the start1 and current1 pointers. There's no need for the insertion function to take start and current parameters as it can simply modify the global variables. (I should note that this is a bad design choice.)

More specifically, the problem is when initializing the list in

if (start==NULL)
{
    start=new_node;
    current=new_node;
}

you only modify the local copy that gets destroyed when it goes out of scope. So start1 and current1 are still NULL after the function returns.

Change the insert function to

void insert(struct node *start,struct node *current)
{
struct node *new_node;

new_node=(struct node*)malloc(sizeof(struct node));

if(start1==NULL)
{
    start1=new_node; // You previously  had start instead of start1
    current1=new_node; // You previously had current instead of current1
}
else
{
    current1->next=new_node;
    current1=new_node;
}

printf("\nEnter coefficient:");
scanf("%f",&new_node->coff);
printf("\nEnter power of x:");
scanf("%d",&new_node->expo);
new_node->next=NULL;
}

Beware: after this modification, you can call insert() (i.e. no need for insert to have parameters) as the function will operate on the global variables. The other option---better, but more time-consuming---is to somehow initialize the linked list and then use the functions in their current form (except for minor modifications).

Upvotes: 1

Related Questions