Czołem Programiści. Mam następujący problem z listą jednokierunkową. Chcę utworzyć własną funkcję która tworzy listę (utwListe()). Funkcja ta korzysta jeszcze z innej również własnoręcznie napisanej funkcji która tworzy element wraz z alokacją pamięci. Problem polega na tym że gdy chcę wywołać funkcję dla wskaźnika który na nic nie wskazuje, po wywołaniu funkcji wyswietlListe() nie otrzymuję żadnych wartości. Natomiast jeśli pusty wskaźnik potraktuję funkcją utwElement() przed wykorzystaniem funkcji utwListe() wszystko działa dobrze. Pomożecie?
#include <stdlib.h>
#include <stdio.h>
typedef struct str {
int n;
struct str * next;
}str;
struct str * utwElement(int dana){
struct str * tmp;
tmp = (struct str *) malloc(sizeof(struct str));
tmp->n = dana;
tmp->next = NULL;
return tmp;
}
void utwListe(struct str * first, int liczbaElementow){
struct str * tmp;
tmp = first;
if (tmp == NULL){
tmp = utwElement(0);
liczbaElementow--;
}
else {
while (tmp->next != NULL){
tmp = tmp->next;
}
}
for (int i = 1; i <= liczbaElementow; i++){
tmp->next = utwElement(i);
tmp = tmp->next;
}
}
void wyswietlListe(struct str * first){
struct str * tmp = first;
while (tmp != NULL){
printf("%d\n", tmp->n);
tmp = tmp->next;
}
}
int main(){
struct str * wsk = NULL;
//wsk = utwElement(5);
utwListe(wsk, 5);
wyswietlListe(wsk);
}