print_entire_list

solo_'s Avatar, Join Date: Aug 2011
Newbie Member
Why is method "print_entire_list" not working?
Code:
#ifndef LINKED
#define LINKED

#include <iostream>

using namespace std;

template<class T>
class Linked {

    protected:
        struct Node{
            T item;
            Node* next;
        }; // struct Node

        Node* head;
        long length;

    public:

        // Postcondition: The Linked container has been initialized
        //                to an empty container.
        Linked(){
            head = NULL;
            length = 0;
        } // default constructor


        // Postcondition: The number of items in the Linked container
        //                has been returned.
        long size() const{
            return length;
        } // size


        // Postcondition: A node with newItem has been inserted at the
        //                front of the Linked container.
        void push_front (const T& newItem){
            Node* newHead = new Node;
            newHead -> item = newItem;
            newHead -> next = head;
            head = newHead;
            length++;
        } // push_front
        
        // Postcondition: A node with newItem has been inserted at the
        //                back of the Linked container.
        void push_back (const T& newItem){
            Node* newHead = new Node;
            newHead -> item = newItem;
            newHead -> next = head;
            head = newHead;
            length++;

        } // push_back

        // Postcondition: We want to print the list
        void print_entire_list (){
            
            int lineBreak = 0;
            int x;
            for(x=0;x<length;x++){

                // We need to break the line at 5, 10, 15, 20 etc
                if(lineBreak > 4){
                    cout << "\n";
                    lineBreak = 0;
                }
                cout << " Some data: " << item();

                lineBreak = lineBreak+1;
            }

        } // print_entire_list


}; // class Linked


#endif
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
What do you expect it to print and have you entered any data in the list before printing?
0
solo_'s Avatar, Join Date: Aug 2011
Newbie Member
Yes, I have entered data:

link.cpp
Code:
#include <iostream>
#include <string>
#include "linked.h"

using namespace std;

int main (){

    // Create list
    Linked <int> myList; 

    // Testing
    myList.push_front (5);
    myList.push_front (10);
    myList.push_front (15);
    myList.push_front (25);
    myList.push_front (20);
    myList.push_front (30);
    myList.push_front (35);
    myList.push_front (40);
    myList.push_front (45);
    myList.push_front (5);
    myList.push_front (50);
    myList.push_front (55);
}
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
You are outputting as
Code:
cout << " Some data: " << item();
where as item is not a function.
0
solo_'s Avatar, Join Date: Aug 2011
Newbie Member
I got it:

Code:
        /*- Print Entire List ---------------------------------------------- */
        // Postcondition: We want to print the list
        void print_entire_list (){

            // Initialize pointer
            Node * p;
            p = head;
            
            // Counter for linebrak in loop
            int lineBreak = 0;

            while(p != NULL){

                // LineBreak?
                if(lineBreak == 5){
                    cout << "\n";
                    lineBreak = 0;
                }


                // Body
                cout << " ["  << p->item << "]";

                // Add extra spaces (formatting)
                if(p->item >= 100 && p->item < 1000){
                    cout << " "; 
                }
                else if(p->item >= 10 && p->item < 100){
                    cout << "  "; 
                }
                else if(p->item >= 0 && p->item < 10){
                    cout << "   "; 
                }

                // Arrow
                cout << " -> ";
                
                // Move pointer to next object
                p =  p->next;

                // LineBreak increment
                lineBreak = lineBreak+1;
            }  
            cout << " [NULL]\n\n";
        } // print_entire_list