I have a base class "Artikel" and a couple of derived classes "AudioCD" and "Boek"
Then I have a class "Winkel" that has a private member ArtikelLijst* stok. Stok is the head of a linked list that contains articles.
In main.cpp, I make a Winkel instance and add some Artikel instances to it (2). After that, I print the result, but for some reason, only the first Artikel gets printed. I've reviewed the code a couple of times, but cannot seem to find the error.
I will post the code that adds an Artikel to the list and the code that prints the Artikels, I will also post Winkel.h (do not mind the dutch comments):
Winkel.h
Code:
#ifndef WINKEL_H
#define WINKEL_H
#include "Artikel.h"
const int AANTAL = 10;
/*
* Een winkel is een array / linked list van artikels
* Sommige functies zijn van toepassing op de versie met de array
* Achter deze functies staat [ARRAY]
* Idem met gelinkte lijst maar dan [LINKED LIST]
*/
// de gelinkte lijst die een winkellijst voorstelt, alternatief voor array
struct ArtikelLijst
{
ArtikelLijst* prev;
ArtikelLijst* next;
Artikel* artikel;
};
class Winkel
{
public:
// zie winkel.cpp om de linked list/array code te veranderen voor de default constructor
Winkel();
// LINKED LIST FUNCTIONS
// Met een linked list kan je niet werken met een get functie zoals bij een array, maar dit is sowieso overbodig
void addArtikel(Artikel* artikel);
// Een artikel ADRES is uniek :-
void deleteArtikel(Artikel* artikel);
// schrijf alle artikels in de lijst uit
void toonArtikelsLL();
// ARRAY FUNCTIONS
// vul een artikel in de array op plaats index
void vulLijst(Artikel* a, int index) { lijst[index] = a; }
// geef een artikel terug uit de array
Artikel* getArtikel(int index) const { return lijst[index]; }
// schrijf alle van NULL verschillende artikels in de array uit
void toonArtikels();
private:
// gelinked lijst manier - lijsthoofd
ArtikelLijst* stok;
// array manier
Artikel* lijst[AANTAL];
};
#endif
Print the articles
Code:
void Winkel::toonArtikelsLL()
{
ArtikelLijst* cursor = stok;
while (cursor != NULL)
{
cursor->artikel->schrijfUit();
cursor = cursor->next;
}
}
Add an article
Code:
void Winkel::addArtikel(Artikel* artikel)
{
// stok is alleen NULL in het begin als de lijst leeg is, dus fix lijsthoofd
if (stok == NULL)
{
stok = new ArtikelLijst;
stok->artikel = artikel;
stok->next = NULL;
stok->prev = NULL;
}
else
{
// we weten dat het lijsthoofd niet null is, dus prev van het lijst hoofd bestaat [triv noob] nee ni zo triv zie if
ArtikelLijst *cursor = stok;
ArtikelLijst *prev = cursor->prev; // triv null
// ga naar de laatste plaats
while (cursor != NULL)
{
prev = cursor;
cursor = cursor->next;
}
// add new article
cursor = new ArtikelLijst;
cursor->artikel = artikel;
cursor->next = NULL;
cursor->prev = prev;
}
}