Cześć,
mam problem z listą jednokierunkową.
Błędów kompilacji nie ma, program się uruchamia, ale przy wprowadzaniu n-tego rekordu wywala błąd (czasem przy trzecim czasem siedemnastym).
Generalnie nie udało mi się jeszcze wprowadzić więcej niż 20 rekordów.
Proszę o wskazówki gdzie jest błąd? Dopiero zaczynam przygodę z C++ i jeszcze nie zupełnie ogarniam alokowanie pamięci i wskaźniki (szczególnie na struktury)
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
struct Przedmioty{string Nazwa; struct Przedmioty *wskNastepnyElementListy;};
struct Przedmioty *PierwszyElement;
struct Przedmioty *Element;
int main()
{
string tempNazwa;
struct Przedmioty *tempAdres;
do
{
Element = (struct Przedmioty*) malloc(sizeof(struct Przedmioty));
if (PierwszyElement == NULL) PierwszyElement = Element;
cout << "Wprowadz nazwe nowego elementu: ";
getline (cin,tempNazwa);
if(tempNazwa == "koniec") return 0;
(*Element).Nazwa = tempNazwa;
(*Element).wskNastepnyElementListy = 0;
if (Element != PierwszyElement)
{
tempAdres = PierwszyElement;
while((*tempAdres).wskNastepnyElementListy != Element)
if ((*tempAdres).wskNastepnyElementListy != 0)
{
tempAdres = (*tempAdres).wskNastepnyElementListy;
}
else
{
(*tempAdres).wskNastepnyElementListy = Element;
};
};
}
while(1);
return 0;
}