Question

C++ Consider the following structure of node and linked list. struct Node { int key; Node...

C++

Consider the following structure of node and linked list.

struct Node {
  int key;
  Node *next;
};

10 -> 20 -> 30 -> 10 -> 10 -> 50 -> 10 -> NULL

What will be the output of following pseudo-code? Consider head is the pointer to the first node of above linked list.

Node *walker = head;
int count = 0;
while(walker!= NULL && count < 3) {
        if(walker->key == 10) {
                 count = count + 1;
         }
         walker = walker->next;
}
cout <<  walker->key  << endl;
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer is 50 ..

because each time walker key is equal to 10 count is incremented in while loop

and we have a condition in while loop that says count needs to be less than 3 .

So when we got to 3rd 10 in linked list count becomes 3 so that is the final iteration .

next statement after if block is while moves the walker to next node

so at the end of while loop walker points to node after the 3rd 10 i..e 50 .

so walker.key prints 50

Add a comment
Know the answer?
Add Answer to:
C++ Consider the following structure of node and linked list. struct Node { int key; Node...
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
ADVERTISEMENT