1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

print_entire_list

Discussion in 'C++' started by solo_, Aug 27, 2011.

  1. solo_

    solo_ New Member

    Joined:
    Aug 27, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Norway
    Home Page:
    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
    
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    What do you expect it to print and have you entered any data in the list before printing?
     
  3. solo_

    solo_ New Member

    Joined:
    Aug 27, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Norway
    Home Page:
    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);
    }
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    You are outputting as
    Code:
    cout << " Some data: " << item();
    where as item is not a function.
     
  5. solo_

    solo_ New Member

    Joined:
    Aug 27, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Norway
    Home Page:
    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
     

Share This Page