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.

At line 49, you have this:

nodeType *head_ptr *newNode;

which is missing a comma, to give this:

nodeType *head_ptr, *newNode;

But, I guess you declare the "newNode" pointer again at line 59 (which is a better place to declare it), so line 49 should probably be:

nodeType *head_ptr;

That should fix your problem. If you have other problems, let us know.

Now it just says D://BubbleSort/..... Line 63 warning unused variable head_ptr and newNode
I gotta make sure there's no warning when I test this thing

It's probably how I got the while statement used.

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;
}

Thing is, that's the only problem I have with my program, everything else checks out fine.

From that last snippet, remove line 3. You don't need the head_ptr variable, since you never use it in the function (as the compiler is telling you), and the newNode variable is declared later (within the while loop). So, the function would become:

istream& operator>>(istream& infile, Bubble &mylist)
{    
   int num;
   int manynodes = 0;  // you need to initialize that variable.

   infile >> num;

   while(infile)
   {
      cout << num << " ";

      nodeType *newNode = new nodeType;
      newNode->info = num;

      newNode ->link = mylist.head_ptr;
      mylist.head_ptr = newNode;

      infile >> num;
      manynodes++;
   }

   return infile;
}

I did it and it already complied and build okay. However, the output I want is not showing up, it is completely blank. Also I still haven't done anything about the void bubblesort I have to use pointers on it.

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++;
      }
 }
}

It'll probably change once I get that figured out

Don't mean to double post, but besides that there's still something that must be causing my out file to not display both the unsorted and sorted numbers.

That's right I almost forgot, I need to change void bubblesort above so that it uses pointers. I need some help with that.

Can you post your .cpp file so that I could check it out and understand how it really works?

#include <iostream>
#include <cstdlib>
using namespace std;

#include "Bubble.h"


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)
{
    int num;
    int manynodes = 0;

    infile >> num;

    while(infile)
    {
         cout <<"Unsorted: " << num << " ";

         nodeType *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 << "Sorted: " << 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++;
      }
  }
}

Here is my .cpp file for Bubble.cpp all I know is that I also have to make sure the void bubblesort also uses pointers as well. And I need to know if the operator to read in and write out operater>> and operator<< is correct.

Can someone help me here. The program build and compiled fine. but I got no output showing reading in and printing out there Notepad

istream& operator>>(istream& infile, Bubble& mylist)
{
    int num;
    if (!(infile >> num) )
        return infile;
    assert(num>=0);

    cout << "Unsorted: " << '\n';

    int buf;
    while (num && (infile >> buf) )
    {
        cout << buf << " ";
        nodeType* newNode = new nodeType;
        newNode->info = buf;
        newNode->link = mylist.head_ptr;
        mylist.head_ptr = newNode;
        num--;
    }

    if (num)
        cout << "The number of obtained elements is lesser than required" << endl;

    return infile;
}

ostream& operator<<(ostream& outfile, const Bubble& alist)
{
    outfile << "Sorted:" << '\n';

    nodeType* current = alist.head_ptr;
    while(current)
    {
        outfile << current->info << " ";
        current = current->link;
    }
    return outfile;
}

Edited 4 Years Ago by sergeych: I've found some mistakes

Try this. I can't check whether my solution works or not because the code you gave me isn't complete, it doesn't contain Bubble.h and the main function.

void bubblesort(int* mylist, int numitems)
{
  int temp;
  int iteration =1;
  int index;
  bool noexchanges = false;
  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) = temp;
              noexchanges = false;
          }
          iteration++;
      }
  }
}

As you asked, just with pointers :)

I guess manynodes is the class parameter holding the number of nodes in a list. If it's true, this parameter should be updated in operator>>. Just like this:

mylist.manynodes = num;

On the other hand, it's bad practice to allow modifying such class parameters. You should define a function called addNode that takes an int. This function gets the int and inserts it into a list as a nodeType.

PS.

Use mylist.head_ptr =0 instead of head_ptr = NULL;

Edited 4 Years Ago by sergeych: mistakes

Use mylist.head_ptr =0 instead of head_ptr = NULL;

This means "use 0 instead of NULL".

//Main Program main.cpp

#include <iostream>
#include <fstream>

using namespace std;

#include "Bubble.h"

int main()
{
    ifstream infile;
    ofstream outfile;

    infile.open("D://sort.txt");
    outfile.open("D://sort.out");

    if(infile)
    {
        cout << "The file is open.";
    }

    Bubble mylist;

    infile >> mylist;

    cout <<"The data is now read.";

    outfile << mylist;

    //inData.close(); //Close infile
    //outData.close(); //Close outfile
    return 0;
}
#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

struct nodeType
{
    int info;
    nodeType *link;
};

class Bubble
{
    public:
          Bubble(); //constructor
                    //deconstructor
                    //copy constructor
                    //operator= (overloading the = operator)
          friend istream & operator>> (istream &infile, Bubble & mylist);
          friend ostream & operator<< (ostream &outfile, const Bubble & alist);
          int size()const;
          void bubblesort();

    private:
          nodeType *head_ptr;
          int manynodes;
};

Here's both my main program and Header file. Now it should make much sense in what the heck did I do to get my logic error. I heard from a classmate that logic error is at operator<<, the current that I decleared is NULL and is pointing somewhere else in my Bubble.cpp file is one of the error as I discovered. I need someone to take these files and run so they can find out what I did wrong at operator>>.

I just need to make sure i got it right because, i'm still not getting the results

This article has been dead for over six months. Start a new discussion instead.