1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
My .h file
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
struct nodeType //struct for linked list
{
int info;
nodeType *link;
};
class Bubble
{
public:
Bubble(); //Default constructor
~Bubble(); //Destructor
int size() const;
Bubble(const Bubble & inclass); //Copy constructor
friend istream & operator>>(istream &infile, Bubble & inclass);
friend ostream & operator<<(ostream &myfile, const Bubble & outclass);
const Bubble& operator=(const Bubble& otherlist);
void Bubblesort();
private:
nodeType * head_ptr;
int manynodes;
};
void list_clear(nodeType* &head_ptr);
void reverselist(nodeType* &mylist);
My .cpp file
#include <iostream>
#include <cstdlib>
#include "Bubble.h"
using namespace std;
void Bubble::Bubblesort()
{
int temp, x;
nodeType *beg, *follow, *end, *prev;
int iteration=1;
bool noexchanges = false;
beg = head_ptr;
end = head_ptr;
while(!noexchanges && end->link != NULL)
{
noexchanges=true;
while (iteration<manynodes)
{
prev=end;
end=end->link;
if(end->link == NULL)
{
cout<<endl;
cout<<iteration<<" doesn't work. No way man!"<<endl;
cout<<endl;
}
iteration++;
}
follow = head_ptr->link;
//prev=end;
end=end->link;
for(x=0; x<iteration-manynodes; x++)
{
if ((prev->info) > (end->info))
{
temp=prev->info;
prev->info=end->info;
end->info=temp;
noexchanges=false;
}
iteration++;
follow=follow->link;
prev=follow;
//end=prev;
} // inner loop
end = prev;
} // outer while loop
}
istream& operator>>(istream & infile, Bubble & inclass)
{
nodeType *follow, *beg,*end;
int tempnum;
list_clear(inclass.head_ptr);
inclass.head_ptr=NULL;
beg, end=NULL;
infile>>tempnum;
while (infile)
{
follow = new nodeType;
follow->info=tempnum;
follow->link=NULL;
if (inclass.head_ptr==NULL)
{
inclass.head_ptr=follow;
}
else
{
end->link=follow;
}
end=follow;
infile>>tempnum;
inclass.manynodes++;
}
cout<<endl;
return infile;
}
ostream& operator<<(ostream & myfile, const Bubble & outclass)
{
nodeType *current;
current=outclass.head_ptr;
while(current != NULL)
{
myfile<<current->info<<" ";
cout<<current->info<<" ";
current=current->link;
}
return myfile;
}
Bubble::Bubble() //Constructor
{
manynodes=0;
head_ptr = NULL;
}
Bubble::Bubble(const Bubble & inclass) //Copy constructor
{
nodeType *tail_ptr;
//list_copy(inclass.head_ptr,head_ptr,tail_ptr);
}
Bubble::~Bubble() //Destructor
{
list_clear(head_ptr);
}
const Bubble& Bubble::operator=(const Bubble& otherlist)
{
nodeType *tail_ptr;
if(this != &otherlist)
list_clear(head_ptr);
//list_copy(otherlist.head_ptr, head_ptr, tail_ptr);
return otherlist;
}
void list_clear(nodeType* & head_ptr)
{
nodeType * removeptr;
while(head_ptr != NULL)
{
removeptr = head_ptr;
head_ptr = head_ptr->link;
delete removeptr;
}
}
void reverselist (nodeType * mylist)
{
nodeType* templist;
nodeType* tempnode;
templist=NULL;
while (mylist!= NULL)
{
tempnode = mylist;
mylist= mylist->link;
tempnode->link = templist;
templist=tempnode;
}
mylist=templist;
}
My main
#include <iostream>
#include <cstdlib>
#include <fstream>
#include "Bubble.cpp"
using namespace std;
int main()
{
ofstream myfile; //interface to write data to files.
ifstream yourfile; //interface to gather data from files.
yourfile.open("bubble input.txt"); // where the data is coming from
myfile.open("bubble output.txt"); //where the data is going to
myfile<<"Lab CSC"<<endl;
Bubble number1;
cout<<number1<<endl;
while(yourfile)
{
yourfile>>number1;
myfile<<number1<<endl;
number1.Bubblesort();
myfile<<number1<<endl;
}
yourfile.close();
myfile.close();
cout<<endl;
cout<<endl;
cout<<"Goodbye."<<endl;
system ("pause");
cout<<endl;
return 0;
}
|