GTK+ IOStream  Beta
<< GTK+ >> add C++ IOStream operators to GTK+. Now with extra abilities ... like network serialisation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BSTSort.C
Go to the documentation of this file.
1 /* Copyright 2000-2013 Matt Flax <flatmax@flatmax.org>
2  This file is part of GTK+ IOStream class set
3 
4  GTK+ IOStream is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  GTK+ IOStream is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You have received a copy of the GNU General Public License
15  along with GTK+ IOStream
16  */
17 #include "BST.H"
18 #include <math.h>
19 #include <time.h>
20 
21 // function to generate a random string
22 string randomStrGen(int length) {
23  static string charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
24  string result;
25  result.resize(length);
26 
27  for (int i = 0; i < length; i++)
28  result[i] = charset[rand() % charset.length()];
29 
30  return result;
31 }
32 
33 // function to generate a random int
34 int findRand(int cnt){
35  int r=(int)round((float)(rand()-RAND_MAX/2)*2.0/(float)RAND_MAX*(float)cnt);
36  return r;
37 }
38 
39 int main(int argc, char *argv[]){
40  srand(time(NULL)); // set the random seed
41 
42  int cnt=10; // the number of items to generate
43 
44  cout<<"want to sort the following :"<<endl;
45 
46  // add a bunch of strings to the link list - could be done with ints as well
47  LinkList<string *> linkList;
48  for (int i=0;i<cnt;i++){
49  string *tempS=new string(randomStrGen(4));
50  linkList.add(tempS);
51  cout<<*tempS<<'\t';
52  }
53  cout<<endl;
54 
55  BST<string> bst; // create the Binary Search Tree
56  bst.sort(&linkList, &string::compare); // sort the LinkList
57 
58  cout<<"The result is :"<<endl; // display the result
59  linkList.grab(1); linkList.prev();
60  for (int i=1; i<=linkList.getCount(); i++)
61  cout<<(*linkList.next())<<'\t';
62  cout<<endl;
63 
64  while (linkList.getCount()) // empty the LinkList
65  delete linkList.remove();
66  return 0;
67 }