I have written a program and am having trouble with the bubblesort... i get very confused on it and am not too familiar with link lists in the first place. The link list runs succesfully when i dont have the bubble sort in the program and it does what its supposed to do. However, i need to sort both list. if i just have some clarification on what to do with my bubble sort on the first list i can figure it out on the second. I understand there is a nested for loop for the bubblesort as you can see but i dont have it set up properly. any help would be appreciated and thank you in advance.
[code=cpp]
void main()
{
cout << "Insert 12 numbers \n" ;
node_type *first,*p,*q,*n ewnode,*temp;
int i,a,b,j;
float sum,average;
sum = 0;
first = new node_type;
p = first;
getdata(a);
(*first).flist = a;
(*first).next = null;
for (i =0; i<=10;++i)
{
getdata(a);
newnode = new node_type;
(*newnode).flis t = a;
(*newnode).next = null;
//**********Links previous node to newnode******//
(*p).next = newnode;
p = newnode;
}
//**********Trave rses list and prints out nodes in chronological order ******//
q = first;
cout << "Unsorted list \n";
while (q != null)
{
cout << (*q).flist << "\n";
sum = sum + (*q).flist;
q = (*q).next;
}
average = sum/12;
cout << "\nThe average of the numbers is " << average << "\n";
//*************** **Bubblesort*** *************** ***//
for ((*q).flist = first; (*q).next != null; (*q).flist = (*q).next)
{
for ((*p).flist = (*q).next; (*p).next != null; (*p).flist = (*p).next)
{
if (q > p)
{
temp = q;
q = p;
p = temp;
}
}
}
q = first;
cout << "Sorted list \n";
while (q != null)
{
cout << (*q).flist << "\n";
q = (*q).next;
}
//**********start of second list(b)******** *****//
cout << "Insert 6 numbers \n" ;
first = new node_type;
p = first;
getdata2(b);
(*first).seclis t = b;
(*first).next = null;
for (i =0; i<=4;++i)
{
getdata2(b);
newnode = new node_type;
(*newnode).secl ist = b;
(*newnode).next = null;
//**********Links previous node to newnode******//
(*p).next = newnode;
p = newnode;
}
//**********Trave rses list and prints out nodes in chronological order ******//
q = first;
cout << "Unsorted list \n";
while (q != null)
{
cout << (*q).seclist << "\n";
q = (*q).next;
}
}[/code]
[code=cpp]
void main()
{
cout << "Insert 12 numbers \n" ;
node_type *first,*p,*q,*n ewnode,*temp;
int i,a,b,j;
float sum,average;
sum = 0;
first = new node_type;
p = first;
getdata(a);
(*first).flist = a;
(*first).next = null;
for (i =0; i<=10;++i)
{
getdata(a);
newnode = new node_type;
(*newnode).flis t = a;
(*newnode).next = null;
//**********Links previous node to newnode******//
(*p).next = newnode;
p = newnode;
}
//**********Trave rses list and prints out nodes in chronological order ******//
q = first;
cout << "Unsorted list \n";
while (q != null)
{
cout << (*q).flist << "\n";
sum = sum + (*q).flist;
q = (*q).next;
}
average = sum/12;
cout << "\nThe average of the numbers is " << average << "\n";
//*************** **Bubblesort*** *************** ***//
for ((*q).flist = first; (*q).next != null; (*q).flist = (*q).next)
{
for ((*p).flist = (*q).next; (*p).next != null; (*p).flist = (*p).next)
{
if (q > p)
{
temp = q;
q = p;
p = temp;
}
}
}
q = first;
cout << "Sorted list \n";
while (q != null)
{
cout << (*q).flist << "\n";
q = (*q).next;
}
//**********start of second list(b)******** *****//
cout << "Insert 6 numbers \n" ;
first = new node_type;
p = first;
getdata2(b);
(*first).seclis t = b;
(*first).next = null;
for (i =0; i<=4;++i)
{
getdata2(b);
newnode = new node_type;
(*newnode).secl ist = b;
(*newnode).next = null;
//**********Links previous node to newnode******//
(*p).next = newnode;
p = newnode;
}
//**********Trave rses list and prints out nodes in chronological order ******//
q = first;
cout << "Unsorted list \n";
while (q != null)
{
cout << (*q).seclist << "\n";
q = (*q).next;
}
}[/code]
Comment