Program kompiluje się bez najmniejszych problemów, tylko w trakcie działania pojawia się błąd.
#include <stdio.h>
#include <stdlib.h> // malloc()
struct lista
{
int wartosc;
struct lista *next_el;
};
struct lista *head;
// funkcja wyszukujaca konkretna wartosc. Wartosc zwracana
// xszukana wartosc w liscie
// 1 gdy wartosc zosta³a znaleziona
// 0 gdy wartosc nie zosta³a znaleziona
int find( int x)
{
struct lista *current_ptr;
current_ptr = head;
while( current_ptr != NULL)
{
if(current_ptr->wartosc == x) return 1;
current_ptr = current_ptr->next_el;
}
return 0;
}
int main()
{
struct lista head;
struct lista *new_ptr;
struct lista *new_ptr2;
struct lista *new_ptr3;
head.next_el = NULL;
new_ptr = malloc(sizeof(struct lista));
new_ptr->wartosc= 12;
head.next_el = new_ptr; // poczatek listy wskazuje na pierwszy element
new_ptr = NULL;
//dodanie 2elem
new_ptr2 = malloc(sizeof(struct lista));
new_ptr2->wartosc = 3;
new_ptr->next_el = new_ptr2;
new_ptr2 = NULL;
//dodanie 3 elementu
new_ptr3 = malloc(sizeof(struct lista));
new_ptr3->wartosc = 9;
new_ptr2->next_el = new_ptr3;
new_ptr3 = NULL;
if(find(29))
{
printf("znalazllem szukanej liczby");
}
else
{
printf("Nie znalazlem szukanej liczby");
}
free(new_ptr);
free(new_ptr2);
free(new_ptr3);
return 0;
}