Bubblesorting a Linked list

Hello all,
I have been given 2 sets of numbers from a file.

Set 1) 25 15 45 35 65 55

Set 2) 29 23 12 39 32 37 41 52 67 59 103 91

I need to take a set of numbers from a file and place into a linked list (using an operator), Bubble Sort the Linked list then output the set to a file in ascending order (using an operator). Here's what I have so far. There are no problems, it compiles. Yet, it won't sort at all. In the bubblesort function, I am trying to move my pointer *end to the end of the linked list and the pointer *prev right behind *end, so it can bubblesort from the end of the list to the front. Thanks for your time and help.

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

Can you please explain the algorithm you are using in your bubble sort. What does the input look like and what is the body of the function doing?
No input from user, sets of numbers came from a .txt file. Used the >> operator to bring them in and place them into a linked list.

I start the *end pointer at the head pointer (head_ptr, given by the class) of the linked list and used the 2nd while loop to move the *end pointer to the end of the linked list and the *prev pointer right behind it.

The for loop, controlled by the length of the linked list, traverses the linked list, backwards. The If statement compares the two integers where *prev and *end is pointing to, respectively. If prev->info is bigger than end->info, then prev->info and end->info switch values with the help of temp. Then both pointers move 1 spot to the left, all the way to the beginning. At the end of the function, *end and *prev should both be at the head pointer and the linked list should be sorted lowest to highest.
main

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
#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 << "\nSorted list....\n";
      myfile<<number1<<endl;    
   }
   
   yourfile.close();
   myfile.close();
   
   cout<<endl;
   cout<<endl;
   
   
   cout<<"Goodbye."<<endl;
   cout<<endl;
   return 0;
}



bubble.h

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
#include <iostream>
#include <cstdio>
#include <fstream>
#include <cstdlib>
#include <algorithm>

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


bubble.cpp

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
#include "Bubble.h"

using namespace std;

void Bubble::Bubblesort()
{
   nodeType *follow, *end, *prev;
   bool exchanges = true;
   end = head_ptr;
   
   while( exchanges )
   {
      exchanges = false;
      
      prev = end;
      follow = prev->link;
      while ( follow )
      { 
	 if ((prev->info) > (follow->info))
	 {
	    swap( prev->info, follow->info );
	    exchanges = true;
	 }
	 prev = prev->link;
	 follow = prev->link;	 
      }
   }
}

istream& operator>>(istream & infile, Bubble & inclass)
{
   
   nodeType *follow,*end;
   int tempnum;
   list_clear(inclass.head_ptr);
   inclass.head_ptr=NULL;
   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;
}


output

Lab CSC
25 15 45 35 65 55 29 23 12 39 32 37 41 52 67 59 103 91 

Sorted list....
12 15 23 25 29 32 35 37 39 41 45 52 55 59 65 67 91 103 


When I get back from work, I will show you an optimized version of the sorting
Last edited on
Topic archived. No new replies allowed.