Thread: Linked List question

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    44

    Linked List question

    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;
    	}
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You need to update the previous node's next member.
    Also suggest you get familiar with smart pointers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. linked list question
    By brb9412 in forum C Programming
    Replies: 16
    Last Post: 01-04-2009, 04:05 PM
  3. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  4. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  5. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM