File:  [Research Unix] / researchv10no / cmd / cfront / libstring / test13.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Tue Apr 24 17:21:35 2018 UTC (8 years, 1 month ago) by root
Branches: belllabs, MAIN
CVS tags: researchv10, HEAD
researchv10 Norman

/* sort standard input and print unique word count */
#include "pair.h"
#include "list.h"

listdeclare(pairST)
listoutimplement(pairST)

pairST_list	sort(pairST_list);

main()
{
	String	s;
	pairST_list	myList;
	/* input */
	while (cin >> s)
		myList.put(new pair(s,1));
// cout << myList << "\n";
	myList = sort(myList);
	/* print result */
	cout << myList << "\n";
}

pairST_list
sort(pairST_list myList)
{
	/* (bubble) sort */
	int swapFlag;
	if ( myList )
		do {
			swapFlag = FALSE;
			pairST_list_iterator	it(myList);
			pair	*x;
			pair	*y;
			while ( it.nextX(x) && it.peekX(y) ) {
// cout << "Compare " << x << " and " << y << "\n";
				if ( x->word > y->word ) {
// cout << "greater\n";
					it.remove();
					it.next();
					it.r_insert(x);
					swapFlag = TRUE;
				} else if ( x->word == y->word ) {
// cout << "equal\n";
					it.remove();
					y->count += x->count;
					delete x;
				}
			}
// cout << myList;
		} while ( swapFlag );
	return myList;
}


ostream&
operator<<(ostream& oo, pairST p)
{
	return oo << "\n" << p->word << "\t" << p->count;
}

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.