Question

Write a member method getPosition(int entry) which returns the position of the entry is in the...

Write a member method getPosition(int entry) which returns the position of the entry is in the linked list. If the entry is not in the list , return -1.

Note: Use C++ language

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

// Considering the linked list contains unique elements and positioning starts from 1

Node* head; // contains the head of the linked list declared global here

int getPosition(int entry)

{

Node* temp = head;

int count = 1;

while(temp!=NULL && temp->data != entry)

{

count++;

temp = temp->next;

}

if(temp == NULL) // entry is not present in the linked list

{

return -1;

}

return count;

}

Hope this code helps you. Please give an upvote if it does.

Add a comment
Know the answer?
Add Answer to:
Write a member method getPosition(int entry) which returns the position of the entry is in 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
ADVERTISEMENT