Witam , mam problem z zaimplementowaniem listy za pomoca wskaźników. W momencie gdy próbuje skompilować program (który niby jest bezbłędny) konsola mi sie wyświetla i od razu przestaje odpowiadać, zawiesza sie. W czym jest problem?
#include <iostream>
using namespace std;
struct element {
element *next;
int key;
};
struct lista {
element *head;
void nowy( int akey)
{
element *nowy = new element;
if ( head == 0)
{
nowy->key = akey;
head = nowy;
}
else
{
// w przeciwnym wypadku wędrujemy na koniec listy
element *temp = head;
while (temp->next)
{
// znajdujemy wskaźnik na ostatni element
temp = temp->next;
}
temp->next = nowy; // ostatni element wskazuje na nasz nowy
nowy->next = 0; // ostatni nie wskazuje na nic
}
}
void przegladaj()
{
element *temp = head;
while (temp->next != NULL)
{
cout << "Klucz to: " << temp->key <<endl;
temp = temp->next;
}
}
};
int main()
{
lista list;
list.nowy(10);
list.nowy(3);
list.przegladaj();
system("pause");
return 0;
}