|
|
1.1 root 1: // list test program. Sorts standard input, prints with counts
2: #include "pair.h"
3: #include "list.h"
4:
5: plistdeclare(pair)
6: plistimplement(pair)
7: plistoutimplement(pair)
8:
9: pairP_list sort(pairP_list);
10:
11: main()
12: {
13: String s;
14: pairP_list myList;
15: /* input */
16: while (cin >> s)
17: myList.put(new pair(s,1));
18: myList = sort(myList);
19: /* print result */
20: operator<<(cout,myList) << "\n"; // work around iostream bug
21: pair *p;
22: while (myList.getX(p))
23: delete p;
24: }
25:
26: pairP_list
27: sort(pairP_list aList) // straight insertion
28: {
29: pairP_list_iterator unsorted(aList);
30: if ( unsorted.next() ) {
31: pair *p;
32: while ( unsorted.r_removeX(p) ) {
33: pairP_list_iterator sorted = unsorted;
34: pair **q; // pointer into sorted part
35: while ( sorted.r_nextX(q) && (*q)->word > p->word )
36: ;
37: if ( (*q)->word < p->word )
38: sorted.next(); // back up
39: else if ( (*q)->word == p->word ) {
40: (*q)->count += p->count;
41: delete p;
42: continue;
43: }
44: sorted.r_insert(p);
45: }
46: }
47: return aList;
48: }
49:
50: ostream&
51: operator<<(ostream& oo, pair *p)
52: {
53: return oo << "(" << p->word << ", " << p->count << ")\n";
54: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.