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