views:

271

answers:

4

I am writing a program in c++ which implements a doubly-linked list that holds a single character in each node. I am inserting characters via the append function:

doubly_linked_list adam;
adam.append('a');

This function is implemented as follows:

//Append node
    node* append(const item c){

        //If the list is not empty...
        if(length){
            //maintain pointers to end nodes
            node* old_last_node = last;
            node* new_last_node = new node;

            //re-assign the double link and exit link
            old_last_node->next = new_last_node;
            new_last_node->back = old_last_node;
            new_last_node->next = NULL;

            //re-assign the last pointer
            last = new_last_node;
        }
        //If this is the first node
        else{
            //assign first and last to the new node
            last = first = new node;

            //assign nulls to the pointers on new node
            first->next = first->back = NULL;
        }

        //increase length and exit
        ++length;
        return last;
    }

However, I think there is an issue, perhaps with the way C++ handles characters. When I go to print my list, somehow I never get the characters to print which I have appended to my list. This is what I'm using to print:

//Friendly output function
    friend std::ostream& operator << (std::ostream& out_s, const doubly_linked_list& source_list){
        //create iteration node pointer
        node* traverse_position = source_list.first;

        //iterate through, reading from start
        for(int i = 1; i <= source_list.length; ++i){
            //print the character
            out_s << (traverse_position->data);
            traverse_position = traverse_position->next;
        }

        //return the output stream
        return out_s;
    }

I just get crap when I print it. It prints characters that I never appended to my list - you know, just characters just from somewhere in the memory. What could possibly be causing this?

+7  A: 

Where are you assigning the value c in the append() function? I fear you may have concentrated too much on the doubly-linked-list part and not enough on the storing-data part. :)

Greg Hewgill
:- ) +1
paxdiablo
+1  A: 

No where in your append method do you actually place the item into the new node. When you go to print, it just prints whatever value happens to be in that memory location (some random value).

MahlerFive
Greg got there first but +1 for getting it right anyway.
paxdiablo
A: 

Thanks! I love you guys!

Whoa, ease back there, big fella :-)
paxdiablo
+3  A: 

As others have already mentioned, you forgot to store the characters you were supposedly appending. It's a reasonable mistake to make. To avoid it in the future, you can let the compiler help you.

Most compilers offer warnings about things that are technically OK, but probably aren't what you really want to do. In your case, you declared the parameter c, but you never used it. With warnings enabled, your compiler could have noticed that and told you that you hadn't used it. That would probably have been enough of a reminder for you that you weren't finished writing that function.

GCC's option to enable common warnings is -Wall. (That's "W" for "warning," plus "all"; it has nothing to do with walls. But it's not really all warnings, either.) For example:

g++ -Wall list-program.cpp

Other compilers have similar options. Check your compiler's documentation for details.

Rob Kennedy