i am just looking for help to understand this. in order to add the member functions i need to enter the function in public (not sure as to how) ie, bool Bsort() or int Bsort() and i am also asking if i put the actual sort code in after the urinary operator/constructer for the Bsort or does it go somewhere else. i have to compile on g++.


1. Add two member functions to the List class performing two separate sorting operations.
2. Use the functions in step(2) to sort the linked list of step (1).


#include <iostream.h>
//typedef int bool; // if your compiler doesn't support boolean type uncomment this
typedef int ListElement;
struct cell // the basic building block of a linked list
{
ListElement item; // the data in this cell
struct cell *next; // the pointer to link to the next cell
};
typedef struct cell* cellptr; // define the type of the pointer to a cell
class List
{
private:
cellptr header; // the header pointer of the linked list
public:
List();
~List();
bool IsListEmpty();
int Length();
bool ListInsert(ListElement);
bool ListDelete(ListElement);
cellptr Retrieve(ListElement);
void PrintList();
int SumList();
float AverageList();
bool Swap (cellptr p1, cellptr p2);
bool Reverse ();
};
List::List():header(NULL)
{
};
bool List::IsListEmpty()
{
return(header == NULL);
};
List::~List()
{
cellptr temp = header;
while(temp!=NULL)
{
header = temp->next;
temp->next = NULL;
delete temp;
temp = header;
}
header = NULL;
};
int List::Length()
{
cellptr temp = header;
int counter=0;
while(temp!=NULL)
{
counter++;
temp=temp->next;
}
return(counter);
};
bool List::ListInsert(ListElement n)
{
cellptr temp = new struct cell;
if(temp==NULL)
{
cout << "Not enough memory!" << endl;
return(0);
}
else
{
temp->item=n;
temp->next=header;
header=temp;
return(1);
}
}
bool List::ListDelete(ListElement n)
{
cellptr temp1 = header, temp2;
if(temp1==NULL)
{
cout << "Empty list!" << endl;
return(0);
}
else if(temp1 -> item==n)
{
header=temp1 -> next;
temp1->next=NULL;
delete temp1;
return(1);
}
else
{
temp2=temp1;
temp1=temp1->next;
while((temp1 != NULL)&&(temp1 -> item != n))
{
temp2=temp1;
temp1=temp1->next;
}
if(temp1==NULL)
{
cout << "Element " << n << " not found!" << endl;
return(0);
}
else
{
temp2->next = temp1->next;
temp1->next=NULL;
delete temp1;
return(1);
}
}
}

// The retrieve function locates a list element in the list and returns
// the pointer to the element. If the element is not found it returns NULL.
cellptr List::Retrieve(ListElement n)
{
cellptr temp = header;
if(temp==NULL)
{
cout << "Empty list!" << endl;
return(NULL);
}
else
{
while((temp != NULL)&&(temp -> item != n))
temp=temp->next;
if(temp==NULL)
{
cout << "Element " << n << " not found!" << endl;
return(NULL);
}
else
return(temp);
}
}
void List::PrintList()
{
cellptr temp = header;
cout << "Showing the list:" << endl;
while(temp!=NULL)
{
cout << temp -> item << endl;
temp=temp->next;
}
}
//Finds the sum of elements in the linked list
int List::SumList()
{
int sum;
cellptr temp = header;
sum = 0;
while(temp != NULL)
{
sum = sum + temp -> item;
temp = temp -> next;
}
cout<< "The sum of the elements in the linked list is: ";
cout<<sum<<endl;
return sum;
}
//Finds the average of elements in the linked list
float List::AverageList()
{
float Avg;
Avg = float (SumList())/Length();
cout<<"The list average is :"<<Avg<<endl;
return Avg;
}
//Swaps the element pointed by p1 with that pointed by p2
bool List::Swap ( cellptr p1, cellptr p2)
{
cellptr temp = header;
int temporary;
temporary = p2 -> item;
p2 -> item = p1 -> item;
p1 -> item = temporary;
return 1;
}
//Reverses the elements in the linked list
bool List::Reverse ()
{
cellptr temp = header;
int size = Length(), i;
for (i=0; i<size/2; i++)
{
Swap(Retrieve(i), Retrieve(size-1-i));
temp = temp ->next;
}
return 1;
}
main()
{
List l;
for(int i=0; i<10; i++)
l.ListInsert(i);
l.PrintList();
/*l.ListDelete(5);
l.ListDelete(9);
l.ListDelete(11);
l.ListInsert(300);
l.ListInsert(101);
l.PrintList();
*/
//Finds the sum of elements in the linked list
l.SumList();
l.PrintList();
//Finds the average of the elements in the linked list
l.AverageList();
l.PrintList();
//Reverses the elements in the linked list
cout<<"Here is the reversed list - "<<endl;
l.Reverse();
l.PrintList();
//Swaps the fifth element with the 7th element
cout<<"Here is the list when elements 5 and 7 are swapped - "<<endl;
l.Swap ( l.Retrieve (5), l.Retrieve (7));
l.PrintList();
}
/*
Showing the list:
0
1
2
3
4
5
6
7
8
9
Here is the list when elements 5 and 7 are swapped -
Showing the list:
0
1
2
3
4
7
6
5
8
9
*/