Question

Q10. Write a method for String toString() method for Stack implemented as a linked list. The toString() method is used to get a String object representing the contents of a class

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public String toString()

{

    // head points to the top element of the stack

    Node trav = head;

   

    String ans = "";

   

    // traverse through the list

    while( trav != null )

    {

        // getVal() returns the value of the node

        ans = ans + trav.getVal() + " ";

       

        // go to next node in the list

        // getNext() returns the value of the next field

        trav = trav.getNext();

    }

   

    return ans;

}

Code Screenshot

public STring toString ) // head points to the top element of the stack Node trav-head; String ans = ; // traverse through

Add a comment
Know the answer?
Add Answer to:
Q10. Write a method for "String toString()" method for Stack implemented as a linked list. The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT