I tried to sort a list, by copying each of the nodes' data into an array. Then after the code that sorted the array, I attempted to copy the value in the elements of the array into each of the nodes' list. Is that even possible, I tried researching the question couldn't find a straight up yes or no. I don't want to use qsort from cstdlib, this crashes, I would like to know if there is a way to make this work. Insight appreciated.
template <typename NODETYPE>
void List<NODETYPE>::sort(){
ListNode<NODETYPE>* currentPtr = firstPtr;
int N = sizeOfList();
NODETYPE a[N];
int l = 0;
int r = 0;
int i,j,min,imin,tmp;
while(currentPtr != NULL){
a[l] = currentPtr->data;
currentPtr = currentPtr ->nextPtr;
l++;
}
for (i=0;i<N-1;i++)
{
imin=i;
min=a[i];
for (j=i+1;j<N;j++)
if (a[j]<min)
{
min=a[j];
imin=j;
}
tmp=a[imin];
a[imin]=a[i];
a[i]=tmp;
}
for ( int y = 0; y < N-1; y++ ){
currentPtr->data = a[y];
currentPtr = currentPtr->nextPtr;
}
lastPtr->data = a[N];
}
int N = sizeOfList(); NODETYPE a[N];is not valid C++, since arrays cannot be declared using a variable as the number of items.NODETYPE a[N]is not standard C++, it's an extension in some compilers. Be aware that if the list is large, this will result in blowing your stack away. Consider usingstd::vectorinstead. You may also want to look intostd::swap.Listclass have a public interface to allow the user from the outside to change the List's data? If not, you need to write one, as it is useless if there is no way to change the data. Is there a way for the outside world to iterate through the list, from beginning to end? Again, I ask this since it would be useless without it. Having those two functions allows you to "sort" the list easily by just utilizing what you've already written.