list.c
Upload User: gzelex
Upload Date: 2007-01-07
Package Size: 707k
Code Size: 1k
Development Platform:

MultiPlatform

  1. #include <LEDA/list.h>
  2. #include <LEDA/stream.h>
  3. int cmp_inv(const string& x, const string& y)    { return compare(y,x); }
  4. int cmp_length(const string& x, const string& y) 
  5. { return x.length() - y.length(); }
  6. int ord(const string& x)   { return (x.length() > 0) ? int(x[0]) : 0;}
  7. void upper_case(string& x)
  8. { int d = 'A' - 'a';
  9.   for(int i=0; i<x.length(); i++) 
  10.     if (x[i] >= 'a' && x[i] <= 'z') x[i] += d;
  11.  }
  12. main()
  13.   string fname = read_string("file: ");
  14.   list<string> L;
  15.   if (fname == "cin")
  16.      L.read("L = ");
  17.   else
  18.      L.read(file_istream(fname),EOF);
  19.   L.permute();
  20. // sort lexicographically
  21.   L.sort();   // compare(string,string) used
  22.   L.print("sorted lexicographically:n",'n');
  23.   newline;
  24.   newline;
  25. // sort decreasing
  26.   L.sort(cmp_inv);
  27.   L.print("sorted decrasing:n",'n');
  28.   newline;
  29.   newline;
  30. // sort by length
  31.   L.sort(cmp_length);
  32.   L.print("sorted by length:n",'n');
  33.   newline;
  34.   newline;
  35. // bucket_sort by first character
  36.   L.bucket_sort(0,255,ord);
  37.   L.print("sorted by s[0]:n",'n');
  38.   newline;
  39.   newline;
  40. // turn characters to upper case
  41.   L.apply(upper_case);
  42.   L.print("upper case:n",'n');
  43.   newline;
  44.   newline;
  45.   cout << "used memory = " << used_memory() << " bytesn";
  46.   return 0;
  47. }