Witam, mam problem przy implementowaniu listy. (polecenie w pierwszym komentarzu kodu) Mianowicie lista nie dosc ze generuje tylko 2 elementy to do tego nie są to elementy losowe. Może ktoś mi wskazać gdzie popełniłem błąd?
#include <iostream>
#include <cstdlib>
using namespace std;
// Polecenie: napisz program generujacy i wyswietlajacy liste jednostronnie wiazana o dlugosci 20 w sposob losowy z
// przedzialu 1-15
struct node
{
int val;
node *next;
};
void dodajelement(node *&lista , int x) //
{
node *pom;
node *pom2 ;
if (lista==NULL)
{
pom=new node;
pom -> val = x;
pom -> next = NULL; // ogarnac!!!
lista=pom;
cout<<pom->val;
pom=NULL;
}
else
{
pom=lista;
if (pom->next!=NULL)
{
pom=pom->next;
}
else
{
pom2 = new node;
pom2->val =x;
pom2->next =NULL;
pom ->next=pom2;
cout<<pom2->val;
}
}
}
void tworzliste(node *&lista)
{
for (int i=0;i<20;i++)
{
int z;
z = rand() % 15;
dodajelement(lista , z);
}
}
int main(int argc, char** argv)
{
node *lista=NULL;
tworzliste(lista);
system("pause");
return 0;
}