Я пытаюсь написать код, который добавляет номера элемента связанного списка. Но после добавления я теряю некоторые элементы. Я не могу найти ошибки. Вот мой код:C Связанный список, теряющие элементы
void push(struct node** head_ref, int new_data){
struct node* new_node = (struct node*)malloc(sizeof(struct node*));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void reverse(struct node** head_ref){
struct node* prev = NULL;
struct node* current = *head_ref;
struct node* next;
while (current != NULL){
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head_ref = prev;
}
struct node *shiftter(struct node *a, int index){
struct node *temp = a;
while(index > 0){
append(&temp, 0);
--index;
}
return temp;
}
struct node *add(struct node *fist, struct node *second){
struct node *gudu = second;
struct node *c = fist;
struct node *hudu = NULL;
reverse(&gudu);
reverse(&c);
while(c != NULL){
push(&hudu, c->data + gudu->data);
c = c->next;
gudu = gudu->next;
}
while(gudu != NULL){
push(&hudu, gudu->data);
gudu = gudu->next;
}
return hudu;
}
int main(int argc, const char * argv[]) {
struct node *a = NULL;
struct node *b = NULL;
push(&a , 1);
push(&a , 2);
push(&a , 3);
push(&a , 4);
push(&b , 5);
push(&b , 1);
push(&b , 2);
push(&b , 4);
printList(a);
printf("\n");
printList(b);
printf("\n");
b = shiftter(b,1);
printList(b);
printf("\n");
printList(add(a, b));
printf("\n");
printList(a);
printf("\n");
printList(b);
return 0;
}
Мой вывод:
4 3 2 1
4 2 1 5
4 2 1 5 0
4 6 4 7 1
4
4
Моя программа закончилась с кодом выхода: 0
Что должна делать функция 'shifter()'? –