My name is Leonard E. Norwood Jr. I'm just practicing a simple bubblesort program but this time using classes and pointers with operators >> and << and also an overloaded operator =. I'm merely trying to sort a list of numbers from lowest to highest. And the purpose of the bubblesort is to search for unsorted numbers to compare and swap until the list is fully sorted. This is my list for practice: 29 23 12 39 32 37 41 52 67 59 103 91. You can easily sort that out in your head. I just got one error that needs to be fixed.
Here's my code:
Bubble.cpp
Bubble::Bubble()
{
head_ptr = NULL;
manynodes = 0;
}
void list_clear(nodeType*& head_ptr)
{
nodeType * removeptr;
while (head_ptr != NULL)
{
removeptr = head_ptr;
head_ptr = head_ptr->link;
delete removeptr;
}
}
void list_copy(const nodeType* source_ptr, nodeType*& head_ptr, nodeType*& tail_ptr)
{
nodeType* temp;
head_ptr = NULL;
tail_ptr = NULL;
if(source_ptr == NULL)
return;
head_ptr = new nodeType;
head_ptr->link = NULL;
head_ptr->info = source_ptr->info;
tail_ptr = head_ptr;
source_ptr = source_ptr->link;
while(source_ptr != NULL)
{
temp = new nodeType;
temp->link = NULL;
temp->info = source_ptr->info;
tail_ptr->link = temp;
tail_ptr = tail_ptr->link;
source_ptr = source_ptr->link;
}
}
int Bubble::size() const
{
return manynodes;
}
istream& operator>>(istream& infile, Bubble &mylist)
{
nodeType *head_ptr *newNode;
int num;
int manynodes;
infile >> num;
while(infile)
{
cout << num << " ";
nodeType *newNode;
newNode = new nodeType;
newNode->info = num;
newNode ->link = mylist.head_ptr;
mylist.head_ptr = newNode;
infile >> num;
manynodes++;
}
return infile;
}
ostream& operator<<(ostream& outfile, const Bubble& alist)
{
nodeType *current;
current = alist.head_ptr;
while(current != NULL)
{
outfile << current->info << " ";
current = current->link;
}
return outfile;
}
void bubblesort(int mylist[], int numitems)
{
int temp;
int iteration;
int index;
bool noexchanges = false;
iteration = 1;
while(iteration < numitems && !! noexchanges)
{
noexchanges = true;
for (index = 0; index < numitems - iteration; index++)
{
if(mylist[index] > mylist[index + 1])
{
temp = mylist[index];
mylist[index] = mylist[index + 1];
mylist[index + 1] = false;
}
iteration++;
}
}
}
Bubble::Bubble()
{
head_ptr = NULL;
manynodes = 0;
}
void list_clear(nodeType*& head_ptr)
{
nodeType * removeptr;
while (head_ptr != NULL)
{
removeptr = head_ptr;
head_ptr = head_ptr->link;
delete removeptr;
}
}
void list_copy(const nodeType* source_ptr, nodeType*& head_ptr, nodeType*& tail_ptr)
{
nodeType* temp;
head_ptr = NULL;
tail_ptr = NULL;
if(source_ptr == NULL)
return;
head_ptr = new nodeType;
head_ptr->link = NULL;
head_ptr->info = source_ptr->info;
tail_ptr = head_ptr;
source_ptr = source_ptr->link;
while(source_ptr != NULL)
{
temp = new nodeType;
temp->link = NULL;
temp->info = source_ptr->info;
tail_ptr->link = temp;
tail_ptr = tail_ptr->link;
source_ptr = source_ptr->link;
}
}
int Bubble::size() const
{
return manynodes;
}
istream& operator>>(istream& infile, Bubble &mylist)
{
nodeType *head_ptr *newNode;
int num;
int manynodes;
infile >> num;
while(infile)
{
cout << num << " ";
nodeType *newNode;
newNode = new nodeType;
newNode->info = num;
newNode ->link = mylist.head_ptr;
mylist.head_ptr = newNode;
infile >> num;
manynodes++;
}
return infile;
}
ostream& operator<<(ostream& outfile, const Bubble& alist)
{
nodeType *current;
current = alist.head_ptr;
while(current != NULL)
{
outfile << current->info << " ";
current = current->link;
}
return outfile;
}
void bubblesort(int mylist[], int numitems)
{
int temp;
int iteration;
int index;
bool noexchanges = false;
iteration = 1;
while(iteration < numitems && !! noexchanges)
{
noexchanges = true;
for (index = 0; index < numitems - iteration; index++)
{
if(mylist[index] > mylist[index + 1])
{
temp = mylist[index];
mylist[index] = mylist[index + 1];
mylist[index + 1] = false;
}
iteration++;
}
}
}
And here's my log right here:
J:\BubbleSort\Bubble.cpp: In function `std::istream& operator>>(std::istream&, Bubble&)':
J:\BubbleSort\Bubble.cpp:63: error: expected primary-expression before '*' token
J:\BubbleSort\Bubble.cpp:63: error: head_ptr' undeclared (first use this function) J:\BubbleSort\Bubble.cpp:63: error: (Each undeclared identifier is reported only once for each function it appears in.) J:\BubbleSort\Bubble.cpp:63: error:
newNode' undeclared (first use this function)
Execution terminated
And the main program to all this is just there to print out the unsorted and sorted list.
So I just need to know what to do about the error above, that and the bubblesort I need to work on with pointers that's all. Any examples are helpful. I'm using CodeBlocks really, but I had to use DevC++ Bloodshed to print out the build messages since my Codeblocks is on my laptop and DevC++ is on my PC. Thank you.