Mam problem z dodaniem elementu na początek listy. Mój kod:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
struct Ksiazka
{
char *autor;
char *tytul;
int rok;
struct Ksiazka *next;
struct Ksiazka *prev;
};
struct Ksiazka *create_element(char *autor_parametr, char *tytul_parametr, int rok)
{
struct Ksiazka *nowy_element=(Ksiazka*)malloc(sizeof(Ksiazka));
nowy_element->autor=(char*)malloc(strlen(autor_parametr)+1);
for(int i=0; i<100; i++)
{
nowy_element->autor[i] = autor_parametr[i];
if(autor_parametr[i] == '\0')
{
break;
}
}
nowy_element->tytul=(char*)malloc(strlen(tytul_parametr)+1);
for(int i=0; i<100; i++)
{
nowy_element->tytul[i] = tytul_parametr[i];
if(tytul_parametr[i]=='\0')
{
break;
}
}
nowy_element->rok = rok;
nowy_element->next = NULL;
nowy_element->prev = NULL;
return nowy_element;
}
void destroy_element(struct Ksiazka *element)
{
free(element->autor);
free(element->tytul);
free(element);
}
void wyswietl_element(struct Ksiazka *element)
{
printf("\nAutor: %s",element->autor);
printf("\nTytul: %s", element->tytul);
printf("\nRok: %i", element->rok);
}
void add_first(struct Ksiazka **head,struct Ksiazka **tail, struct Ksiazka nowa1)
{
nowa1->next=*head;
nowa1->prev=NULL;
(*head)->prev=nowa1;
(*head)=nowa1;
}
int main()
{
char autor[100];
char tytul[100];
int rok;
printf("Podaj autora: ");
scanf("%s",autor);
printf("Podaj tytul: ");
scanf("%s",tytul);
printf("Podaj rok: ");
scanf("%i",&rok);
Ksiazka *nowa=create_element(autor, tytul, rok);
printf("/nPodaj autora: ");
scanf("%s",autor);
printf("Podaj tytul: ");
scanf("%s",tytul);
printf("Podaj rok: ");
scanf("%i",&rok);
Ksiazka *nowa1=create_element(autor, tytul, rok);
add_first (nowa,nowa1);
if (nowa->prev!=NULL)
{
wyswietl_element(nowa1);
wyswietl_element(nowa);
}
else
{
wyswietl_element(nowa);
wyswietl_element(nowa1);
}
destroy_element(nowa);
scanf("%i",&rok);
return 0;
}
Mógłby ktoś podpowiedzieć?