I am in need of some direction. First off I want to admit this is an assignment but I have hit a block. I need to sort, by the number of times a string occurs (count), and output the top 10. I have found what number gives me the top 10 so from there I need to know how to sort them. Any suggestions would be very, very helpful.
Here is my code:
#include <iostream>
#include <string.h>
using namespace std;
struct wl
{
char *word;
int count;
struct wl * next;
};
int main()
{
struct wl *start, *p1, *p2, *p3;
char tmp[256], *s1;
int p7;
start = NULL;
while (1){
cin>>tmp;
if (cin.eof()) break;
for (p1 = start; p1 != NULL; p1 = p1 -> next)
{
if (strcmp(tmp,p1->word)==0){
p1->count++;
break;
}
}
if (p1 != NULL) continue;
p1 = new struct wl;
p1->count=1;
s1= new char [strlen(tmp)+1];
strcpy(s1,tmp);
p1->word=s1;
p1->next=start;
start=p1;
}
for (p1=start; p1 != NULL; p1=p1->next)
{
p7=261;
if (p1->count >p7)
{
cout<<p1->count<<" "<<p1->word<<endl;
}
}
return 0;
}
Thank you for your time and I hope someone can point me in the right direction!
---------- Post updated at 11:48 PM ---------- Previous update was at 11:45 PM ----------
I also forgot to add that the input is from an external text file that contains a bunch of words
---------- Post updated 04-05-10 at 12:02 AM ---------- Previous update was 04-04-10 at 11:48 PM ----------
Also, once again I remember that I am supposed to use the Linux sort commands somehow, unfortunately I can't seem to figure out how to use this within my program