So I'm writing a program in C that reads in a bunch of words from an unspecified number of files and I'm to print out the top N words in the file. I'm using a hash to store them in, where each bucket is a linked list. The lists are sorted by alphabetical order, and every Node has a word and count variable (obvioiusly I only store unique words, repeats increment the count). I have the linked list, the hash, and the reading all working, but what I can't figure out, is an easy way to get the words with the highest counts out of the hash efficiently and print them out.
I keep track of the total number of unique words. Unless I make another array to the size of N, and keep checking in there every time I go through a Node in the hash to see if it's count is bigger than anyone in there, but that seems like way too much trouble.

My hash is basically

Node** hash  = (Node**)malloc(sizeof(Node*)*HASHSIZE);

any particular reason to use a hash? A simple linked list of structures would do the job. When done reading just sort the list by frequency and you're done.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.