MipsMoreLikeWhips
MipsMoreLikeWhips

Reputation: 147

Keeping track of how many times a node has been visited

I have been researching this topic for a while. I have yet to come to a solid conclusion.

How would you keep track of how many times a node has been visited in a Doubly Linked List?

For example:

Let's say we enter a few nodes and each node holds a value of type char.

The user types in the values to the node that they want to visit.

User enters : 'b', 'b', 'c' 'b', 'a', ' a'.

Now 'b' has been visited three times.

Now since b is the most visited node, you want to move that node to the front.

Moving a node to the front is easy, but I have no idea how one would keep track of a node.

Any help would be much appreciated.

Upvotes: 1

Views: 509

Answers (1)

ravi
ravi

Reputation: 10733

You can add a count field in your node as follows:-

struct node
{
char alpha;
int count;
struct node *next;
}

Also, define a constructor where you would set count to 0. You have to continuously check for count to sort linked list at every input.

One thing for sure is that this will be very poor choice of data structure for this.

EDITED IN RESPONSE TO THE COMMENT:-

Try mapping it with std::priority_queue where priority will be the count of the word. Choose max-heap for implementing this. OR you can also use std::multimap<int, string> ( int being count and string being your word) for simplicity.

Upvotes: 3

Related Questions