user7009435
user7009435

Reputation:

Having trouble printing a singly linked list stack recursively

I'm currently having trouble figuring out how to do this. My idea right now is to have the stack pop() this every time print is called recursively within itself, but that is not working for me.

template <class T>
void Stack<T>::print() const {  
    if (top->next == nullptr) {
        cout << "End of list" << endl;
    }
    else {
        cout << top->data;
        this->pop();
        print();
    }
}

When I declare a Stack of type int in my main via Stack<int> test, I get the error "passing const Stack<int> as 'this' argument discards qualifiers.

Upvotes: 0

Views: 47

Answers (2)

JaMiT
JaMiT

Reputation: 16843

passing const Stack<int> as 'this' argument discards qualifiers

This is your problem. You qualified your print function with const. That is a promise that you will not change the stack as you print it (which is a reasonable expectation). Calling pop would change the stack, breaking that promise.

The "qualifier" in the compiler's error message is const, so "discards qualifiers" basically means "discards your promise to not change *this".

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 310950

For starters the function is not a recursive function (as you showed it before updating your question).

Moreover it has undefined behavior when is called for an empty stack (that is when the pointer top is equal to nullptr).

As for the error message then the function pop changes the stack (it is a non-constant member function) while it is called from the constant function print. So the compiler issue an error.

Thus it it is supposed that the function print will change the stack during outputting its values then it shall be declared as a non-constant function.

The function can look something like it is shown below. I assume that the function push or its analog is declared something like

void push( const T & );

Here you are.

template <class T>
std::ostream & Stack<T>::print( std::ostream &os = std::cout ) 
{
    if ( this->top )
    {
        os << top->data << ' ';

        auto value = top->data;

        this->pop();

        this->print( os );

        this->push( value );      
    }

    return os;
}

And the function can be called like

Stack<int> test;

//...

test.print() << '\n';

Here is a demonstrative program that shows the function above with minor changed applied to the standard class template std::stack (because you did not show your stack definition).

#include <iostream>
#include <stack>

template <class T>
std::ostream & print( std::stack<T> &st, std::ostream &os = std::cout ) 
{
    if ( not st.empty() )
    {
        os << st.top() << ' ';

        auto value = st.top();

        st.pop();

        print( st, os );

        st.push( value );      
    }

    return os;
}

int main() 
{
    std::stack<int> st( std::stack<int>::container_type { 5, 4, 3, 2, 1 } );

    print( st ) << '\n';

    return 0;
}

The program output is

1 2 3 4 5 

Upvotes: 0

Related Questions