I was trying to add 2 large numbers stored in two different circular linked lists. The result will be stored in another linked list. I tried to wrote the whole code but got stuck with my create() function. I'm only showing the part of the code which is not working.
Code:
struct node
{
int info;
struct node *next;
};
struct node *getnode(void)
{
struct node *p;
p=malloc(sizeof(struct node));
return p;
}
void create(struct node *r)
{
int num;
if(r==NULL)
{
r=getnode();
r->info=-1;
r->next=r;
} else
{
printf("Enter 5 digits or less");
scanf("%d",&num);
insafter(r,num);
}
}
int main(void)
{
struct node *p=NULL,*q=NULL;
int ch;
char ch2;
while(1)
{
printf("What do you wanna do?\n1.make a list\n2.sum of the integers\n3.exit");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
printf("Which list p or q");
scanf(" %c",&ch2);
switch(ch2)
{
case 'p':case 'P':
create(p);
break;
case 'q':case 'Q':
create(q);
break;
}
}
break;
case 2:
{
s=addint(p,q);
printf("The sum is");
for(s=s->next;s->info!=-1;s=s->next)
printf("%d",s->info);
}
break;
case 3:exit(0);
}
}
}
Firstly I choose the option 1. to make a list, then option p for creating the list named p, here it goes in the create() function. When I try to make a circular list using create() function it gets stuck to the if condition(which is always getting true), i.e r is always coming out to be NULL in each call to create(). Why is this happening?
Thanks