misstj555 45 Junior Poster in Training

Hi. I wanted to review the idea of "Link List" but using C++ and not Java. I borrowed a header file from a textbook to test how a link list works (I know that if I don't paste the whole code [its not my code anyway. It was from a textbook], it would be hard to help me but I don't want to copy the whole code from the text book. I will just show the functions that I am using. If my question still does not make sense, please mention it in the comment section below so that I can clarify). I am just using the function called "void copyTheList(const theNode *pointToSource, theNode *&pointToHead, theNode *&pointToTail);. Here is how it is implemented (more details about the other functions that are invoked in the code are written in the code block):

void copyTheList(const theNode *pointToSource, theNode *&pointToHead, theNode *&pointToTail)
{
    pointToHead = NULL;
    pointToTail = NULL;

    if (pointToSource == NULL)
        return;

       //"insertHead" is another function I made in the header file that is declared like this: 
       // void insertHead(theNode*& pointToHead, const theNode::value_type& entered). 
      // This is what it does: a new node that populates the value of "entered" is added 
      // at the head of the linked list. Now "pointToHead" points to the head of the new linked list.

        insertHead(pointToHead, pointToSource->data());
        pointToTail = pointToHead;

        //"link() is actually another function that is declared like this in the header file:
        //theNode *link() const { return fieldOfTheLink; }
        //It returns the current link of the node. "fieldOfTheLink" is declared like this:
        //theNode *fieldOfTheLink; 
        //However- using the textbook that I am learning from- it is declared a private variable

        pointToSource = pointToSource->link();
        while (pointToSource != NULL) {

        //"data() is another function that is declared like this in the header file:
        //value_type data() const { return fieldForData; }
        //It returns the current link of the node. "fieldOfTheLink" is declared like this:
        //theNode *fieldForData; 
        //However- using the textbook that I am learning from- it is declared a private variable

        //Also, the insert function is a fuction that is declared like this:
        //void insert(theNode *pointToThePrevious, const theNode::value_type &enter);
        //It inserts a new node after the previous node.

            insert(pointToTail, pointToSource->data());
            pointToTail = pointToTail->link();
            pointToSource = pointToSource->link();
        }
    }

I just wanted to create a test file to test this fuction only. Here is what I tried but I it has some syntax errors:

        cout<< "\n Here is our new Link List: "<<endl;
        node *a = new node(25); //This is the tail
        node *b = new node(20, a);
        node *c = new node(15, b);
        node *d = new node(10, c);
        node *e = new node(5, d);//This is the head

        //Print list
        for(node *showList = e; showList != NULL; showList = showList -> link())
        {
            cout<< showList -> data() << " ";
        }
    cout<<""<<endl;

    //Test "void list_copy(const node *source_ptr, node *&head_ptr, node *&tail_ptr)"
    cout<< "Now testing fuction called: \"void list_copy(const node *source_ptr, node *&head_ptr, node *&tail_ptr)\""<<endl;
            //I must have invoked this incorrectly
    const node *copyHere = new node();
    // list_copy(copyHere->head_ptr, e, a);
    //list_copy(copyHere, e, a);
    print(copyHere);
    cout<<""<<endl;

Can someone give me an idea of how to test this fuction so I can see what it does (or at least help me fix the syntax errors in the test code I made)? Thank You. I just wanted some help from people who may already know a lot about "Linked Lists" and could help me learn it though this textbook example.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.