1. ## add two large numbers

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:
{
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

2. Two things :
1) Are you sure that case 'p' or 'q' always work fine ?
You should have also a default case.
Better remove the space before scanf(" %c",&ch2);, you may read \n.

2) (More question than notation) As far as i can understand from your code you want to add.. etc two large numbers, but your struct stores more than one digit right ?
Now imagine those lists (as |X| a node with a number in it)

p = |1|2|3|45|5023|
q = |2|6|11|4|

How would you add these two lists ?
See 5023 should be added to the whole q list and the rest is just p.

3. Surely you're not expecting the function to be able to change the value of r as seen by the program? (It can change the value pointed to by r, but not make it point somewhere else, and if r doesn't point to anything valid then you're stuck.)

And I'm not sure what ch4 means by the bit in red; the point of the code in red is to remove the \n from what is read in? That looks fine to me.

4. Originally Posted by tabstop
Surely you're not expecting the function to be able to change the value of r as seen by the program? (It can change the value pointed to by r, but not make it point somewhere else, and if r doesn't point to anything valid then you're stuck.)

And I'm not sure what ch4 means by the bit in red; the point of the code in red is to remove the \n from what is read in? That looks fine to me.
This is what I'm doing. For the first node(which I'm making it a header), the info field is -1, and the next field is that node itself as it's the first node in a circular list. For that I'm allocating space for the pointer using getnode(), the next time a node is inserted I'm inserting it after the header node. But I dont understand what you're saying, how is it NULL everytime?

And yes I'm using space in scanf to ignore the \n in the buffer.

5. Originally Posted by BEN10
This is what I'm doing. For the first node(which I'm making it a header), the info field is -1, and the next field is that node itself as it's the first node in a circular list.
And then you throw it away. You've seen this, I'm sure:
Code:
```#include <stdio.h>

void change(int i) {
i = 10;
}

int main() {
int i = 5;
change(i);
printf("%d\n", i);
return 0;
}```
It prints five, right? Changes made in functions disappear when the function ends.

(If you like, a closer example:
Code:
```#include <stdio.h>
#include <stdlib.h>

void change (int *i) {
i = NULL;  //make it go away
}

int main(void) {
int x = 5;
int *i = &x;
printf("%p\n", i);
change(i);
printf("%p\n", i);
return 0;
}```

6. Thanks. Now I get it. I've corrected it and that problem has gone. But now it's not printing the sum, but I'll try it a bit and then come with the code(if it troubles me).

7. Ok. Now after spending some time with the code, I found that all mistake was done in the creation of circular list. So I decided to write another program for circular list. Thsi is what I wrote.
Code:
```#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *next;
};
void display(struct node *p)
{
struct node *q;
for(q=p;((q->next)->info)!=-1;q=q->next)
printf("\n%d",q->info);
}
struct node *getnode(void)
{
struct node *p;
p=malloc(sizeof(struct node));
return p;
}
void insafter(struct node *p,int x)
{
struct node *q;
if(p==NULL)
{
printf("void insertion");
getch();
exit(1);
}
q=getnode();
q->info=x;
q->next=p->next;
p->next=q;
}
void create(struct node **r)
{
int num;
if(*r==NULL)
{
*r=getnode();
(*r)->info=-1;
(*r)->next=*r;
}
else
{
printf("Enter number");
scanf("%d",&num);
insafter(*r,num);
}
}
int main(void)
{
struct node *p=NULL;
int ch;
while(1)
{
printf("What do you wanna\n1.make a list\n2.display\n3.exit");
scanf("%d",&ch);
switch(ch)
{
case 1:
create(&p);
break;
case 2:
display(p);
break;
case 3:
exit(0);
break;
}
}
getch();
}```
Now the problem with the code is that it doesn't displays the contents of the node properly. Suppose that I enter the nodes with value of (except for the very first node which has info=-1) 2,3. Then I choose the option of displaying it and it prints -1 and 3 not 2. Why is this happening? I'm not sure whether my insafter() function is correct or not.
And one more thing, the way I understand the code, please tell me if I'm correct or not. Firstly p points to the header node with info field -1. Then as I insert more nodes then q points to them in succession, I mean if I enter 2 for the second node's info, q(in insafter()) points to this node, as I ask for another node with info 3, q points to the new node(with info 3). My doubt is, Is p pointing always to the header node and it's q which points to the newly allocated node or it's p which is moving through the nodes?

8. If you want to print the entire list, you should probably let your for loop run to the end of the list instead of stopping early.

9. What's wrong with my for loop? It's starting from the header till the last node.

10. Originally Posted by BEN10
What's wrong with my for loop? It's starting from the header till the last node.
That's the point. It stops when it reaches the last node. That means it doesn't actually execute the body of the for loop for the last node....

11. I'm not sure why you're using circular lists. Or are you?
I was trying to add 2 large numbers stored in two different circular linked lists.
It seems like you're making too much out of this. What's the point of them being circular? It just makes running through them harder than it has to be.

Quzah.

12. Originally Posted by tabstop
That's the point. It stops when it reaches the last node. That means it doesn't actually execute the body of the for loop for the last node....
Ok. Now my question will sound slightly stupid but here it is. If I input the nodes in this order
-1->2->3 then should I print it like the same order or is it ok with this order also -1->3->2? By the way -1 is the info field of the header node.

Originally Posted by quzah
I'm not sure why you're using circular lists. Or are you?It seems like you're making too much out of this. What's the point of them being circular? It just makes running through them harder than it has to be.

Quzah.
Actually this all started when I was studying the book. A simple example was given in the book of adding two large numbers. So I decided to write a program to add two large numbers using circular lists. But then I got stuck in the very first thing i.e in constructiing a circular list. It's just for practice.

13. Originally Posted by BEN10
Ok. Now my question will sound slightly stupid but here it is. If I input the nodes in this order
-1->2->3 then should I print it like the same order or is it ok with this order also -1->3->2? By the way -1 is the info field of the header node.
Whatever you want. You're building your list in a strange way to try to print it out in the order it was given, though.

14. Originally Posted by tabstop
Whatever you want. You're building your list in a strange way to try to print it out in the order it was given, though.
Now I'm done with the circular list creation and it prints the in the correct order.

15. Yipee. I've made the code for adding two large integers and it's working perfectly fine. Thanks tabstop for your help.
By the way will it be efficient to find the factorial of 3 digit numbers using this method which uses circular list. (by placing the digits of the answer in the nodes)