I have the following struct for a linked list:
struct Node {
int type;
int otherInfo;
Node * next;
}
I want to create a sorted (eventually) linked list that keeps track of how many times each “type” appears. An example node would be:
struct Node2 {
int type;
int frequency;
//A linked list to keep track of the "otherInfo"
Node2 * next;
}
My current algorithm is O(n^2), which is unreasonable because the original linked list can sometimes have over 30,000 elements. Is there a more efficient way to do this?
Note: type can be pretty much any positive integer, so I can’t simply make an static array with type as the indexes.
EDIT: The frequency list does not have to be a linked list. I gave that as an example. Later on in the program, I sort the frequency list using a heap. I just need to be able to traverse the frequency list later.
Sounds like what you really need is a map or a basic binary search tree. The key type will be your
typevalue, and the value type will hold yourfrequency.