Witam,
Oto moja implementacja listy dwukierunkowej z szablonem, wg mnie jest poprawna lecz program się wywala. Czy jest jakiś błąd ?? :)
#pragma once
#include <iostream>
using namespace std;
template <class TYP>
struct Wezel
{
Wezel *prev,*next;
TYP k;
};
template <class TYP>
class Lista {
public:
Lista() {
pierwszy = NULL;
liczba_elementow = 0;
}
void dodajNaKoniec(const TYP& k);
void dodaj(const TYP& k, unsigned int l);
void usunOstatni();
Wezel<TYP>* zwrocWskaznikNaWezel(unsigned int indeks);
int Rozmiar(){ return liczba_elementow;}
~Lista(){
while (liczba_elementow > 0)
usunOstatni();
}
private:
Wezel<TYP> *pierwszy;
int liczba_elementow;
};
template <class TYP>
void Lista<TYP>::dodajNaKoniec(const TYP& k)
{
Wezel<TYP> *nowy_wezel = new Wezel<TYP>();
nowy_wezel -> k = k;
nowy_wezel -> prev = NULL;
nowy_wezel -> next = NULL;
if(liczba_elementow == 0)
pierwszy = nowy_wezel;
else
{
Wezel<TYP> *tmp;
tmp = zwrocWskaznikNaWezel(liczba_elementow-1);
nowy_wezel -> prev = tmp;
tmp -> next = nowy_wezel;
}
liczba_elementow++;
}
template <class TYP>
Wezel<TYP>* Lista<TYP>::zwrocWskaznikNaWezel(unsigned int indeks)
{
Wezel<TYP>* wezel = pierwszy;
for(unsigned int i = 0; i < indeks; i++)
{
if(wezel = NULL)
return NULL;
wezel = wezel -> next;
}
return wezel;
}
template <class TYP>
void Lista<TYP>::dodaj(const TYP& k, unsigned int l)
{
Wezel<TYP> *nowy_wezel = new Wezel<TYP>();
nowy_wezel -> k = k;
Wezel<TYP> *tmp;
tmp = zwrocWskaznikNaWezel(l);
nowy_wezel -> next = tmp -> next;
nowy_wezel -> prev = tmp;
tmp -> next = nowy_wezel;
nowy_wezel -> next -> prev = nowy_wezel;
liczba_elementow++;
}
template <class TYP>
void Lista<TYP>::usunOstatni()
{
if(liczba_elementow < 2)
{
delete pierwszy;
pierwszy = NULL;
}
else
{
Wezel<TYP> *tmp = zwrocWskaznikNaWezel(liczba_elementow - 2);
delete tmp -> next;
tmp -> next = NULL;
}
liczba_elementow--;
}