File:  [Research Unix] / researchv10no / cmd / cfront / libstring / test3.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));
	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);
			pairST	x;
			pairST	y;
			while ( it.nextX(x) && it.peekX(y) )
				if ( x->word > y->word ) {
					it.remove();
					it.next();
					it.r_insert(x);
					swapFlag = TRUE;
				} else if ( x->word == y->word ) {
					it.remove();
					y->count += x->count;
					delete x;
				}
		} 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.