sort issues

Hey all I am new here and have been dealing with this issue for the entire weekend. This is an assignment, I will say so upfront, and I am just confused as to how to even start to sort this program. I have an external .txt file that serves as the input to this program and I need to sort the output so it shows the top 10 words that occur the most often.

Here is the code I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>

#include <string.h>



using namespace std;



struct wl

	{

	char *word;

	int count;

	struct wl * next;

	};



int main()

	{

	struct wl *start, *p1, *p2;

	char tmp[256], *s1;

	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)

	{

		cout<<p1->count<<" "<<p1->word<<endl;

	}

return 0;

}



I just need to know what the proper sort function that needs to be used and I can attempt to figure it out, but any help would be greatly appreciated! Thanks!

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <string.h>
using namespace std;

struct wl
{
     char *word;
     int count;
     struct wl * next;
};


bool is_less(const wl * wl1, const wl * wl2)
{
     return wl1->count>wl2->count;
}

int main()
{
     struct wl *start, *p1, *p2;
     char tmp[256], *s1;
     start = NULL;
     int elements=0;
     
     while (true)
     {
          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;
    
          elements++;
  		  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)
     {
          cout<<p1->count<<" "<<p1->word<<endl;
     }
     
     cout << endl;
	
	
	 wl ** array=new wl*[elements];
	 int i=0;
	 for (p1=start; p1 != NULL; p1=p1->next)
	 {
              array[i++]=p1;
         }
	
	 sort(array,array+elements,is_less);
	
	 cout << "sorted..." << endl;
         for (i=0; i<elements && i<10; i++)
         {
              cout << array[i]->count << " " << array[i]->word << endl;
         }
	 delete[]array;
	
	 p1=start;
	 while (p1)
	 {
           p2=p1;
           p1=p1->next;
           delete p2;
         }


     system("pause");
     return 0;
}
Last edited on
Topic archived. No new replies allowed.