Witam,
Mam dziwny problem i nie umiem sobie z nim poradzić. Mógłby mi ktoś wytłumaczyć co robię źle. Kod ma tworzyć liste jednokierunkową do której doda parę imion następnie program ma wyświetlić listę i zaraz po tym ją usunąć. Jeśli zamiast scanf zrobię wywołanie ręczne fukcji push_top, np: push_top(&head, "Adam"); push_top(&head, "Maciek"); wszystko działa. Jednak gdy użyję zmiennej w pętli for program wyświetla mi "Maciek Maciek".
Kod:
#include <stdio.h>
#include <stdlib.h>
//struktura danych listy
typedef struct ListElement{
char* data;
struct ListElement *next;
} List;
//dodawanie elementu do listy
void push_top(List **head, char* data){
List *current;
current =(List *)malloc(sizeof(List));
current->data = data;
current->next=(*head);
*head=current;
}
//wyświetlanie listy
void show(List *head){
List *current=head;
while (current != NULL){
printf("%s\n", current->data);
current = current->next;
}
}
//usuwanie listy
void remove_list(List *head){
while(head != NULL){
List *current=head;
current->data = NULL;
head = current->next;
}
}
int main (){
List *head;
head = (List *)malloc(sizeof(List));
head=NULL;
int number;
printf("Podaj ile imion chcesz podac: ");
scanf("%d", &number);
//chyba tu jest błąd
char yourname[100];
for(int i=number; i>0; i--){
printf("Podaj imie: ");
scanf("%s", yourname);
push_top(&head, yourname);
}
//chyba tu jest błąd^
show(head);
remove_list(head);
return(0);
}