C++ outputting and inputting a single character


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;

This function is implemented as follows:

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

        //If the list is not empty...
            //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
            //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
        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?

Best Solution

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. :)