Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   print_entire_list (http://www.go4expert.com/forums/printentirelist-t26581/)

solo_ 27Aug2011 22:33

print_entire_list
 
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


shabbir 28Aug2011 09:37

Re: print_entire_list
 
What do you expect it to print and have you entered any data in the list before printing?

solo_ 28Aug2011 19:25

Re: print_entire_list
 
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);
}


shabbir 28Aug2011 20:57

Re: print_entire_list
 
You are outputting as
Code:

cout << " Some data: " << item();
where as item is not a function.

solo_ 28Aug2011 21:06

Re: print_entire_list
 
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



All times are GMT +5.5. The time now is 09:54.