Siemka, mam problem z programem. Kompiluje się, aczkolwiek coś nie gra, wydaje mi się, że przy dodawaniu elementów. Będę wdzięczny, jeśli ktoś wskaże mi pomyłkę.
//header
#include <iostream>
#include <cstdlib>
template <typename Type>
class Lista
{
private:
struct Element
{
Element *next;
Type dane;
};
Element *head;
public:
Lista();
~Lista();
void show();
void add(Type d);
void usun(Element * elem);
bool isempty();
int rozmiar();
void czysc();
};
//metody
template <typename Type>
Lista<Type>::Lista()
{
head = NULL;
std::cout << "dziala konstruktor" << std::endl;
}
template <typename Type>
Lista<Type>::~Lista()
{
czysc();
}
template <typename Type>
void Lista<Type>::czysc()
{
if(head == NULL)
{
std::cerr << "Nie ma czego usunac" << std::endl;
return;
}
else
{
Element *temp = new Element;
for(temp = head; temp != NULL; temp = temp->next)
{
std::cout << "destruktor" << std::endl;
delete temp;
}
}
}
template <typename Type>
void Lista<Type>::show()
{
Element *temp = new Element;
for(temp = head; temp->next!=NULL; temp = temp->next)
{
std::cout << temp->dane << std::endl;
}
}
template <typename Type>
void Lista<Type>::add(Type d)
{
Element * nowa = new Element;
nowa->dane = d;
if(head == NULL)
{
std::cout << "dodawanie" << std::endl;
head = nowa;
}
else
{
Element *temp = new Element;
temp = head;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next = nowa;
nowa->next = NULL;
}
}
//main
int main()
{
Lista<int> L;
L.add(2);
L.show();
}