Witam!
jestem początkujący jeśli chodzi o c i mam pewien problem.
muszę zrobić program, który sumuje elementy dwóch list i następnie przedstawia je w postaci trzeciej listy:
ze stworzeniem dwóch list nie mam problemu, ale nie mam pojęcia jak zsumować elementy obu z nich tzn. element 1 z listy 1 z elementem 1 z listy 2 itd. aż do końca listy i potem jak je wrzucić do trzeciej listy :/ to mój kod:
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
int a;
struct lists {
int value;
struct lists *next;
};
struct list_i {
struct lists *head;
};
typedef struct list_i *List;
List list_create() {
List list = malloc(sizeof(struct list_i));
list->head = NULL;
return list;
}
void add_random_el (List list, int howmany) {
int i;
struct lists *it = list->head;
struct lists *tmp;
for (i=0; i<howmany; i++)
it = it->next;
struct lists *new_element = malloc(sizeof(struct lists));
new_element->value = rand()%1000;
new_element->next = it->next;
it->next = new_element;
}
void print_list (List list) {
struct lists *i = list->head;
while(i != NULL){
printf("%d\n", i->value);
i = i->next;
}
}
void list_clear(List list) {
struct lists *i = list->head;
struct lists *tmp;
while(i != NULL) {
tmp = i;
i = i->next;
free(tmp);
}
}
void list_destroy(List list) {
list_clear(list);
free(list);
}
int main()
{
int b;
List one = list_create();
List two = list_create();
printf("give number of integers in lists:\n");
scanf("%d", &b);
add_random_el(one, b);
add_random_el(two, b);
print_list(one);
print_list(two);
list_destroy(one);
list_destroy(two);
}
byłbym wdzięczny za jakiekolwiek rozwiązanie mojego problemu.