|
|
1.1 root 1: /* sort standard input and print unique word count */
2: #include "pair.h"
3: #include "list.h"
4:
5: listdeclare(pairST)
6: listoutimplement(pairST)
7:
8: pairST_list sort(pairST_list);
9:
10: main()
11: {
12: String s;
13: pairST_list myList;
14: /* input */
15: while (cin >> s)
16: myList.put(new pair(s,1));
17: // cout << myList << "\n";
18: myList = sort(myList);
19: /* print result */
20: cout << myList << "\n";
21: }
22:
23: pairST_list
24: sort(pairST_list myList)
25: {
26: /* (bubble) sort */
27: int swapFlag;
28: if ( myList )
29: do {
30: swapFlag = FALSE;
31: pairST_list_iterator it(myList);
32: pair *x;
33: pair *y;
34: while ( it.nextX(x) && it.peekX(y) ) {
35: // cout << "Compare " << x << " and " << y << "\n";
36: if ( x->word > y->word ) {
37: // cout << "greater\n";
38: it.remove();
39: it.next();
40: it.r_insert(x);
41: swapFlag = TRUE;
42: } else if ( x->word == y->word ) {
43: // cout << "equal\n";
44: it.remove();
45: y->count += x->count;
46: delete x;
47: }
48: }
49: // cout << myList;
50: } while ( swapFlag );
51: return myList;
52: }
53:
54:
55: ostream&
56: operator<<(ostream& oo, pairST p)
57: {
58: return oo << "\n" << p->word << "\t" << p->count;
59: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.