Thread: Printing a linked list

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    4

    Printing a linked list

    i have a linked list in my program that is circular . i have to print from a specific number in the list and when i do im getting an error. now this function is a constant and must stay a constant, the line with the stars is giving me this error
    (C:\Windows\Desktop\Project5\Functions.cpp(117) : error C2166: l-value specifies const object)

    Code:
    void ClosedList::PrintFrom(int item) const
    
    {         
    
        NodeType* currPtr = head->backlink;
    
        NodeType* newNodePtr = new NodeType;
    
        newNodePtr->component = item;
    
        newNodePtr->link = head;
    
        head = newNodePtr;*******************************
    
        newNodePtr = NULL;
    
        if(!IsEmpty())
    
        {
    
    	do
    
    	{
    
    	     cout << currPtr->component << "    ";
    
    	     currPtr = currPtr->backlink;
    
    	}while (currPtr != head);
    
    	   cout << currPtr->component << "    ";
    
    	}
    
    	cout <<endl;
    
    bool ClosedList::IsEmpty() const
    {
    	return (head == NULL);
    }

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    i presume tha the "head" pointer is a GLOBAL pointer that is located in the global namespace, and by including "const" after the function's name you signify that the function will not change anything beyond the scope of this function

    and on the line

    head = newNodePtr;*******************************
    you are changing the head pointer....

    if you make the function not "const" i think the error should go away....




    matheo917

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    4

    Global head

    Head is a private member of a class
    Code:
    struct  NodeType;
    struct NodeType
    {
    int component;
    NodeType* link;
    NodeType* backlink;
    };
    
    class ClosedList
    {
    public:
    	bool IsEmpty() const;
    	void Append(int item);
    	int TotalCount();
    	void Print() const;
    	void PrintReverse() const;
    	void PrintFrom(int item)const;
    	void Delete(int item);
    	ClosedList();
    	~ClosedList();
    	ClosedList(const ClosedList& otherList);
    private:
    	NodeType* head;

  4. #4
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279

    still....

    still.............that function is designed to not alter anything besides what was created and what dies within that function...

    i don't have time to follow the logic step by step in your entire program but once again i don't realy believe that this function is supposed to give "head" a new address to point to....
    considering the fact that all it supposed to do is print the list, which in result all it does is retrieves your data...(without altering it)


    go over your logic again, perhaps you want to create a "cur" poiner and assign value of "head" to it and then manipulate your program by using "cur" not head


    good luck

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    4

    error fixed but...

    i fixed the error but now i can print in the right order. the number i have to print from is the third number in the list to the end then it has to rap back around and print the first two numbers, any suggestions

    The list in the order of appending
    92 82 62 72 99

    Code:
    void ClosedList::PrintFrom(int item) const
    {
    
    	NodeType* currPtr = head->backlink;
    	NodeType* newNodePtr = new NodeType;
    
    	newNodePtr->component = item;
    	newNodePtr->backlink = currPtr;
    	currPtr = newNodePtr;
    	newNodePtr = NULL;
    	
    	if(!IsEmpty())
    	{
    		do
    		{
    			cout << currPtr->component << "    ";
    			currPtr = currPtr->backlink;
    			
    		}while (currPtr != head);
    		
    		cout << currPtr->component << "    ";
    	}
    	cout <<endl;
    }

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    designate a second pointer to hold the position where you start in the list and keep searching for that pointer every time before you print. If current pointer start pointer then stop printing. I suspect you will need to write a special case for when the list is just one node long, but what the hey.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  3. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  4. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM