help with linked list in c++

  • How can i do to run a loop in a linked list. I know the stl has already one but i can't use it, because at school they want us to first be familiar with linked list.


    #definition of node of the list
    class listNode
    {
    public:
    listNode* getNext() ;
    //return the pointer to the next item
    void changeNext(listNode*) ;
    //change the pointer pointing to the next node
    string getNode_content();
    //return the value of the attribute nodeContent;

    private :
    string nodeContent;
    //informations hold by the node
    listNode* nextNode;
    //pointer to the next node
    };

    #definition of the list
    class myList
    {
    public:
    myList();
    //constructor
    void addNode(string n);
    //add a new node to the list;
    private :
    int nbNodes;
    //hold the number of node in the list
    listNode* headList;
    //pointer to the first node of the list
    listNode* currentPos ;
    //pointer to the current node i.e the last inserted node in the list
    }


    every thing works, except that i can find a way to run a loop trough the whole list
    here is my attempt


    #loop on the lsit and show each node content
    for(int i = 0; i < nbNodes; i++ )
    {
    listNode* tmp = headList-> getNext() ;
    cout <getNode_content() < }


    What is not working ?


  • #loop on the lsit and show each node content
    for(int i = 0; i < nbNodes; i++ )
    {
    listNode* tmp = headList-> getNext() ;
    cout <getNode_content() < }


    What is not working ?


    Usually, we iterate the Linked List checking the next pointer value whether its NULL or not.

    So, my code would be,


    listNode* tmp=headList;
    while(tmp->getNext() != null)
    {
    cout<getNode_content()< tmp=tmp->getNext();
    }


    Hope that helps :)







  • #If you have any other info about this subject , Please add it free.#
    Your name:
    E-mail:
    Telphone:

    Your comments:


    If you have any other info about help with linked list in c++ , Please add it free.