Hi guys. I'm trying to learn how to add two long integers in a linked list.
So 1->2->3->NULL and 4->5->6->NULL should give me 5->7->9->NULL
I know how to approach the problem but the problem is, the thing I'm trying to do isn't working and the compiler gives me an error.
So far, this is a very very rough draft of my code and I'm basically going to change it later but for the purposes of testing, this should be enough.
So my method of creating the linked list is this:
Code:
void create_list(struct list **ptr,char *input) {
int i;
struct list *tmp;
for(i = 0; i < strlen(input); i++){
if(i == 0){
(*ptr) = malloc(sizeof(struct list));
(*ptr)->data = input[i]-'0';
(*ptr)->next = NULL;
(*ptr)->prev = NULL;
}
else {
tmp = malloc(sizeof(struct list));
tmp->data = input[i]-'0';
tmp->next = NULL;
tmp->prev = *ptr;
(*ptr)->next = tmp;
*ptr = tmp;
}
}
//printf("%d", temp->prev->data);
}
I know it works because as you can see, I included a printf just for testing and if I enter values like 1234 in the command prompt, I get the value 3 when printf works so that's fine.
So what I have for my add method is this:
Code:
void add(struct list *first, struct list *second) {
int carry = 0;
while(*first != NULL && *second != NULL) {
}
}
Yes, I'm aware the add method is virtually empty but my problem is simple. Why is the while loop giving me an error?
What I'm trying to do is traverse the list and I included a while loop to check if both lists are not null and then I'm going to get the data from both list's tail and add them together. But I just have to figure out the while loop first.